fix: currencies.

This commit is contained in:
elforjani3
2021-02-17 16:57:37 +02:00
parent 95cccfd13b
commit 6908173414
7 changed files with 245 additions and 178 deletions

View File

@@ -0,0 +1,10 @@
import React from 'react';
import CurrencyDeleteAlert from 'containers/Alerts/Currencies/CurrencyDeleteAlert';
export default function CurrenciesAlerts() {
return (
<div>
<CurrencyDeleteAlert name={'currency-delete'} />
</div>
);
}

View File

@@ -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) => (
<Menu>
<MenuItem
icon={<Icon icon="pen-18" />}
text={formatMessage({ id: 'edit_currency' })}
onClick={() => handleEditCurrency(currency)}
/>
<MenuItem
icon={<Icon icon="trash-16" iconSize={16} />}
text={formatMessage({ id: 'delete_currency' })}
onClick={() => onDeleteCurrency(currency)}
intent={Intent.DANGER}
/>
</Menu>
),
[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 }) => (
<Popover
content={actionMenuList(cell.row.original)}
position={Position.RIGHT_BOTTOM}
>
<Button icon={<Icon icon="more-h-16" iconSize={16} />} />
</Popover>
),
className: 'actions',
width: 50,
disableResizing: true,
},
],
[actionMenuList, formatMessage],
);
const handleDataTableFetchData = useCallback(
(...args) => {
saveInvoke(onFetchData, ...args);
},
[onFetchData],
);
// Handle delete currency.
const handleDeleteCurrency = ({ currency_code }) => {
openAlert('currency-delete', { currency_code: currency_code });
};
return (
<DataTable
columns={columns}
data={currenciesList}
loading={currenciesLoading}
onFetchData={handleDataTableFetchData}
data={currencies}
loading={isCurrenciesLoading}
progressBarLoading={isCurrenciesLoading}
TableLoadingRenderer={TableSkeletonRows}
noInitialFetch={true}
rowContextMenu={onRowContextMenu}
payload={{
onDeleteCurrency: handleDeleteCurrency,
onEditCurrency: handleEditCurrency,
}}
rowContextMenu={ActionMenuList}
{...tableProps}
/>
);
}
export default compose(
withRouter,
withDashboardActions,
withDialogActions,
withCurrencies(({ currenciesList, currenciesLoading }) => ({
currenciesList,
currenciesLoading,
})),
withAlertActions,
)(CurrenciesDataTable);

View File

@@ -1,97 +1,31 @@
import React, { useCallback, useState, useEffect } from 'react';
import { Alert, Intent } from '@blueprintjs/core';
import { useQuery } from 'react-query';
import {
FormattedMessage as T,
FormattedHTMLMessage,
useIntl,
} from 'react-intl';
import React, { useEffect } from 'react';
import { FormattedMessage as T, useIntl } from 'react-intl';
import { CurrenciesProvider } from './CurrenciesProvider';
import CurrenciesDataTable from './CurrenciesDataTable';
import AppToaster from 'components/AppToaster';
import CurrenciesAlerts from './CurrenciesAlerts';
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
import withCurrenciesActions from 'containers/Currencies/withCurrenciesActions';
import { compose } from 'utils';
// Currencies landing list page.
function CurrenciesList({
// #withCurrenciesActions
requestDeleteCurrency,
requestFetchCurrencies,
// #withDashboardActions
changePreferencesPageTitle,
}) {
const [deleteCurrencyState, setDeleteCurrencyState] = useState(false);
const { formatMessage } = useIntl();
const fetchCurrencies = useQuery(
'currencies-table',
() => requestFetchCurrencies(),
{ enabled: true },
);
useEffect(() => {
changePreferencesPageTitle(formatMessage({ id: 'currencies' }));
}, [changePreferencesPageTitle, formatMessage]);
const handleEditCurrency = useCallback(() => {}, []);
// Handle click and cancel/confirm currency delete
const handleDeleteCurrency = useCallback((currency) => {
setDeleteCurrencyState(currency);
}, [setDeleteCurrencyState]);
// handle cancel delete currency alert.
const handleCancelCurrencyDelete = () => {
setDeleteCurrencyState(false);
};
// Handle confirm Currency delete
const handleConfirmCurrencyDelete = useCallback(
(refetch) => {
requestDeleteCurrency(deleteCurrencyState.currency_code)
.then((response) => {
setDeleteCurrencyState(false);
AppToaster.show({
message: formatMessage({
id: 'the_currency_has_been_deleted_successfully',
}),
intent: Intent.SUCCESS,
});
})
.catch((errors) => {
setDeleteCurrencyState(false);
});
},
[deleteCurrencyState, requestDeleteCurrency, formatMessage],
);
return (
<>
<CurrenciesDataTable
onDeleteCurrency={handleDeleteCurrency}
onEditCurrency={handleEditCurrency}
/>
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'delete'} />}
intent={Intent.DANGER}
isOpen={deleteCurrencyState}
onCancel={handleCancelCurrencyDelete}
onConfirm={handleConfirmCurrencyDelete}
>
<p>
Once you delete this currency, you won't be able to restore it later. Are you sure you want to delete ?
</p>
</Alert>
</>
<CurrenciesProvider>
<CurrenciesDataTable />
<CurrenciesAlerts />
</CurrenciesProvider>
);
}
export default compose(
withDashboardActions,
withCurrenciesActions,
)(CurrenciesList);
export default compose(withDashboardActions)(CurrenciesList);

View File

@@ -0,0 +1,27 @@
import React, { createContext, useContext } from 'react';
import { useCurrencies } from 'hooks/query';
const CurrenciesContext = createContext();
/**
* currencies provider.
*/
function CurrenciesProvider({ ...props }) {
// fetches the currencies list.
const { data: currencies, isFetching: isCurrenciesLoading } = useCurrencies();
const state = {
currencies,
isCurrenciesLoading,
};
return (
<>
<CurrenciesContext.Provider value={state} {...props} />
</>
);
}
const useCurrenciesContext = () => useContext(CurrenciesContext);
export { CurrenciesProvider, useCurrenciesContext };

View File

@@ -0,0 +1,84 @@
import React, { useMemo } from 'react';
import {
Menu,
Popover,
Button,
Position,
MenuItem,
Intent,
} from '@blueprintjs/core';
import { useIntl } from 'react-intl';
import { Icon } from 'components';
import { safeCallback } from 'utils';
/**
* Row actions menu list.
*/
export function ActionMenuList({
row: { original },
payload: { onEditCurrency, onDeleteCurrency },
}) {
const { formatMessage } = useIntl();
return (
<Menu>
<MenuItem
icon={<Icon icon="pen-18" />}
text={formatMessage({ id: 'edit_currency' })}
onClick={safeCallback(onEditCurrency, original)}
/>
<MenuItem
icon={<Icon icon="trash-16" iconSize={16} />}
text={formatMessage({ id: 'delete_currency' })}
onClick={safeCallback(onDeleteCurrency, original)}
intent={Intent.DANGER}
/>
</Menu>
);
}
/**
* Actions cell.
*/
export const ActionsCell = (props) => {
return (
<Popover
position={Position.RIGHT_BOTTOM}
content={<ActionMenuList {...props} />}
>
<Button icon={<Icon icon="more-h-16" iconSize={16} />} />
</Popover>
);
};
export function useCurrenciesTableColumns() {
const { formatMessage } = useIntl();
return 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: ActionsCell,
className: 'actions',
width: 50,
disableResizing: true,
},
],
[formatMessage],
);
}