import React, {useState} from 'react'; import * as Yup from 'yup'; import { useFormik } from 'formik'; import { FormGroup, MenuItem, Intent, InputGroup, Position, Button, TextArea, ControlGroup, } from '@blueprintjs/core'; import { DateInput } from "@blueprintjs/datetime"; import { Select } from '@blueprintjs/select'; import { useIntl } from 'react-intl'; import { momentFormatter } from 'utils'; import moment from 'moment'; import AppToaster from 'components/AppToaster'; export default function ExpenseForm({ accounts, editExpense, submitExpense, expenseDetails, currencies, }) { const intl = useIntl(); const [state, setState] = useState({ selectedExpenseAccount: null, selectedPaymentAccount: null, }); const validationSchema = Yup.object().shape({ date: Yup.date().required(), description: Yup.string().trim(), expense_account_id: Yup.number().required(), payment_account_id: Yup.number().required(), amount: Yup.number().required(), currency_code: Yup.string().required(), publish: Yup.boolean(), exchange_rate: Yup.number(), }); const formik = useFormik({ enableReinitialize: true, validationSchema: validationSchema, initialValues: { date: null, }, onSubmit: (values) => { submitExpense(values).then((response) => { AppToaster.show({ message: 'the_expense_has_been_submit', }); }).catch((error) => { }) } }); // Account item of select accounts field. const accountItem = (item, { handleClick, modifiers, query }) => { return () }; const onChangeAccount = () => { }; const onChangePaymentAccount = () => { }; const handleDateChange = (date) => { const formatted = moment(date).format('YYYY/MM/DD'); formik.setFieldValue('date', formatted); }; // 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 onExpenseAccountSelect = (account) => { setState({ ...state, selectedExpenseAccount: account }); formik.setFieldValue('expense_account_id', account.id); }; const onChangePaymentAccountSelect = (account) => { setState({ ...state, selectedPaymentAccount: account }); formik.setFieldValue('payment_account_id', account.id); }; const onAmountCurrencySelect = (currency) => { formik.setFieldValue('currency_code', currency.id); }; const paymentAccountLabel = state.selectedPaymentAccount ? state.selectedPaymentAccount.name : 'Select Payment Account'; const expenseAccountLabel = state.selectedExpenseAccount ? state.selectedExpenseAccount.name : 'Select Expense Account'; const handleClose = () => { }; return (