From 7c2f2f2835ac7a8a13704bbc9fd706128243286a Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sun, 14 Mar 2021 15:43:27 +0300
Subject: feat: add Delete button

---
 src/containers/Service/ServiceForm.tsx | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

(limited to 'src/containers/Service/ServiceForm.tsx')

diff --git a/src/containers/Service/ServiceForm.tsx b/src/containers/Service/ServiceForm.tsx
index e2dfef6..69cc3c8 100644
--- a/src/containers/Service/ServiceForm.tsx
+++ b/src/containers/Service/ServiceForm.tsx
@@ -4,24 +4,22 @@ import { Formik } from 'formik';
 import _ from 'lodash';
 import Page, { Action } from '../Page';
 import hooks from '../../hooks/useAPIClient';
-import { post, patch } from '../../requests';
+import { post, patch, del } 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 handleDelete = () => del(`/${service.route}/${id}`)
+    .then(() => history.push(`/${service.route}`));
+
   const onSubmit = (values: any) => {
     const promise = id
       ? patch(`/${service.route}/${id}`, values)
@@ -29,6 +27,12 @@ const ServiceForm: React.FC = () => {
     return promise.then(() => history.push(`/${service.route}`));
   };
 
+  const actions: Action[] = [
+    { name: 'Назад', variant: 'outlined', route: '..' },
+    { name: 'Удалить', variant: 'outlined', onClick: handleDelete },
+    { name: 'Сохранить', type: 'submit', form: 'form' },
+  ];
+
   return (
     <Page title={id ? item?.name : `Новый ${service.nameSingular}`} actions={actions}>
       {(!id || item) && (
-- 
cgit v1.2.3

