summaryrefslogtreecommitdiff
path: root/src/containers/ProductForm.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-14 06:30:22 +0300
committereug-vs <eug-vs@keemail.me>2021-03-14 06:30:22 +0300
commit610584c3ce986cdea431180f545e023cee14d5d2 (patch)
treea4c1b2a3de7b4ccf7cef8a4bdf5d366cc62492b5 /src/containers/ProductForm.tsx
parent790cb41dfe3283fea96789fbacc28a077c474e44 (diff)
downloadcommercel-ui-610584c3ce986cdea431180f545e023cee14d5d2.tar.gz
feat: implement Product Add/Edit
Diffstat (limited to 'src/containers/ProductForm.tsx')
-rw-r--r--src/containers/ProductForm.tsx51
1 files changed, 44 insertions, 7 deletions
diff --git a/src/containers/ProductForm.tsx b/src/containers/ProductForm.tsx
index d79b1d1..0d21df9 100644
--- a/src/containers/ProductForm.tsx
+++ b/src/containers/ProductForm.tsx
@@ -1,14 +1,51 @@
import React from 'react';
-import Page, { Action } from '../containers/Page';
+import { useParams, useHistory } from 'react-router-dom';
+import { Formik, Form, Field } from 'formik';
+import Page, { Action } from './Page';
+import Input from '../components/Input';
+import { useProduct } from '../hooks/useAPIClient';
+import { post, patch } from '../requests';
+
+interface Params {
+ id: string;
+}
const actions: Action[] = [
- { name: 'Назад', route: '/', variant: 'outlined' },
- { name: 'Сохранить', route: '/' },
+ { name: 'Назад', variant: 'outlined', route: '..' },
+ { name: 'Сохранить', type: 'submit', form: 'productForm' },
];
-const ProductForm: React.FC = () => (
- <Page title="Продукт" actions={actions}>
- </Page>
-);
+const ProductForm: React.FC = () => {
+ const history = useHistory();
+ const { id } = useParams<Params>();
+ const { data: product } = useProduct(id);
+
+ const onSubmit = (values: any) => {
+ const promise = id
+ ? patch(`/products/${id}`, values)
+ : post('/products', values);
+ return promise.then(() => history.push('/products'));
+ };
+
+ return (
+ <Page title={id ? product?.name : 'Новый товар'} actions={actions}>
+ {(!id || product) && (
+ <Formik
+ initialValues={product || { name: '', price: '' }}
+ onSubmit={onSubmit}
+ >
+ {() => (
+ <Form id="productForm">
+ <div className="max-w-lg">
+ <Field name="name" label="Название" as={Input} />
+ <Field name="price" type="number" label="Цена ($)" as={Input} />
+ </div>
+ </Form>
+ )}
+ </Formik>
+ )}
+ </Page>
+ );
+};
export default ProductForm;