summaryrefslogtreecommitdiff
path: root/src/containers
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-14 07:05:45 +0300
committereug-vs <eug-vs@keemail.me>2021-03-14 07:05:45 +0300
commitde4811ce8d2e739901c047f39e9b4b7c18298e74 (patch)
tree01450303f497bb6c8433a360efc732f9fb9b4f08 /src/containers
parente615ad0282fa02ce7e81a847b43dd3146f69b769 (diff)
downloadcommercel-ui-de4811ce8d2e739901c047f39e9b4b7c18298e74.tar.gz
feat: add Contractors section
Diffstat (limited to 'src/containers')
-rw-r--r--src/containers/ContractorForm.tsx52
-rw-r--r--src/containers/Contractors.tsx33
2 files changed, 85 insertions, 0 deletions
diff --git a/src/containers/ContractorForm.tsx b/src/containers/ContractorForm.tsx
new file mode 100644
index 0000000..7f0d660
--- /dev/null
+++ b/src/containers/ContractorForm.tsx
@@ -0,0 +1,52 @@
+import React from 'react';
+import { useParams, useHistory } from 'react-router-dom';
+import { Formik, Form, Field } from 'formik';
+import Page, { Action } from './Page';
+import Input from '../components/Input';
+import { useContractor } from '../hooks/useAPIClient';
+import { post, patch } from '../requests';
+
+interface Params {
+ id: string;
+}
+
+const actions: Action[] = [
+ { name: 'Назад', variant: 'outlined', route: '..' },
+ { name: 'Сохранить', type: 'submit', form: 'contractorForm' },
+];
+
+const ContractorForm: React.FC = () => {
+ const history = useHistory();
+ const { id } = useParams<Params>();
+ const { data: contractor } = useContractor(id);
+
+ const onSubmit = (values: any) => {
+ const promise = id
+ ? patch(`/contractors/${id}`, values)
+ : post('/contractors', values);
+ return promise.then(() => history.push('/contractors'));
+ };
+
+ return (
+ <Page title={id ? contractor?.name : 'Новый контрагент'} actions={actions}>
+ {(!id || contractor) && (
+ <Formik
+ initialValues={contractor || { name: '', debt: '', vatId: '' }}
+ onSubmit={onSubmit}
+ >
+ {() => (
+ <Form id="contractorForm">
+ <div className="max-w-lg">
+ <Field name="name" label="Название" as={Input} />
+ <Field name="vatId" label="УНП" as={Input} />
+ <Field name="debt" type="number" label="Долг ($)" as={Input} />
+ </div>
+ </Form>
+ )}
+ </Formik>
+ )}
+ </Page>
+ );
+};
+
+export default ContractorForm;
diff --git a/src/containers/Contractors.tsx b/src/containers/Contractors.tsx
new file mode 100644
index 0000000..5d589ea
--- /dev/null
+++ b/src/containers/Contractors.tsx
@@ -0,0 +1,33 @@
+import React from 'react';
+import { useHistory } from 'react-router-dom';
+import Page from './Page';
+import ListTable from '../components/ListTable';
+import { useContractors } from '../hooks/useAPIClient';
+
+const fields = [
+ { key: 'vatId', label: 'УНП' },
+ { key: 'name', label: 'Название' },
+ { key: 'debt', label: 'Долг' },
+];
+
+const actions = [
+ { name: 'Добавить', route: 'contractors/add' },
+];
+
+const Contractors: React.FC = () => {
+ const history = useHistory();
+ const { data: contractors } = useContractors();
+
+ const handleRowClick = (index: number) => {
+ const contractor = contractors && contractors[index];
+ history.push(`/contractors/edit/${contractor?._id}`);
+ };
+
+ return (
+ <Page title="Контрагенты" actions={actions}>
+ <ListTable items={contractors} fields={fields} handleRowClick={handleRowClick} />
+ </Page>
+ );
+};
+
+export default Contractors;