From 2c106d1d3f6d59b10ad946e01e8bb3d0df587e40 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sun, 14 Mar 2021 10:10:16 +0300
Subject: refactor: create Service abstraction

---
 src/containers/ContractorForm.tsx | 51 ++++++---------------------------------
 1 file changed, 8 insertions(+), 43 deletions(-)

(limited to 'src/containers/ContractorForm.tsx')

diff --git a/src/containers/ContractorForm.tsx b/src/containers/ContractorForm.tsx
index 7f0d660..a67eabe 100644
--- a/src/containers/ContractorForm.tsx
+++ b/src/containers/ContractorForm.tsx
@@ -1,51 +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 { 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>
+    <Form id="form">
+      <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>
   );
 };
 
-- 
cgit v1.2.3

