summaryrefslogtreecommitdiff
path: root/src/containers/Service/ServiceForm.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/Service/ServiceForm.tsx
parentde4811ce8d2e739901c047f39e9b4b7c18298e74 (diff)
downloadcommercel-ui-2c106d1d3f6d59b10ad946e01e8bb3d0df587e40.tar.gz
refactor: create Service abstraction
Diffstat (limited to 'src/containers/Service/ServiceForm.tsx')
-rw-r--r--src/containers/Service/ServiceForm.tsx45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/containers/Service/ServiceForm.tsx b/src/containers/Service/ServiceForm.tsx
new file mode 100644
index 0000000..273f5bd
--- /dev/null
+++ b/src/containers/Service/ServiceForm.tsx
@@ -0,0 +1,45 @@
+import React, { useContext } from 'react';
+import { useParams, useHistory } from 'react-router-dom';
+import { Formik } from 'formik';
+import Page, { Action } from '../Page';
+import hooks from '../../hooks/useAPIClient';
+import { post, patch } from '../../requests';
+import ServiceContext from './ServiceContext';
+
+interface Params {
+ id: string;
+}
+
+const actions: Action[] = [
+ { name: 'Назад', variant: 'outlined', route: '..' },
+ { name: 'Сохранить', type: 'submit', form: 'form' },
+];
+
+const ServiceForm: React.FC = () => {
+ const service = useContext(ServiceContext);
+ const history = useHistory();
+ const { id } = useParams<Params>();
+ const { data: item } = hooks[service.route].useItem(id);
+
+ const onSubmit = (values: any) => {
+ const promise = id
+ ? patch(`/${service.route}/${id}`, values)
+ : post(`/${service.route}`, values);
+ return promise.then(() => history.push(`/${service.route}`));
+ };
+
+ return (
+ <Page title={id ? item?.name : `Новый ${service.nameSingular}`} actions={actions}>
+ {(!id || item) && (
+ <Formik
+ initialValues={item || { name: '', debt: '', vatId: '' }}
+ onSubmit={onSubmit}
+ >
+ <service.Form />
+ </Formik>
+ )}
+ </Page>
+ );
+};
+
+export default ServiceForm;