diff --git a/client/src/components/Items/ItemForm.js b/client/src/components/Items/ItemForm.js
index dee577483..0ddcd0570 100644
--- a/client/src/components/Items/ItemForm.js
+++ b/client/src/components/Items/ItemForm.js
@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useState, useMemo, useCallback } from 'react';
import * as Yup from 'yup';
import { useFormik } from 'formik';
import {
@@ -7,7 +7,8 @@ import {
Intent,
InputGroup,
HTMLSelect,
- Button
+ Button,
+ Classes,
} from '@blueprintjs/core';
import { Row, Col } from 'react-grid-system';
import { Select } from '@blueprintjs/select';
@@ -15,6 +16,10 @@ import AppToaster from 'components/AppToaster';
import AccountsConnect from 'connectors/Accounts.connector';
import ItemsConnect from 'connectors/Items.connect';
import {compose} from 'utils';
+import ErrorMessage from 'components/ErrorMessage';
+import classNames from 'classnames';
+import Icon from 'components/Icon';
+
const ItemForm = ({
requestSubmitItem,
@@ -22,17 +27,16 @@ const ItemForm = ({
}) => {
const [selectedAccounts, setSelectedAccounts] = useState({});
- const ItemTypeDisplay = [
+ const ItemTypeDisplay = useMemo(() => ([
{ value: null, label: 'Select Item Type' },
{ value: 'service', label: 'Service' },
{ value: 'inventory', label: 'Inventory' },
{ value: 'non-inventory', label: 'Non-Inventory' }
- ];
+ ]), []);
+
const validationSchema = Yup.object().shape({
name: Yup.string().required(),
- type: Yup.string()
- .trim()
- .required(),
+ type: Yup.string().trim().required(),
sku: Yup.string().required(),
cost_price: Yup.number().required(),
sell_price: Yup.number().required(),
@@ -42,22 +46,39 @@ const ItemForm = ({
category_id: Yup.number().required(),
stock: Yup.string() || Yup.boolean()
});
+
+ const initialValues = useMemo(() => ({
+ name: '',
+ type: '',
+ sku: '',
+ cost_price: null,
+ sell_price: null,
+ cost_account_id: null,
+ sell_account_id: null,
+ inventory_account_id: null,
+ category_id: null,
+ note: '',
+ }), []);
+
const formik = useFormik({
enableReinitialize: true,
validationSchema: validationSchema,
- initialValues: {},
- onSubmit: values => {
- requestSubmitItem(values)
- .then(response => {
- AppToaster.show({
- message: 'The_Items_has_been_Submit'
- });
- })
- .catch(err => {
- alert(err.message);
+ initialValues: {
+ ...initialValues,
+ },
+ onSubmit: (values, { setSubmitting }) => {
+ requestSubmitItem(values).then((response) => {
+ AppToaster.show({
+ message: 'The_Items_has_been_Submit'
});
+ setSubmitting(false);
+ })
+ .catch((error) => {
+ setSubmitting(false);
+ });
}
});
+ const {errors, values, touched} = useMemo(() => formik, [formik]);
const accountItem = (item, { handleClick }) => (
@@ -73,131 +94,130 @@ const ItemForm = ({
}
};
- const onItemAccountSelect = filedName => {
- return account => {
+ const onItemAccountSelect = useCallback((filedName) => {
+ return (account) => {
setSelectedAccounts({
...selectedAccounts,
[filedName]: account
});
formik.setFieldValue(filedName, account.id);
};
- };
+ }, [formik, selectedAccounts]);
- const getSelectedAccountLabel = (fieldName, defaultLabel) => {
+ const getSelectedAccountLabel = useCallback((fieldName, defaultLabel) => {
return typeof selectedAccounts[fieldName] !== 'undefined'
- ? selectedAccounts[fieldName].name
- : defaultLabel;
- };
+ ? selectedAccounts[fieldName].name : defaultLabel;
+ }, [selectedAccounts]);
+
+ const requiredSpan = useMemo(() => (*), []);
+ const infoIcon = useMemo(() => (