import React, { useCallback, useState, useMemo, useEffect } from 'react'; import { Button, Popover, Menu, MenuItem, MenuDivider, Position, Classes, Tooltip, Intent, } from '@blueprintjs/core'; import { withRouter } from 'react-router'; import { FormattedMessage as T, useIntl } from 'react-intl'; import classnames from 'classnames'; import { Icon, DataTable, Money, If, Choose, } from 'components'; import { compose } from 'utils'; import { useUpdateEffect } from 'hooks'; import withDashboardActions from 'containers/Dashboard/withDashboardActions'; import withAccountsActions from 'containers/Accounts/withAccountsActions'; import withAccounts from 'containers/Accounts/withAccounts'; import withDialogActions from 'containers/Dialog/withDialogActions'; import withCurrentView from 'containers/Views/withCurrentView'; function NormalCell({ cell }) { const { formatMessage } = useIntl(); const account = cell.row.original; const normal = account?.type?.normal || ''; const arrowDirection = normal === 'credit' ? 'down' : 'up'; return ( ); } function BalanceCell({ cell }) { const account = cell.row.original; return (account.amount) ? ( ) : ( ); } function InactiveSemafro() { return ( } className={classnames( Classes.TOOLTIP_INDICATOR, 'bp3-popover-wrapper--inactive-semafro' )} position={Position.TOP} hoverOpenDelay={250}>
); } function AccountNameAccessor(row) { return ( <> {row.name} { row.name } ); } function AccountsDataTable({ // #withDashboardActions accountsTable, accountsLoading, // #withDialog. openDialog, currentViewId, // own properties onFetchData, onSelectedRowsChange, onDeleteAccount, onInactiveAccount, onActivateAccount, }) { const [isMounted, setIsMounted] = useState(false); const { formatMessage } = useIntl(); useEffect(() => { setIsMounted(false); }, [currentViewId]); useUpdateEffect(() => { if (!accountsLoading) { setIsMounted(true); } }, [accountsLoading, setIsMounted]); const handleEditAccount = useCallback( (account) => () => { openDialog('account-form', { action: 'edit', id: account.id }); }, [openDialog], ); const handleNewParentAccount = useCallback( (account) => { openDialog('account-form', { action: 'new_child', parentAccountId: account.id, accountTypeId: account.account_type_id, }); }, [openDialog], ); const actionMenuList = useCallback( (account) => ( } text={formatMessage({ id: 'view_details' })} /> } text={formatMessage({ id: 'edit_account' })} onClick={handleEditAccount(account)} /> } text={formatMessage({ id: 'new_child_account' })} onClick={() => handleNewParentAccount(account)} /> } onClick={() => onInactiveAccount(account)} /> } onClick={() => onActivateAccount(account)} /> } intent={Intent.DANGER} onClick={() => onDeleteAccount(account)} /> ), [ handleEditAccount, onDeleteAccount, onInactiveAccount, handleNewParentAccount, formatMessage, ], ); const rowContextMenu = (cell) => { return actionMenuList(cell.row.original); }; const columns = useMemo( () => [ { id: 'name', Header: formatMessage({ id: 'account_name' }), accessor: AccountNameAccessor, className: 'account_name', width: 220, }, { id: 'code', Header: formatMessage({ id: 'code' }), accessor: 'code', className: 'code', width: 125, }, { id: 'type', Header: formatMessage({ id: 'type' }), accessor: 'type.label', className: 'type', width: 140, }, { id: 'normal', Header: formatMessage({ id: 'normal' }), Cell: NormalCell, accessor: 'type.normal', className: 'normal', width: 115, }, { id: 'currency', Header: formatMessage({ id: 'currency' }), accessor: (row) => 'USD', width: 100, }, { id: 'balance', Header: formatMessage({ id: 'balance' }), accessor: 'amount', Cell: BalanceCell, width: 150, }, { id: 'actions', Header: '', Cell: ({ cell }) => (