import React from 'react'; import { FastField, Field, ErrorMessage } from 'formik'; import { Classes, FormGroup, RadioGroup, Radio, InputGroup, } from '@blueprintjs/core'; import classNames from 'classnames'; import { FormattedMessage as T, If } from 'components'; import intl from 'react-intl-universal'; import { inputIntent, handleStringChange } from 'utils'; import { FieldRequiredHint, ListSelect } from 'components'; import { CLASSES } from 'common/classes'; import allocateLandedCostType from 'common/allocateLandedCostType'; import AllocateLandedCostFormBody from './AllocateLandedCostFormBody'; import { transactionsSelectShouldUpdate, allocateCostToEntries, resetAllocatedCostEntries, } from './utils'; import { useAllocateLandedConstDialogContext } from './AllocateLandedCostDialogProvider'; /** * Allocate landed cost form fields. */ export default function AllocateLandedCostFormFields() { // Allocated landed cost dialog. const { costTransactionEntries, landedCostTransactions } = useAllocateLandedConstDialogContext(); return (
{/*------------Transaction type -----------*/} {({ form: { values, setFieldValue }, field: { value }, meta: { error, touched }, }) => ( } labelInfo={} helperText={} intent={inputIntent({ error, touched })} inline={true} className={classNames(CLASSES.FILL, 'form-group--transaction_type')} > { const { items } = values; setFieldValue('transaction_type', type.value); setFieldValue('transaction_id', ''); setFieldValue('transaction_entry_id', ''); setFieldValue('amount', ''); setFieldValue('items', resetAllocatedCostEntries(items)); }} filterable={false} selectedItem={value} selectedItemProp={'value'} textProp={'name'} popoverProps={{ minimal: true }} /> )} {/*------------ Transaction -----------*/} {({ form, field: { value }, meta: { error, touched } }) => ( } labelInfo={} intent={inputIntent({ error, touched })} helperText={} className={classNames(CLASSES.FILL, 'form-group--transaction_id')} inline={true} > { const { items } = form.values; form.setFieldValue('transaction_id', id); form.setFieldValue('transaction_entry_id', ''); form.setFieldValue('amount', ''); form.setFieldValue('items', resetAllocatedCostEntries(items)); }} filterable={false} selectedItem={value} selectedItemProp={'id'} textProp={'name'} labelProp={'formatted_unallocated_cost_amount'} defaultText={intl.get( 'landed_cost.dialog.label_select_transaction', )} popoverProps={{ minimal: true }} /> )} {/*------------ Transaction line -----------*/} 0}> {({ form, field: { value }, meta: { error, touched } }) => ( } intent={inputIntent({ error, touched })} helperText={} className={classNames( CLASSES.FILL, 'form-group--transaction_entry_id', )} inline={true} > { const { id, unallocated_cost_amount: unallocatedAmount } = entry; const { items, allocation_method } = form.values; form.setFieldValue('amount', unallocatedAmount); form.setFieldValue('transaction_entry_id', id); form.setFieldValue( 'items', allocateCostToEntries( unallocatedAmount, allocation_method, items, ), ); }} filterable={false} selectedItem={value} selectedItemProp={'id'} textProp={'name'} labelProp={'formatted_unallocated_cost_amount'} defaultText={intl.get( 'landed_cost.dialog.label_select_transaction_entry', )} popoverProps={{ minimal: true }} /> )} {/*------------ Amount -----------*/} {({ form, field, meta: { error, touched } }) => ( } intent={inputIntent({ error, touched })} helperText={} className={'form-group--amount'} inline={true} > { const amount = e.target.value; const { allocation_method, items } = form.values; form.setFieldValue( 'items', allocateCostToEntries(amount, allocation_method, items), ); }} /> )} {/*------------ Allocation method -----------*/} {({ form, field: { value }, meta: { touched, error } }) => ( } labelInfo={} className={'form-group--allocation_method'} intent={inputIntent({ error, touched })} helperText={} inline={true} > { const { amount, items } = form.values; form.setFieldValue('allocation_method', _value); form.setFieldValue( 'items', allocateCostToEntries(amount, _value, items), ); })} selectedValue={value} inline={true} > } value="quantity" /> } value="value" /> )} {/*------------ Allocate Landed cost Table -----------*/}
); }