import React, { useState } from 'react'; import { Button, Classes, FormGroup, InputGroup, Intent, TextArea, MenuItem, Checkbox } from '@blueprintjs/core'; import { Select } from '@blueprintjs/select'; import * as Yup from 'yup'; import { useFormik } from 'formik'; import { useIntl } from 'react-intl'; import { omit } from 'lodash'; import { compose } from 'utils'; import useAsync from 'hooks/async'; import Dialog from 'components/Dialog'; import AppToaster from 'components/AppToaster'; import DialogConnect from 'connectors/Dialog.connector'; import DialogReduxConnect from 'components/DialogReduxConnect'; import AccountFormDialogConnect from 'connectors/AccountFormDialog.connector'; function AccountFormDialog({ name, payload, isOpen, accountsTypes, accounts, fetchAccounts, fetchAccountTypes, closeDialog, submitAccount, fetchAccount, editAccount }) { const intl = useIntl(); const accountFormValidationSchema = Yup.object().shape({ name: Yup.string().required(intl.formatMessage({ id: 'required' })), code: Yup.number(intl.formatMessage({ id: 'field_name_must_be_number' })), account_type_id: Yup.string() .nullable() .required(intl.formatMessage({ id: 'required' })), description: Yup.string().trim() }); // Formik const formik = useFormik({ enableReinitialize: true, initialValues: { ...(payload.action === 'edit' && editAccount) }, validationSchema: accountFormValidationSchema, onSubmit: values => { const exclude = ['subaccount']; if (payload.action === 'edit') { editAccount({ payload: payload.id, form: { ...omit(values, exclude) } }).then(response => { closeDialog(name); AppToaster.show({ message: 'the_account_has_been_edited' }); }); } else { submitAccount({ form: { ...omit(values, exclude) } }).then(response => { closeDialog(name); AppToaster.show({ message: 'the_account_has_been_submit' }); }); } } }); const [state, setState] = useState({ loading: true, dialogActive: true, selectedAccountType: null, selectedSubaccount: null }); // Filters accounts types items. const filterAccountTypeItems = (query, accountType, _index, exactMatch) => { const normalizedTitle = accountType.name.toLowerCase(); const normalizedQuery = query.toLowerCase(); if (exactMatch) { return normalizedTitle === normalizedQuery; } else { return normalizedTitle.indexOf(normalizedQuery) >= 0; } }; // Account type item of select filed. const accountTypeItem = (item, { handleClick, modifiers, query }) => { return ; }; // Account item of select accounts field. const accountItem = (item, { handleClick, modifiers, query }) => { return ( ); }; // Filters accounts items. const filterAccountsPredicater = (query, account, _index, exactMatch) => { const normalizedTitle = account.name.toLowerCase(); const normalizedQuery = query.toLowerCase(); if (exactMatch) { return normalizedTitle === normalizedQuery; } else { return `${account.code} ${normalizedTitle}`.indexOf(normalizedQuery) >= 0; } }; const handleClose = () => { closeDialog(name); }; const fetchHook = useAsync(async () => { await Promise.all([ fetchAccounts(), fetchAccountTypes(), // Fetch the target in case edit mode. ...(payload.action === 'edit' ? [fetchAccount(payload.id)] : []) ]); }); const onDialogOpening = async () => { fetchHook.execute(); }; const onChangeAccountType = accountType => { setState({ ...state, selectedAccountType: accountType.name }); formik.setFieldValue('account_type_id', accountType.id); }; const onChangeSubaccount = account => { setState({ ...state, selectedSubaccount: account }); formik.setFieldValue('parent_account_id', account.id); }; const onDialogClosed = () => { formik.resetForm(); setState({ ...state, selectedSubaccount: null, selectedAccountType: null }); }; return (
{formik.values.subaccount && ( )}