summaryrefslogtreecommitdiff
path: root/src/containers/ProductForm.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-14 10:10:16 +0300
committereug-vs <eug-vs@keemail.me>2021-03-14 10:10:16 +0300
commit2c106d1d3f6d59b10ad946e01e8bb3d0df587e40 (patch)
treed0eff1bb17d55c34e52eb7c77d30631eaee87839 /src/containers/ProductForm.tsx
parentde4811ce8d2e739901c047f39e9b4b7c18298e74 (diff)
downloadcommercel-ui-2c106d1d3f6d59b10ad946e01e8bb3d0df587e40.tar.gz
refactor: create Service abstraction
Diffstat (limited to 'src/containers/ProductForm.tsx')
-rw-r--r--src/containers/ProductForm.tsx48
1 files changed, 7 insertions, 41 deletions
diff --git a/src/containers/ProductForm.tsx b/src/containers/ProductForm.tsx
index 0d21df9..4d01881 100644
--- a/src/containers/ProductForm.tsx
+++ b/src/containers/ProductForm.tsx
@@ -1,50 +1,16 @@
import React from 'react';
-import { useParams, useHistory } from 'react-router-dom';
-import { Formik, Form, Field } from 'formik';
-import Page, { Action } from './Page';
+import { Form, Field } from 'formik';
import Input from '../components/Input';
-import { useProduct } from '../hooks/useAPIClient';
-import { post, patch } from '../requests';
-interface Params {
- id: string;
-}
-
-const actions: Action[] = [
- { name: 'Назад', variant: 'outlined', route: '..' },
- { name: 'Сохранить', type: 'submit', form: 'productForm' },
-];
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>
+ <Form id="form">
+ <div className="max-w-lg">
+ <Field name="name" label="Название" as={Input} />
+ <Field name="price" type="number" label="Цена ($)" as={Input} />
+ </div>
+ </Form>
);
};