From 6908173414230e15d5160d2bfad3aadc5d8e76bf Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Wed, 17 Feb 2021 16:57:37 +0200 Subject: [PATCH] fix: currencies. --- .../Alerts/Currencies/CurrencyDeleteAlert.js | 75 ++++++++++ .../Currencies/CurrenciesAlerts.js | 10 ++ .../Currencies/CurrenciesDataTable.js | 140 +++++------------- .../Preferences/Currencies/CurrenciesList.js | 86 ++--------- .../Currencies/CurrenciesProvider.js | 27 ++++ .../Preferences/Currencies/components.js | 84 +++++++++++ client/src/lang/en/index.js | 1 + 7 files changed, 245 insertions(+), 178 deletions(-) create mode 100644 client/src/containers/Alerts/Currencies/CurrencyDeleteAlert.js create mode 100644 client/src/containers/Preferences/Currencies/CurrenciesAlerts.js create mode 100644 client/src/containers/Preferences/Currencies/CurrenciesProvider.js create mode 100644 client/src/containers/Preferences/Currencies/components.js diff --git a/client/src/containers/Alerts/Currencies/CurrencyDeleteAlert.js b/client/src/containers/Alerts/Currencies/CurrencyDeleteAlert.js new file mode 100644 index 000000000..48c40f7dd --- /dev/null +++ b/client/src/containers/Alerts/Currencies/CurrencyDeleteAlert.js @@ -0,0 +1,75 @@ +import React from 'react'; +import { + FormattedMessage as T, + FormattedHTMLMessage, + useIntl, +} from 'react-intl'; +import { Intent, Alert } from '@blueprintjs/core'; +import { AppToaster } from 'components'; + +import { useDeleteCurrency } from 'hooks/query'; + +import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect'; +import withAlertActions from 'containers/Alert/withAlertActions'; + +import { compose } from 'utils'; + +/** + * Currency delete alerts. + */ +function CurrencyDeleteAlert({ + name, + + // #withAlertStoreConnect + isOpen, + payload: { currency_code }, + + // #withAlertActions + closeAlert, +}) { + const { formatMessage } = useIntl(); + const { mutateAsync: deleteCurrency, isLoading } = useDeleteCurrency(); + + // handle cancel delete currency alert. + const handleCancelCurrencyDelete = () => closeAlert(name); + + // handle alert confirm delete currency. + const handleConfirmCurrencyDelete = () => { + deleteCurrency(currency_code) + .then((response) => { + AppToaster.show({ + message: formatMessage({ + id: 'the_currency_has_been_deleted_successfully', + }), + intent: Intent.SUCCESS, + }); + closeAlert(name); + }) + .catch(() => { + closeAlert(name); + }); + }; + + return ( + } + confirmButtonText={} + intent={Intent.DANGER} + isOpen={isOpen} + onCancel={handleCancelCurrencyDelete} + onConfirm={handleConfirmCurrencyDelete} + loading={isLoading} + > +

+ +

+
+ ); +} + +export default compose( + withAlertStoreConnect(), + withAlertActions, +)(CurrencyDeleteAlert); diff --git a/client/src/containers/Preferences/Currencies/CurrenciesAlerts.js b/client/src/containers/Preferences/Currencies/CurrenciesAlerts.js new file mode 100644 index 000000000..8fa73e47f --- /dev/null +++ b/client/src/containers/Preferences/Currencies/CurrenciesAlerts.js @@ -0,0 +1,10 @@ +import React from 'react'; +import CurrencyDeleteAlert from 'containers/Alerts/Currencies/CurrencyDeleteAlert'; + +export default function CurrenciesAlerts() { + return ( +
+ +
+ ); +} diff --git a/client/src/containers/Preferences/Currencies/CurrenciesDataTable.js b/client/src/containers/Preferences/Currencies/CurrenciesDataTable.js index 071585af5..a1e131593 100644 --- a/client/src/containers/Preferences/Currencies/CurrenciesDataTable.js +++ b/client/src/containers/Preferences/Currencies/CurrenciesDataTable.js @@ -1,36 +1,35 @@ -import React, { useCallback, useMemo } from 'react'; -import { - Intent, - Button, - Popover, - Menu, - MenuItem, - Position, -} from '@blueprintjs/core'; -import { withRouter } from 'react-router'; -import { useIntl } from 'react-intl'; -import { compose, saveInvoke } from 'utils'; +import React, { useCallback } from 'react'; +import { compose } from 'utils'; -import { DataTable, Icon } from 'components'; +import { DataTable } from 'components'; + +import { useCurrenciesContext } from './CurrenciesProvider'; +import TableSkeletonRows from 'components/Datatable/TableSkeletonRows'; + +import { ActionMenuList, useCurrenciesTableColumns } from './components'; -import withDashboardActions from 'containers/Dashboard/withDashboardActions'; -import withCurrencies from 'containers/Currencies/withCurrencies'; import withDialogActions from 'containers/Dialog/withDialogActions'; +import withAlertActions from 'containers/Alert/withAlertActions'; +/** + * Currencies table. + */ function CurrenciesDataTable({ - // #withCurrencies - currenciesList, - currenciesLoading, - // #ownProps - onFetchData, - onDeleteCurrency, + tableProps, // #withDialog. openDialog, -}) { - const { formatMessage } = useIntl(); + // #withAlertActions + openAlert, +}) { + const { currencies, isCurrenciesLoading } = useCurrenciesContext(); + + // Table columns. + const columns = useCurrenciesTableColumns(); + + // Handle Edit Currency. const handleEditCurrency = useCallback( (currency) => { openDialog('currency-form', { @@ -41,93 +40,30 @@ function CurrenciesDataTable({ [openDialog], ); - const actionMenuList = useCallback( - (currency) => ( - - } - text={formatMessage({ id: 'edit_currency' })} - onClick={() => handleEditCurrency(currency)} - /> - } - text={formatMessage({ id: 'delete_currency' })} - onClick={() => onDeleteCurrency(currency)} - intent={Intent.DANGER} - /> - - ), - [handleEditCurrency, onDeleteCurrency, formatMessage], - ); - - const onRowContextMenu = useCallback( - (cell) => { - return actionMenuList(cell.row.original); - }, - [actionMenuList], - ); - - const columns = useMemo( - () => [ - { - Header: formatMessage({ id: 'currency_name' }), - accessor: 'currency_name', - width: 150, - }, - { - Header: formatMessage({ id: 'currency_code' }), - accessor: 'currency_code', - className: 'currency_code', - width: 120, - }, - { - Header: 'Currency sign', - width: 120, - }, - { - id: 'actions', - Header: '', - Cell: ({ cell }) => ( - -