fix: optimize item form performance.

This commit is contained in:
Ahmed Bouhuolia
2020-11-11 15:21:07 +02:00
parent 20e4c74422
commit 4cd4ff3530
7 changed files with 419 additions and 434 deletions

View File

@@ -292,7 +292,7 @@ export default compose(
withAccounts(({ accountsList }) => ({ withAccounts(({ accountsList }) => ({
accountsList, accountsList,
})), })),
withCustomers(({ customersItems }) => ({ withCustomers(({ customers }) => ({
customers: customersItems, customers,
})), })),
)(MakeJournalEntriesTable); )(MakeJournalEntriesTable);

View File

@@ -1,9 +1,7 @@
import React, { useMemo, useCallback, useEffect } from 'react'; import React, { useMemo, useCallback, useEffect } from 'react';
import * as Yup from 'yup'; import * as Yup from 'yup';
import { useFormik } from 'formik'; import { useFormik, Formik, Form } from 'formik';
import { import { Intent } from '@blueprintjs/core';
Intent
} from '@blueprintjs/core';
import { queryCache } from 'react-query'; import { queryCache } from 'react-query';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { pick, pickBy } from 'lodash'; import { pick, pickBy } from 'lodash';
@@ -41,7 +39,6 @@ const defaultInitialValues = {
purchasable: true, purchasable: true,
}; };
/** /**
* Item form. * Item form.
*/ */
@@ -88,14 +85,12 @@ function ItemForm({
.required() .required()
.label(formatMessage({ id: 'item_type_' })), .label(formatMessage({ id: 'item_type_' })),
sku: Yup.string().trim(), sku: Yup.string().trim(),
cost_price: Yup.number() cost_price: Yup.number().when(['purchasable'], {
.when(['purchasable'], {
is: true, is: true,
then: Yup.number().required(), then: Yup.number().required(),
otherwise: Yup.number().nullable(true), otherwise: Yup.number().nullable(true),
}), }),
sell_price: Yup.number() sell_price: Yup.number().when(['sellable'], {
.when(['sellable'], {
is: true, is: true,
then: Yup.number().required(), then: Yup.number().required(),
otherwise: Yup.number().nullable(true), otherwise: Yup.number().nullable(true),
@@ -145,21 +140,24 @@ function ItemForm({
); );
useEffect(() => { useEffect(() => {
(!isNewMode) !isNewMode
? changePageTitle(formatMessage({ id: 'edit_item_details' })) ? changePageTitle(formatMessage({ id: 'edit_item_details' }))
: changePageTitle(formatMessage({ id: 'new_item' })); : changePageTitle(formatMessage({ id: 'new_item' }));
}, [changePageTitle, isNewMode, formatMessage]); }, [changePageTitle, isNewMode, formatMessage]);
const transformApiErrors = (errors) => { const transformApiErrors = (errors) => {
const fields = {}; const fields = {};
if (errors.find(e => e.type === 'ITEM.NAME.ALREADY.EXISTS')) { if (errors.find((e) => e.type === 'ITEM.NAME.ALREADY.EXISTS')) {
fields.name = formatMessage({ id: 'the_name_used_before' }) fields.name = formatMessage({ id: 'the_name_used_before' });
} }
return fields; return fields;
} };
// Handles the form submit. // Handles the form submit.
const handleFormSubmit = (values, { setSubmitting, resetForm, setErrors }) => { const handleFormSubmit = (
values,
{ setSubmitting, resetForm, setErrors },
) => {
setSubmitting(true); setSubmitting(true);
const form = { ...values }; const form = { ...values };
@@ -167,9 +165,9 @@ function ItemForm({
AppToaster.show({ AppToaster.show({
message: formatMessage( message: formatMessage(
{ {
id: (isNewMode) ? id: isNewMode
'service_has_been_successful_created' : ? 'service_has_been_successful_created'
'the_item_has_been_successfully_edited', : 'the_item_has_been_successfully_edited',
}, },
{ {
number: itemId, number: itemId,
@@ -196,28 +194,13 @@ function ItemForm({
} }
}; };
const { // useEffect(() => {
getFieldProps, // if (values.item_type) {
setFieldValue, // changePageSubtitle(formatMessage({ id: values.item_type }));
values, // } else {
touched, // changePageSubtitle('');
errors, // }
handleSubmit, // }, [values.item_type, changePageSubtitle, formatMessage]);
isSubmitting,
} = useFormik({
enableReinitialize: true,
validationSchema: validationSchema,
initialValues,
onSubmit: handleFormSubmit
});
useEffect(() => {
if (values.item_type) {
changePageSubtitle(formatMessage({ id: values.item_type }));
} else {
changePageSubtitle('');
}
}, [values.item_type, changePageSubtitle, formatMessage]);
const initialAttachmentFiles = useMemo(() => { const initialAttachmentFiles = useMemo(() => {
return itemDetail && itemDetail.media return itemDetail && itemDetail.media
@@ -229,9 +212,12 @@ function ItemForm({
: []; : [];
}, [itemDetail]); }, [itemDetail]);
const handleDropFiles = useCallback((_files) => { const handleDropFiles = useCallback(
(_files) => {
setFiles(_files.filter((file) => file.uploaded === false)); setFiles(_files.filter((file) => file.uploaded === false));
}, [setFiles]); },
[setFiles],
);
const handleDeleteFile = useCallback( const handleDeleteFile = useCallback(
(_deletedFiles) => { (_deletedFiles) => {
@@ -250,39 +236,30 @@ function ItemForm({
return ( return (
<div class={classNames(CLASSES.PAGE_FORM_ITEM)}> <div class={classNames(CLASSES.PAGE_FORM_ITEM)}>
<form onSubmit={handleSubmit}> <Formik
enableReinitialize={true}
validationSchema={validationSchema}
initialValues={initialValues}
onSubmit={handleFormSubmit}
>
{({ isSubmitting }) => (
<Form>
<div class={classNames(CLASSES.PAGE_FORM_BODY)}> <div class={classNames(CLASSES.PAGE_FORM_BODY)}>
<ItemFormPrimarySection <ItemFormPrimarySection />
errors={errors} <ItemFormBody />
touched={touched} <ItemFormInventorySection />
values={values}
getFieldProps={getFieldProps}
setFieldValue={setFieldValue}
/>
<ItemFormBody
touched={touched}
errors={errors}
values={values}
getFieldProps={getFieldProps}
setFieldValue={setFieldValue}
/>
<ItemFormInventorySection
errors={errors}
touched={touched}
values={values}
setFieldValue={setFieldValue}
getFieldProps={getFieldProps}
/>
</div> </div>
<ItemFormFloatingActions <ItemFormFloatingActions
isSubmitting={isSubmitting} isSubmitting={isSubmitting}
itemId={itemId} itemId={itemId}
onCancelClick={handleCancelBtnClick} onCancelClick={handleCancelBtnClick}
/> />
</form> </Form>
)}
</Formik>
</div> </div>
); );
}; }
export default compose( export default compose(
withItemsActions, withItemsActions,

View File

@@ -1,15 +1,14 @@
import React from 'react'; import React from 'react';
import { FastField, ErrorMessage } from 'formik';
import { import {
FormGroup, FormGroup,
Intent, Intent,
InputGroup,
Classes, Classes,
Checkbox, Checkbox,
} from '@blueprintjs/core'; } from '@blueprintjs/core';
import { import {
AccountsSelectList, AccountsSelectList,
MoneyInputGroup, MoneyInputGroup,
ErrorMessage,
Col, Col,
Row, Row,
Hint, Hint,
@@ -18,74 +17,67 @@ import { FormattedMessage as T } from 'react-intl';
import classNames from 'classnames'; import classNames from 'classnames';
import withAccounts from 'containers/Accounts/withAccounts'; import withAccounts from 'containers/Accounts/withAccounts';
import { compose } from 'utils'; import { compose, inputIntent } from 'utils';
/** /**
* Item form body. * Item form body.
*/ */
function ItemFormBody({ function ItemFormBody({ accountsList }) {
getFieldProps,
touched,
errors,
values,
setFieldValue,
accountsList,
}) {
return ( return (
<div class="page-form__section page-form__section--selling-cost"> <div class="page-form__section page-form__section--selling-cost">
<Row> <Row>
<Col xs={6}> <Col xs={6}>
{/*------------- Sellable checkbox ------------- */} {/*------------- Purchasable checbox ------------- */}
<FastField name={'sellable'}>
{({ field, field: { value } }) => (
<FormGroup inline={true} className={'form-group--sellable'}> <FormGroup inline={true} className={'form-group--sellable'}>
<Checkbox <Checkbox
inline={true} inline={true}
label={ label={
<h3> <h3>
<T id={'i_purchase_this_item'} /> <T id={'i_sell_this_item'} />
</h3> </h3>
} }
checked={values.sellable} defaultChecked={value}
{...getFieldProps('sellable')} {...field}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*------------- Selling price ------------- */} {/*------------- Selling price ------------- */}
<FastField name={'sell_price'}>
{({ form, field, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'selling_price'} />} label={<T id={'selling_price'} />}
className={'form-group--item-selling-price'} className={'form-group--sell_price'}
intent={errors.sell_price && touched.sell_price && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={ helperText={<ErrorMessage name={'sell_price'} />}
<ErrorMessage {...{ errors, touched }} name="sell_price" />
}
inline={true} inline={true}
> >
<MoneyInputGroup <MoneyInputGroup
value={values.sell_price} value={value}
prefix={'$'} prefix={'$'}
onChange={(e, value) => {
setFieldValue('sell_price', value);
}}
inputGroupProps={{ inputGroupProps={{
medium: true, medium: true,
intent: // intent: error && touched/ && Intent.DANGER,
errors.sell_price && touched.sell_price && Intent.DANGER,
}} }}
disabled={!values.sellable} disabled={!form.values.sellable}
onChange={field.onChange}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*------------- Selling account ------------- */} {/*------------- Selling account ------------- */}
<FastField name={'sell_account_id'}>
{({ form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'account'} />} label={<T id={'account'} />}
labelInfo={<Hint />} labelInfo={<Hint />}
inline={true} inline={true}
intent={ intent={inputIntent({ error, touched })}
errors.sell_account_id && touched.sell_account_id && Intent.DANGER helperText={<ErrorMessage name="sell_account_id" />}
}
helperText={
<ErrorMessage {...{ errors, touched }} name="sell_account_id" />
}
className={classNames( className={classNames(
'form-group--sell-account', 'form-group--sell-account',
'form-group--select-list', 'form-group--select-list',
@@ -95,67 +87,69 @@ function ItemFormBody({
<AccountsSelectList <AccountsSelectList
accounts={accountsList} accounts={accountsList}
onAccountSelected={(account) => { onAccountSelected={(account) => {
setFieldValue('sell_account_id', account.id); form.setFieldValue('sell_account_id', account.id);
}} }}
defaultSelectText={<T id={'select_account'} />} defaultSelectText={<T id={'select_account'} />}
selectedAccountId={values.sell_account_id} selectedAccountId={value}
disabled={!values.sellable} disabled={!form.values.sellable}
filterByTypes={['income']} filterByTypes={['income']}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</Col> </Col>
<Col xs={6}> <Col xs={6}>
{/*------------- Purchasable checbox ------------- */} {/*------------- Sellable checkbox ------------- */}
<FastField name={'purchasable'}>
{({ field, field: { value }, meta: { error, touched } }) => (
<FormGroup inline={true} className={'form-group--purchasable'}> <FormGroup inline={true} className={'form-group--purchasable'}>
<Checkbox <Checkbox
inline={true} inline={true}
label={ label={
<h3> <h3>
<T id={'i_sell_this_item'} /> <T id={'i_purchase_this_item'} />
</h3> </h3>
} }
defaultChecked={values.purchasable} checked={value}
{...getFieldProps('purchasable')} {...field}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*------------- Cost price ------------- */} {/*------------- Cost price ------------- */}
<FastField name={'cost_price'}>
{({ field, form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'cost_price'} />} label={<T id={'cost_price'} />}
className={'form-group--item-cost-price'} className={'form-group--item-cost-price'}
intent={errors.cost_price && touched.cost_price && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={ helperText={<ErrorMessage name="cost_price" />}
<ErrorMessage {...{ errors, touched }} name="cost_price" />
}
inline={true} inline={true}
> >
<MoneyInputGroup <MoneyInputGroup
value={values.cost_price} value={value}
prefix={'$'} prefix={'$'}
onChange={(e, value) => {
setFieldValue('cost_price', value);
}}
inputGroupProps={{ inputGroupProps={{
medium: true, medium: true,
intent:
errors.cost_price && touched.cost_price && Intent.DANGER,
}} }}
disabled={!values.purchasable} disabled={!form.values.purchasable}
{...field}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*------------- Cost account ------------- */} {/*------------- Cost account ------------- */}
<FastField name={'cost_account_id'}>
{({ form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'account'} />} label={<T id={'account'} />}
labelInfo={<Hint />} labelInfo={<Hint />}
inline={true} inline={true}
intent={ intent={inputIntent({ error, touched })}
errors.cost_account_id && touched.cost_account_id && Intent.DANGER helperText={<ErrorMessage name="cost_account_id" />}
}
helperText={
<ErrorMessage {...{ errors, touched }} name="cost_account_id" />
}
className={classNames( className={classNames(
'form-group--cost-account', 'form-group--cost-account',
'form-group--select-list', 'form-group--select-list',
@@ -165,14 +159,16 @@ function ItemFormBody({
<AccountsSelectList <AccountsSelectList
accounts={accountsList} accounts={accountsList}
onAccountSelected={(account) => { onAccountSelected={(account) => {
setFieldValue('cost_account_id', account.id); form.setFieldValue('cost_account_id', account.id);
}} }}
defaultSelectText={<T id={'select_account'} />} defaultSelectText={<T id={'select_account'} />}
selectedAccountId={values.cost_account_id} selectedAccountId={value}
disabled={!values.purchasable} disabled={!form.values.purchasable}
filterByTypes={['cost_of_goods_sold']} filterByTypes={['cost_of_goods_sold']}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</Col> </Col>
</Row> </Row>
</div> </div>

View File

@@ -1,24 +1,15 @@
import React from 'react'; import React, { memo } from 'react';
import { import { Button, Intent, FormGroup, Checkbox } from '@blueprintjs/core';
Button,
Intent,
FormGroup,
Checkbox
} from '@blueprintjs/core';
import { FormattedMessage as T } from 'react-intl'; import { FormattedMessage as T } from 'react-intl';
import { saveInvoke } from 'utils'; import { saveInvoke } from 'utils';
import classNames from 'classnames'; import classNames from 'classnames';
import { ErrorMessage, FastField } from 'formik';
import { CLASSES } from 'common/classes'; import { CLASSES } from 'common/classes';
/** /**
* Item form floating actions. * Item form floating actions.
*/ */
export default function ItemFormFloatingActions({ export default function ItemFormFloatingActions({ isSubmitting, itemId, onCancelClick }) {
isSubmitting,
itemId,
onCancelClick
}) {
const handleCancelBtnClick = (event) => { const handleCancelBtnClick = (event) => {
saveInvoke(onCancelClick, event.currentTarget.value); saveInvoke(onCancelClick, event.currentTarget.value);
}; };
@@ -37,18 +28,24 @@ export default function ItemFormFloatingActions({
</Button> </Button>
{/*----------- Active ----------*/} {/*----------- Active ----------*/}
<FormGroup <FastField name={'type'}>
label={' '} {({ field, field: { value } }) => (
inline={true} <FormGroup label={' '} inline={true} className={'form-group--active'}>
className={'form-group--active'}
>
<Checkbox <Checkbox
inline={true} inline={true}
label={<T id={'active'} />} label={<T id={'active'} />}
// defaultChecked={values.active} defaultChecked={value}
// {...getFieldProps('active')} {...field}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</div> </div>
); );
} }
// function areEqual(prevProps, nextProps) {
// return prevProps.isSubmitting === nextProps.isSubmitting;
// }
// export default memo(ItemFormFloatingActions, areEqual);

View File

@@ -1,30 +1,18 @@
import React from 'react'; import React from 'react';
import { import { Field, FastField, ErrorMessage } from 'formik';
FormGroup, import { FormGroup, Intent, InputGroup, Position } from '@blueprintjs/core';
Intent,
InputGroup,
Position,
} from '@blueprintjs/core';
import { DateInput } from '@blueprintjs/datetime'; import { DateInput } from '@blueprintjs/datetime';
import { AccountsSelectList, ErrorMessage, Col, Row, Hint } from 'components'; import { AccountsSelectList, Col, Row, Hint } from 'components';
import { CLASSES } from 'common/classes'; import { CLASSES } from 'common/classes';
import { FormattedMessage as T } from 'react-intl'; import { FormattedMessage as T } from 'react-intl';
import classNames from 'classnames'; import classNames from 'classnames';
import withAccounts from 'containers/Accounts/withAccounts'; import withAccounts from 'containers/Accounts/withAccounts';
import { compose, tansformDateValue, momentFormatter } from 'utils'; import { compose, tansformDateValue, momentFormatter, inputIntent } from 'utils';
/** /**
* Item form inventory sections. * Item form inventory sections.
*/ */
function ItemFormInventorySection({ function ItemFormInventorySection({ accountsList }) {
errors,
touched,
setFieldValue,
values,
getFieldProps,
accountsList,
}) {
return ( return (
<div class="page-form__section page-form__section--inventory"> <div class="page-form__section page-form__section--inventory">
<h3> <h3>
@@ -34,20 +22,13 @@ function ItemFormInventorySection({
<Row> <Row>
<Col xs={6}> <Col xs={6}>
{/*------------- Inventory account ------------- */} {/*------------- Inventory account ------------- */}
<FastField name={'inventory_account_id'}>
{({ form, field: { value }, meta: { touched, error } }) => (
<FormGroup <FormGroup
label={<T id={'inventory_account'} />} label={<T id={'inventory_account'} />}
inline={true} inline={true}
intent={ intent={inputIntent({ error, touched })}
errors.inventory_account_id && helperText={<ErrorMessage name="inventory_account_id" />}
touched.inventory_account_id &&
Intent.DANGER
}
helperText={
<ErrorMessage
{...{ errors, touched }}
name="inventory_account_id"
/>
}
className={classNames( className={classNames(
'form-group--item-inventory_account', 'form-group--item-inventory_account',
'form-group--select-list', 'form-group--select-list',
@@ -57,26 +38,34 @@ function ItemFormInventorySection({
<AccountsSelectList <AccountsSelectList
accounts={accountsList} accounts={accountsList}
onAccountSelected={(account) => { onAccountSelected={(account) => {
setFieldValue('inventory_account_id', account.id); form.setFieldValue('inventory_account_id', account.id);
}} }}
defaultSelectText={<T id={'select_account'} />} defaultSelectText={<T id={'select_account'} />}
selectedAccountId={values.inventory_account_id} selectedAccountId={value}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
<FastField name={'opening_quantity'}>
{({ field, field: { value }, meta: { touched, error } }) => (
<FormGroup <FormGroup
label={<T id={'opening_quantity'} />} label={<T id={'opening_quantity'} />}
labelInfo={<Hint />} labelInfo={<Hint />}
className={'form-group--opening_quantity'} className={'form-group--opening_quantity'}
intent={inputIntent({ error, touched })}
inline={true} inline={true}
> >
<InputGroup <InputGroup
medium={true} medium={true}
intent={errors.opening_quantity && Intent.DANGER} {...field}
{...getFieldProps('opening_quantity')}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
<FastField name={'opening_date'}>
{({ form, field: { value }, meta: { touched, error } }) => (
<FormGroup <FormGroup
label={<T id={'opening_date'} />} label={<T id={'opening_date'} />}
labelInfo={<Hint />} labelInfo={<Hint />}
@@ -85,32 +74,39 @@ function ItemFormInventorySection({
'form-group--opening_date', 'form-group--opening_date',
CLASSES.FILL, CLASSES.FILL,
)} )}
intent={inputIntent({ error, touched })}
inline={true} inline={true}
> >
<DateInput <DateInput
{...momentFormatter('YYYY/MM/DD')} {...momentFormatter('YYYY/MM/DD')}
value={tansformDateValue(values.payment_date)} value={tansformDateValue(value)}
onChange={(value) => { onChange={(value) => {
setFieldValue('opening_date', value); form.setFieldValue('opening_date', value);
}} }}
popoverProps={{ position: Position.BOTTOM, minimal: true }} popoverProps={{ position: Position.BOTTOM, minimal: true }}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</Col> </Col>
<Col xs={6}> <Col xs={6}>
<FastField name={'opening_average_rate'}>
{({ field, field: { value }, meta: { touched, error } }) => (
<FormGroup <FormGroup
label={'Opening average rate'} label={'Opening average rate'}
labelInfo={<Hint />} labelInfo={<Hint />}
className={'form-group--opening_average_rate'} className={'form-group--opening_average_rate'}
intent={inputIntent({ error, touched })}
inline={true} inline={true}
> >
<InputGroup <InputGroup
medium={true} medium={true}
intent={errors.opening_average_rate && Intent.DANGER} {...field}
{...getFieldProps('opening_average_rate')}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</Col> </Col>
</Row> </Row>
</div> </div>

View File

@@ -7,12 +7,11 @@ import {
Classes, Classes,
Radio, Radio,
Position, Position,
Tooltip,
} from '@blueprintjs/core'; } from '@blueprintjs/core';
import { FormattedMessage as T } from 'react-intl'; import { FormattedMessage as T } from 'react-intl';
import { ErrorMessage, FastField } from 'formik';
import { import {
CategoriesSelectList, CategoriesSelectList,
ErrorMessage,
Hint, Hint,
Col, Col,
Row, Row,
@@ -24,111 +23,122 @@ import { CLASSES } from 'common/classes';
import withItemCategories from 'containers/Items/withItemCategories'; import withItemCategories from 'containers/Items/withItemCategories';
import withAccounts from 'containers/Accounts/withAccounts'; import withAccounts from 'containers/Accounts/withAccounts';
import { compose, handleStringChange } from 'utils'; import { compose, handleStringChange, inputIntent } from 'utils';
/** /**
* Item form primary section. * Item form primary section.
*/ */
function ItemFormPrimarySection({ function ItemFormPrimarySection({
getFieldProps,
setFieldValue,
errors,
touched,
values,
// #withItemCategories // #withItemCategories
categoriesList, categoriesList,
}) { }) {
const itemTypeHintContent = ( const itemTypeHintContent = (
<> <>
<div class="mb1"><strong>{'Service: '}</strong>{'Services that you provide to customers. '}</div> <div class="mb1">
<div class="mb1"><strong>{'Inventory: '}</strong>{'Products you buy and/or sell and that you track quantities of.'}</div> <strong>{'Service: '}</strong>
<div class="mb1"><strong>{'Non-Inventory: '}</strong>{'Products you buy and/or sell but dont need to (or cant) track quantities of, for example, nuts and bolts used in an installation.'}</div> {'Services that you provide to customers. '}
</>); </div>
<div class="mb1">
<strong>{'Inventory: '}</strong>
{'Products you buy and/or sell and that you track quantities of.'}
</div>
<div class="mb1">
<strong>{'Non-Inventory: '}</strong>
{
'Products you buy and/or sell but dont need to (or cant) track quantities of, for example, nuts and bolts used in an installation.'
}
</div>
</>
);
return ( return (
<div className={classNames(CLASSES.PAGE_FORM_HEADER_PRIMARY)}> <div className={classNames(CLASSES.PAGE_FORM_HEADER_PRIMARY)}>
<Row> <Row>
<Col xs={7}> <Col xs={7}>
{/*----------- Item type ----------*/} {/*----------- Item type ----------*/}
<FastField name={'type'}>
{({ form, field: { value }, meta: { touched, error } }) => (
<FormGroup <FormGroup
medium={true} medium={true}
label={<T id={'item_type'} />} label={<T id={'item_type'} />}
labelInfo={ labelInfo={
<span> <span>
<FieldRequiredHint /> <FieldRequiredHint />
<Hint content={itemTypeHintContent} position={Position.BOTTOM_LEFT} /> <Hint
content={itemTypeHintContent}
position={Position.BOTTOM_LEFT}
/>
</span> </span>
} }
className={'form-group--item-type'} className={'form-group--item-type'}
intent={errors.type && touched.type && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={<ErrorMessage {...{ errors, touched }} name="type" />} helperText={<ErrorMessage name="item_type" />}
inline={true} inline={true}
> >
<RadioGroup <RadioGroup
inline={true} inline={true}
onChange={handleStringChange((value) => { onChange={handleStringChange((_value) => {
setFieldValue('type', value); form.setFieldValue('type', _value);
})} })}
selectedValue={values.type} selectedValue={value}
> >
<Radio <Radio label={<T id={'service'} />} value="service" />
label={<T id={'service'} />} <Radio label={<T id={'inventory'} />} value="inventory" />
value="service"
/>
<Radio
label={<T id={'inventory'} />}
value="inventory"
/>
<Radio <Radio
label={<T id={'non_inventory'} />} label={<T id={'non_inventory'} />}
value="non-inventory" value="non-inventory"
/> />
</RadioGroup> </RadioGroup>
</FormGroup> </FormGroup>
)}
</FastField>
{/*----------- Item name ----------*/} {/*----------- Item name ----------*/}
<FastField name={'name'}>
{({ field, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'item_name'} />} label={<T id={'item_name'} />}
labelInfo={<FieldRequiredHint />} labelInfo={<FieldRequiredHint />}
className={'form-group--item-name'} className={'form-group--item-name'}
intent={errors.name && touched.name && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={<ErrorMessage {...{ errors, touched }} name="name" />} helperText={<ErrorMessage name={'name'} />}
inline={true} inline={true}
> >
<InputGroup <InputGroup
medium={true} medium={true}
intent={errors.name && touched.name && Intent.DANGER} {...field}
{...getFieldProps('name')}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*----------- SKU ----------*/} {/*----------- SKU ----------*/}
<FastField name={'code'}>
{({ field, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'item_code'} />} label={<T id={'item_code'} />}
labelInfo={<Hint />} labelInfo={<Hint />}
className={'form-group--item-sku'} className={' -group--item-sku'}
intent={errors.sku && touched.sku && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={<ErrorMessage {...{ errors, touched }} name="sku" />} helperText={<ErrorMessage name={'code'} />}
inline={true} inline={true}
> >
<InputGroup <InputGroup
medium={true} medium={true}
intent={errors.sku && touched.sku && Intent.DANGER} {...field}
{...getFieldProps('sku')}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
{/*----------- Item category ----------*/} {/*----------- Item category ----------*/}
<FastField name={'type'}>
{({ form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'category'} />} label={<T id={'category'} />}
labelInfo={<Hint />}
inline={true} inline={true}
intent={errors.category_id && touched.category_id && Intent.DANGER} intent={inputIntent({ error, touched })}
helperText={ helperText={<ErrorMessage name="category_id" />}
<ErrorMessage {...{ errors, touched }} name="category" />
}
className={classNames( className={classNames(
'form-group--select-list', 'form-group--select-list',
'form-group--category', 'form-group--category',
@@ -137,13 +147,15 @@ function ItemFormPrimarySection({
> >
<CategoriesSelectList <CategoriesSelectList
categoriesList={categoriesList} categoriesList={categoriesList}
selecetedCategoryId={values.category_id} selecetedCategoryId={value}
onCategorySelected={(category) => { onCategorySelected={(category) => {
setFieldValue('category_id', category.id); form.setFieldValue('category_id', category.id);
}} }}
popoverProps={{ minimal: true }} popoverProps={{ minimal: true }}
/> />
</FormGroup> </FormGroup>
)}
</FastField>
</Col> </Col>
<Col xs={3}> <Col xs={3}>

View File

@@ -1,5 +1,8 @@
import moment from 'moment'; import moment from 'moment';
import _ from 'lodash'; import _ from 'lodash';
import {
Intent,
} from '@blueprintjs/core';
import Currency from 'js-money/lib/currency'; import Currency from 'js-money/lib/currency';
import PProgress from 'p-progress'; import PProgress from 'p-progress';
import accounting from 'accounting'; import accounting from 'accounting';
@@ -285,3 +288,7 @@ export const transformToForm = (obj, emptyInitialValues) => {
(val, key) => val !== null && Object.keys(emptyInitialValues).includes(key), (val, key) => val !== null && Object.keys(emptyInitialValues).includes(key),
) )
} }
export function inputIntent({ error, touched }){
return error && touched ? Intent.DANGER : '';
}