This commit is contained in:
elforjani3
2020-11-10 13:33:30 +02:00
7 changed files with 57 additions and 37 deletions

View File

@@ -2,6 +2,7 @@ import React, { useCallback, useState, useEffect, useMemo } from 'react';
import { MenuItem, Button } from '@blueprintjs/core'; import { MenuItem, Button } from '@blueprintjs/core';
import { Select } from '@blueprintjs/select'; import { Select } from '@blueprintjs/select';
import { FormattedMessage as T } from 'react-intl'; import { FormattedMessage as T } from 'react-intl';
import { isEmpty } from 'lodash';
export default function AccountsSelectList({ export default function AccountsSelectList({
accounts, accounts,
@@ -10,11 +11,36 @@ export default function AccountsSelectList({
defaultSelectText = 'Select account', defaultSelectText = 'Select account',
onAccountSelected, onAccountSelected,
disabled = false, disabled = false,
filterByRootTypes = [],
filterByTypes = [],
filterByNormal
}) { }) {
// Filters accounts based on filter props.
const filteredAccounts = useMemo(() => {
let filteredAccounts = [...accounts];
if (!isEmpty(filterByRootTypes)) {
filteredAccounts = filteredAccounts.filter(
(account) => filterByRootTypes.indexOf(account.type.root_type) !== -1,
);
}
if (!isEmpty(filterByTypes)) {
filteredAccounts = filteredAccounts.filter(
(account) => filterByTypes.indexOf(account.type.key) !== -1,
);
}
if (!isEmpty(filterByNormal)) {
filteredAccounts = filteredAccounts.filter(
(account) => filterByTypes.indexOf(account.type.normal) === filterByNormal,
);
}
return filteredAccounts;
}, [accounts, filterByRootTypes, filterByTypes, filterByNormal]);
// Find initial account object to set it as default account in initial render. // Find initial account object to set it as default account in initial render.
const initialAccount = useMemo( const initialAccount = useMemo(
() => accounts.find((a) => a.id === initialAccountId), () => filteredAccounts.find((a) => a.id === initialAccountId),
[initialAccountId], [initialAccountId, filteredAccounts],
); );
const [selectedAccount, setSelectedAccount] = useState( const [selectedAccount, setSelectedAccount] = useState(
@@ -24,11 +50,11 @@ export default function AccountsSelectList({
useEffect(() => { useEffect(() => {
if (typeof selectedAccountId !== 'undefined') { if (typeof selectedAccountId !== 'undefined') {
const account = selectedAccountId const account = selectedAccountId
? accounts.find((a) => a.id === selectedAccountId) ? filteredAccounts.find((a) => a.id === selectedAccountId)
: null; : null;
setSelectedAccount(account); setSelectedAccount(account);
} }
}, [selectedAccountId, accounts, setSelectedAccount]); }, [selectedAccountId, filteredAccounts, setSelectedAccount]);
// Account item of select accounts field. // Account item of select accounts field.
const accountItem = useCallback((item, { handleClick, modifiers, query }) => { const accountItem = useCallback((item, { handleClick, modifiers, query }) => {
@@ -69,7 +95,7 @@ export default function AccountsSelectList({
return ( return (
<Select <Select
items={accounts} items={filteredAccounts}
noResults={<MenuItem disabled={true} text={<T id={'no_accounts'} />} />} noResults={<MenuItem disabled={true} text={<T id={'no_accounts'} />} />}
itemRenderer={accountItem} itemRenderer={accountItem}
itemPredicate={filterAccountsPredicater} itemPredicate={filterAccountsPredicater}

View File

@@ -5,10 +5,20 @@ import { FormGroup, Classes, Intent } from '@blueprintjs/core';
// Account cell renderer. // Account cell renderer.
const AccountCellRenderer = ({ const AccountCellRenderer = ({
column: { id, accountsDataProp }, column: {
id,
accountsDataProp,
filterAccountsByRootType,
filterAccountsByTypes,
},
row: { index, original }, row: { index, original },
cell: { value: initialValue }, cell: { value: initialValue },
payload: { accounts: defaultAccounts, updateData, errors, ...restProps }, payload: {
accounts: defaultAccounts,
updateData,
errors,
...restPayloadProps
},
}) => { }) => {
const handleAccountSelected = useCallback( const handleAccountSelected = useCallback(
(account) => { (account) => {
@@ -19,9 +29,10 @@ const AccountCellRenderer = ({
const error = errors?.[index]?.[id]; const error = errors?.[index]?.[id];
const accounts = useMemo( const accounts = useMemo(
() => restProps[accountsDataProp] || defaultAccounts, () => restPayloadProps[accountsDataProp] || defaultAccounts,
[restProps, defaultAccounts, accountsDataProp], [restPayloadProps, defaultAccounts, accountsDataProp],
); );
return ( return (
<FormGroup <FormGroup
intent={error ? Intent.DANGER : null} intent={error ? Intent.DANGER : null}
@@ -35,6 +46,8 @@ const AccountCellRenderer = ({
accounts={accounts} accounts={accounts}
onAccountSelected={handleAccountSelected} onAccountSelected={handleAccountSelected}
selectedAccountId={initialValue} selectedAccountId={initialValue}
filterByRootTypes={filterAccountsByRootType}
filterByTypes={filterAccountsByTypes}
/> />
</FormGroup> </FormGroup>
); );

View File

@@ -63,12 +63,6 @@ function ExpenseFormHeader({
[setFieldValue, selectedItems], [setFieldValue, selectedItems],
); );
// Filter payment accounts.
const paymentAccounts = useMemo(
() => accountsList.filter((a) => a?.type?.key === 'current_asset'),
[accountsList],
);
// handle change customer // handle change customer
const onChangeCustomer = useCallback( const onChangeCustomer = useCallback(
(filedName) => { (filedName) => {
@@ -126,10 +120,11 @@ function ExpenseFormHeader({
} }
> >
<AccountsSelectList <AccountsSelectList
accounts={paymentAccounts} accounts={accountsList}
onAccountSelected={onChangeAccount} onAccountSelected={onChangeAccount}
defaultSelectText={<T id={'select_payment_account'} />} defaultSelectText={<T id={'select_payment_account'} />}
selectedAccountId={values.payment_account_id} selectedAccountId={values.payment_account_id}
filterByTypes={['current_asset']}
/> />
</FormGroup> </FormGroup>
</Col> </Col>

View File

@@ -127,7 +127,7 @@ function ExpenseTable({
disableSortBy: true, disableSortBy: true,
disableResizing: true, disableResizing: true,
width: 250, width: 250,
accountsDataProp: 'expenseAccounts', filterAccountsByRootType: ['expense'],
}, },
{ {
Header: formatMessage({ id: 'amount_currency' }, { currency: 'USD' }), Header: formatMessage({ id: 'amount_currency' }, { currency: 'USD' }),
@@ -220,12 +220,6 @@ function ExpenseTable({
[rows], [rows],
); );
// Filter expense accounts.
const expenseAccounts = useMemo(
() => accountsList.filter((a) => a?.type?.root_type === 'expense'),
[accountsList],
);
return ( return (
<div className={'dashboard__insider--expense-form__table'}> <div className={'dashboard__insider--expense-form__table'}>
<DataTable <DataTable
@@ -235,7 +229,6 @@ function ExpenseTable({
sticky={true} sticky={true}
payload={{ payload={{
accounts: accountsList, accounts: accountsList,
expenseAccounts,
errors: errors.categories || [], errors: errors.categories || [],
updateData: handleUpdateData, updateData: handleUpdateData,
removeRow: handleRemoveRow, removeRow: handleRemoveRow,

View File

@@ -104,6 +104,7 @@ function ItemFormBody({
defaultSelectText={<T id={'select_account'} />} defaultSelectText={<T id={'select_account'} />}
selectedAccountId={values.sell_account_id} selectedAccountId={values.sell_account_id}
disabled={!values.sellable} disabled={!values.sellable}
filterByTypes={['income']}
/> />
</FormGroup> </FormGroup>
@@ -173,6 +174,7 @@ function ItemFormBody({
defaultSelectText={<T id={'select_account'} />} defaultSelectText={<T id={'select_account'} />}
selectedAccountId={values.cost_account_id} selectedAccountId={values.cost_account_id}
disabled={!values.purchasable} disabled={!values.purchasable}
filterByTypes={['cost_of_goods_sold']}
/> />
</FormGroup> </FormGroup>
</Col> </Col>

View File

@@ -76,11 +76,6 @@ function PaymentMadeFormHeader({
}, },
[setFieldValue], [setFieldValue],
); );
// Filter Payment accounts.
const paymentAccounts = useMemo(
() => accountsList.filter((a) => a?.type?.key === 'current_asset'),
[accountsList],
);
const handleReceiveFullAmountClick = () => { const handleReceiveFullAmountClick = () => {
setFieldValue('full_amount', payableFullAmount); setFieldValue('full_amount', payableFullAmount);
@@ -208,11 +203,12 @@ function PaymentMadeFormHeader({
} }
> >
<AccountsSelectList <AccountsSelectList
accounts={paymentAccounts} accounts={accountsList}
labelInfo={<FieldRequiredHint />} labelInfo={<FieldRequiredHint />}
onAccountSelected={onChangeSelect('payment_account_id')} onAccountSelected={onChangeSelect('payment_account_id')}
defaultSelectText={<T id={'select_payment_account'} />} defaultSelectText={<T id={'select_payment_account'} />}
selectedAccountId={values.payment_account_id} selectedAccountId={values.payment_account_id}
filterByTypes={['current_asset']}
/> />
</FormGroup> </FormGroup>

View File

@@ -64,12 +64,6 @@ function PaymentReceiveFormHeader({
[setFieldValue], [setFieldValue],
); );
// Filter deposit accounts.
const depositAccounts = useMemo(
() => accountsList.filter((a) => a?.type?.key === 'current_asset'),
[accountsList],
);
const triggerFullAmountChanged = (value) => { const triggerFullAmountChanged = (value) => {
onFullAmountChanged && onFullAmountChanged(value); onFullAmountChanged && onFullAmountChanged(value);
}; };
@@ -210,11 +204,12 @@ function PaymentReceiveFormHeader({
} }
> >
<AccountsSelectList <AccountsSelectList
accounts={depositAccounts} accounts={accountsList}
labelInfo={<FieldRequiredHint />} labelInfo={<FieldRequiredHint />}
onAccountSelected={onChangeSelect('deposit_account_id')} onAccountSelected={onChangeSelect('deposit_account_id')}
defaultSelectText={<T id={'select_deposit_account'} />} defaultSelectText={<T id={'select_deposit_account'} />}
selectedAccountId={values.deposit_account_id} selectedAccountId={values.deposit_account_id}
filterByTypes={['current_asset']}
/> />
</FormGroup> </FormGroup>