diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-18 20:32:41 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-18 21:57:43 +0300 |
commit | 746c0191380aef770f711bcfebc3cbd919c61b7d (patch) | |
tree | e2e978bbf446f0686a1f21fc272e8c6cc3430990 /src/containers | |
parent | 1027bb6565b70c0bb2008f034c01db5605c6d129 (diff) | |
download | commercel-ui-746c0191380aef770f711bcfebc3cbd919c61b7d.tar.gz |
feat: improve WaybillForm
Diffstat (limited to 'src/containers')
-rw-r--r-- | src/containers/ProductForm.tsx | 1 | ||||
-rw-r--r-- | src/containers/WaybillForm.tsx | 84 |
2 files changed, 56 insertions, 29 deletions
diff --git a/src/containers/ProductForm.tsx b/src/containers/ProductForm.tsx index 060fbbb..f458372 100644 --- a/src/containers/ProductForm.tsx +++ b/src/containers/ProductForm.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { Form } from 'formik'; import Input from '../components/Input'; +import DatePicker from '../components/DatePicker'; const ProductForm: React.FC = () => { diff --git a/src/containers/WaybillForm.tsx b/src/containers/WaybillForm.tsx index bd326d2..a6a37e2 100644 --- a/src/containers/WaybillForm.tsx +++ b/src/containers/WaybillForm.tsx @@ -1,55 +1,81 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Form, FormikProps } from 'formik'; import _ from 'lodash'; +import moment from 'moment'; import Input from '../components/Input'; import Button from '../components/Button'; import Select from '../components/Select'; import Paper from '../components/Paper'; -import useOptions from '../hooks/useOptions'; +import hooks from '../hooks/useAPIClient'; + + +const mapper = (item: any) => ({ key: item._id, label: item.name }); const WaybillForm: React.FC<FormikProps> = ({ setFieldValue, values }) => { - const [recordsNumber, setRecordsNumber] = useState<number>(values.records.length); + const { data: products } = hooks.products.useList(); + const { data: contractors } = hooks.contractors.useList(); - const handleAddRecord = () => setRecordsNumber(v => v + 1); + if (!values.date) setFieldValue('date', moment().format('YYYY-MM-DD')); + if (!values.contractorId && contractors) setFieldValue('contractorId', contractors[0]._id); - const productInitFields = _.times(recordsNumber).map(index => `records.${index}.productId`); + const handleAddRecord = () => setFieldValue('records', [...values.records, { + productId: _.map(products, '_id').reduce((acc, id) => { + return acc || (!_.map(values.records, 'productId').includes(id) && id); + }, false), + price: '', + quantity: 1, + }]); - const contractorOptions = useOptions('contractors', ['contractorId'], values, setFieldValue); - const productOptions = useOptions('products', productInitFields, values, setFieldValue); + const handleRemoveRecord = (index: number) => () => { + const records = [...values.records]; + records.splice(index, 1); + setFieldValue('records', records); + }; return ( <Form id="form"> <Select name="contractorId" label="Контрагент" - options={contractorOptions} - /> - <Select - name="operation" - label="Операция" - options={[ - { key: 'in', label: 'Приход' }, - { key: 'out', label: 'Расход' }, - ]} + options={contractors?.map(mapper)} /> - {_.times(recordsNumber).map(index => ( - <Paper variant="outlined" className="my-4 md:mx-4"> + <div className="grid grid-cols-2"> + <Select + name="operation" + label="Операция" + options={[ + { key: 'in', label: 'Приход' }, + { key: 'out', label: 'Расход' }, + ]} + /> + <Input name="date" type="date" label="Дата" /> + </div> + {values.records.map((record, index) => ( + <Paper variant="outlined" className="my-4 md:mx-4" key={`${index}-${record.productId}`}> <Select name={`records.${index}.productId`} label="Товар" - options={productOptions} - /> - <Input - name={`records.${index}.price`} - type="number" - label="Цена, $" - /> - <Input - name={`records.${index}.quantity`} - type="number" - label="Количество" + options={products?.map(mapper)} + required /> + <div className="grid grid-cols-3"> + <Input + name={`records.${index}.price`} + type="number" + label="Цена, $" + required + /> + <Input + name={`records.${index}.quantity`} + type="number" + label="Количество" + required + /> + <div className="flex justify-end items-end"> + <Button onClick={handleRemoveRecord(index)} size="sm" variant="outlined">Удалить</Button> + </div> + </div> </Paper> ))} <Button onClick={handleAddRecord} variant="outlined" size="sm">Добавить товар</Button> |