import React, { useMemo, useCallback, useState } from 'react'; import { InputGroup, FormGroup, Intent, Position, MenuItem, Classes, } from '@blueprintjs/core'; import { DateInput } from '@blueprintjs/datetime'; import { FormattedMessage as T } from 'react-intl'; import { Row, Col } from 'react-grid-system'; import moment from 'moment'; import { momentFormatter, compose, tansformDateValue } from 'utils'; import classNames from 'classnames'; import { ListSelect, ContactSelecetList, ErrorMessage, AccountsSelectList, FieldRequiredHint, Hint, } from 'components'; import withCurrencies from 'containers/Currencies/withCurrencies'; import withAccounts from 'containers/Accounts/withAccounts'; import withCustomers from 'containers/Customers/withCustomers'; function ExpenseFormHeader({ formik: { errors, touched, setFieldValue, getFieldProps, values }, currenciesList, accountsList, accountsTypes, customers, }) { const [selectedItems, setSelectedItems] = useState({}); const handleDateChange = useCallback( (date) => { const formatted = moment(date).format('YYYY-MM-DD'); setFieldValue('payment_date', formatted); }, [setFieldValue], ); const currencyCodeRenderer = useCallback((item, { handleClick }) => { return ( ); }, []); // Filters Currency code. const filterCurrencyCode = (query, currency, _index, exactMatch) => { const normalizedTitle = currency.currency_code.toLowerCase(); const normalizedQuery = query.toLowerCase(); if (exactMatch) { return normalizedTitle === normalizedQuery; } else { return ( `${currency.currency_code} ${normalizedTitle}`.indexOf( normalizedQuery, ) >= 0 ); } }; // Handles change account. const onChangeAccount = useCallback( (account) => { setFieldValue('payment_account_id', account.id); }, [setFieldValue], ); const onItemsSelect = useCallback( (filedName) => { return (filed) => { setSelectedItems({ ...selectedItems, [filedName]: filed, }); setFieldValue(filedName, filed.currency_code); }; }, [setFieldValue, selectedItems], ); // Filter payment accounts. const paymentAccounts = useMemo( () => accountsList.filter((a) => a?.type?.key === 'current_asset'), [accountsList], ); const CustomerRenderer = useCallback( (cutomer, { handleClick }) => ( ), [], ); // handle change customer const onChangeCustomer = useCallback( (filedName) => { return (customer) => { setFieldValue(filedName, customer.id); }; }, [setFieldValue], ); return (
} className={classNames('form-group--select-list', Classes.FILL)} labelInfo={} intent={errors.beneficiary && touched.beneficiary && Intent.DANGER} helperText={ } > } onContactSelected={onChangeCustomer('customer_id')} /> } className={classNames( 'form-group--payment_account', 'form-group--select-list', Classes.FILL, )} labelInfo={} intent={ errors.payment_account_id && touched.payment_account_id && Intent.DANGER } helperText={ } > } selectedAccountId={values.payment_account_id} /> } labelInfo={} className={classNames('form-group--select-list', Classes.FILL)} intent={ errors.payment_date && touched.payment_date && Intent.DANGER } helperText={ } > } className={classNames( 'form-group--select-list', 'form-group--currency', Classes.FILL, )} intent={ errors.currency_code && touched.currency_code && Intent.DANGER } helperText={ } > } itemRenderer={currencyCodeRenderer} itemPredicate={filterCurrencyCode} popoverProps={{ minimal: true }} onItemSelect={onItemsSelect('currency_code')} selectedItem={values.currency_code} selectedItemProp={'currency_code'} defaultText={} labelProp={'currency_code'} /> } className={classNames('form-group--ref_no', Classes.FILL)} intent={ errors.reference_no && touched.reference_no && Intent.DANGER } helperText={ } >
); } export default compose( withAccounts(({ accountsList, accountsTypes }) => ({ accountsList, accountsTypes, })), withCurrencies(({ currenciesList }) => ({ currenciesList, })), withCustomers(({ customers }) => ({ customers, })), )(ExpenseFormHeader);