From b6fc06ea0c8dbc67411990accbc03db5de3a764e Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Sat, 30 Oct 2021 17:19:35 +0200 Subject: [PATCH] fix: BIG-166 cashflow new bank/cash account in cashflow service. --- .../CashFlowAccountsActionsBar.js | 10 ++- .../AccountDialog/AccountDialogContent.js | 1 - .../AccountDialog/AccountDialogFormContent.js | 12 +++- src/containers/Dialogs/AccountDialog/utils.js | 63 ++++++++++++++++--- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/containers/CashFlow/CashFlowAccounts/CashFlowAccountsActionsBar.js b/src/containers/CashFlow/CashFlowAccounts/CashFlowAccountsActionsBar.js index ccd2609b8..cb9e86b84 100644 --- a/src/containers/CashFlow/CashFlowAccounts/CashFlowAccountsActionsBar.js +++ b/src/containers/CashFlow/CashFlowAccounts/CashFlowAccountsActionsBar.js @@ -35,11 +35,17 @@ function CashFlowAccountsActionsBar({ }; // Handle add bank account. const handleAddBankAccount = () => { - openDialog('account-form', {}); + openDialog('account-form', { + action: 'NEW_ACCOUNT_DEFINED_TYPE', + accountType: 'cash', + }); }; // Handle add cash account. const handleAddCashAccount = () => { - openDialog('account-form', {}); + openDialog('account-form', { + action: 'NEW_ACCOUNT_DEFINED_TYPE', + accountType: 'bank', + }); }; // Handle inactive switch changing. const handleInactiveSwitchChange = (event) => { diff --git a/src/containers/Dialogs/AccountDialog/AccountDialogContent.js b/src/containers/Dialogs/AccountDialog/AccountDialogContent.js index 08bdbf73d..2c9856cba 100644 --- a/src/containers/Dialogs/AccountDialog/AccountDialogContent.js +++ b/src/containers/Dialogs/AccountDialog/AccountDialogContent.js @@ -12,7 +12,6 @@ export default function AccountDialogContent({ parentAccountId, accountType, }) { - return ( { form.setFieldValue('account_type', accountType.key); }} - disabled={action === 'edit' || action === 'new_child'} + disabled={ + action === 'edit' || + action === 'new_child' || + action === 'NEW_ACCOUNT_DEFINED_TYPE' + } popoverProps={{ minimal: true }} popoverFill={true} /> @@ -172,7 +176,11 @@ function AccountFormDialogFields({
- diff --git a/src/containers/Dialogs/AccountDialog/utils.js b/src/containers/Dialogs/AccountDialog/utils.js index 4868c0c74..7ce128577 100644 --- a/src/containers/Dialogs/AccountDialog/utils.js +++ b/src/containers/Dialogs/AccountDialog/utils.js @@ -1,4 +1,5 @@ import intl from 'react-intl-universal'; +import * as R from 'ramda'; export const transformApiErrors = (errors) => { const fields = {}; @@ -11,15 +12,57 @@ export const transformApiErrors = (errors) => { return fields; }; -export const transformAccountToForm = (account, { - action, - parentAccountId, - accountType -}) => { +/** + * Payload transformer in account edit mode. + */ +function transformEditMode(payload) { + return { + parent_account_id: payload.parentAccountId || '', + account_type: payload.accountType || '', + subaccount: true, + }; +} + +/** + * Payload transformer in new account with defined type. + */ +function transformNewAccountDefinedType(payload) { + return { + account_type: payload.accountType || '', + }; +} + +/** + * Merged the fetched account with transformed payload. + */ +const mergeWithAccount = R.curry((transformed, account) => { return { - parent_account_id: action === 'new_child' ? parentAccountId : '', - account_type: action === 'new_child'? accountType : '', - subaccount: action === 'new_child' ? true : false, ...account, - } -} \ No newline at end of file + ...transformed, + }; +}); + +/** + * Defined payload transformers. + */ +function getConditions() { + return [ + ['edit', transformEditMode], + ['NEW_ACCOUNT_DEFINED_TYPE', transformNewAccountDefinedType], + ]; +} + +/** + * Transformes the given payload to account form initial values. + */ +export const transformAccountToForm = (account, payload) => { + const conditions = getConditions(); + + const results = conditions.map((condition) => { + return [ + condition[0] === payload.action ? R.T : R.F, + mergeWithAccount(condition[1](payload)), + ]; + }); + return R.cond(results)(account); +};