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 { Icon, DataTable, Money, If, } 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; const { balance = null } = account; return balance ? ( ) : ( -- ); } function AccountNameAccessor(row) { return row.description ? ( {row.name} ) : ( row.name ); } function AccountsDataTable({ // #withDashboardActions accounts, 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) => ( handleNewParentAccount(account)} /> onInactiveAccount(account)} /> onActivateAccount(account)} /> 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.name', 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: 'balance', Cell: BalanceCell, width: 150, }, { id: 'actions', Header: '', Cell: ({ cell }) => (