From 49a44a5762a2863566267689002834ee88d06abb Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 16 Mar 2021 01:38:54 +0300 Subject: feat: add WaybillPanel --- src/containers/Service/Service.tsx | 6 ++-- src/containers/Service/ServiceContext.tsx | 5 +++ src/containers/Service/ServiceForm.tsx | 52 --------------------------- src/containers/Service/ServiceItem.tsx | 59 +++++++++++++++++++++++++++++++ src/containers/Service/ServiceList.tsx | 2 +- src/containers/WaybillForm.tsx | 40 ++++++++++----------- src/containers/WaybillPanel.tsx | 44 +++++++++++++++++++++++ src/index.tsx | 2 ++ 8 files changed, 133 insertions(+), 77 deletions(-) delete mode 100644 src/containers/Service/ServiceForm.tsx create mode 100644 src/containers/Service/ServiceItem.tsx create mode 100644 src/containers/WaybillPanel.tsx diff --git a/src/containers/Service/Service.tsx b/src/containers/Service/Service.tsx index 3a7cbee..162fce0 100644 --- a/src/containers/Service/Service.tsx +++ b/src/containers/Service/Service.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Route, Switch, useRouteMatch } from 'react-router-dom'; import ServiceList from './ServiceList'; -import ServiceForm from './ServiceForm'; +import ServiceItem from './ServiceItem'; const Service: React.FC = () => { @@ -10,8 +10,8 @@ const Service: React.FC = () => { return ( - - + + ); }; diff --git a/src/containers/Service/ServiceContext.tsx b/src/containers/Service/ServiceContext.tsx index c035417..b4200a0 100644 --- a/src/containers/Service/ServiceContext.tsx +++ b/src/containers/Service/ServiceContext.tsx @@ -1,6 +1,10 @@ import React from 'react'; import { FormikProps } from 'formik'; +export interface PanelProps { + item: any; +} + export interface ServiceParams { route: string; name: string; @@ -8,6 +12,7 @@ export interface ServiceParams { tableFields: any[]; default: Record; Form?: React.FC; + Panel?: React.FC; } const ServiceContext = React.createContext({ diff --git a/src/containers/Service/ServiceForm.tsx b/src/containers/Service/ServiceForm.tsx deleted file mode 100644 index 0c02cbd..0000000 --- a/src/containers/Service/ServiceForm.tsx +++ /dev/null @@ -1,52 +0,0 @@ -import React, { useContext } from 'react'; -import { useParams, useHistory } from 'react-router-dom'; -import { Formik } from 'formik'; -import _ from 'lodash'; -import Page, { Action } from '../Page'; -import hooks from '../../hooks/useAPIClient'; -import { post, patch, del } from '../../requests'; -import ServiceContext from './ServiceContext'; - -interface Params { - id: string; -} - -const ServiceForm: React.FC = () => { - const service = useContext(ServiceContext); - const history = useHistory(); - const { id } = useParams(); - const { data: item, mutate } = 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) - : post(`/${service.route}`, values); - return promise.then(response => { - mutate(response.data); - history.push(`/${service.route}`); - }); - }; - - const actions: Action[] = [ - { name: 'Назад', variant: 'outlined', route: '..' }, - { name: 'Удалить', variant: 'outlined', onClick: handleDelete }, - { name: 'Сохранить', type: 'submit', form: 'form' }, - ]; - - return ( - - {(!id || item) && ( - - )} - - ); -}; - -export default ServiceForm; diff --git a/src/containers/Service/ServiceItem.tsx b/src/containers/Service/ServiceItem.tsx new file mode 100644 index 0000000..bd449f9 --- /dev/null +++ b/src/containers/Service/ServiceItem.tsx @@ -0,0 +1,59 @@ +import React, { useContext } from 'react'; +import { useParams, useHistory } from 'react-router-dom'; +import { Formik } from 'formik'; +import _ from 'lodash'; +import Page, { Action } from '../Page'; +import hooks from '../../hooks/useAPIClient'; +import { post, patch, del } from '../../requests'; +import ServiceContext from './ServiceContext'; + +interface Params { + id: string; +} + +const ServiceItem: React.FC = () => { + const service = useContext(ServiceContext); + const history = useHistory(); + const { id } = useParams(); + const { data: item, mutate } = 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) + : post(`/${service.route}`, values); + return promise.then(response => { + mutate(response.data); + history.push(`/${service.route}`); + }); + }; + + const actions: Action[] = [ + { name: 'Назад', variant: 'outlined', onClick: history.goBack }, + { name: 'Удалить', variant: 'outlined', onClick: handleDelete }, + { name: 'Сохранить', type: 'submit', form: 'form' }, + ]; + + return ( + + {(!id || item) && ( +
+ +
+ )} + {item && service.Panel && } +
+ ); +}; + +export default ServiceItem; diff --git a/src/containers/Service/ServiceList.tsx b/src/containers/Service/ServiceList.tsx index 6af2d1b..b7054eb 100644 --- a/src/containers/Service/ServiceList.tsx +++ b/src/containers/Service/ServiceList.tsx @@ -15,7 +15,7 @@ const ServiceList: React.FC = () => { const handleRowClick = (index: number) => { const item = data && data[index]; - history.push(`/${service.route}/edit/${item?._id}`); + history.push(`/${service.route}/${item?._id}`); }; return ( diff --git a/src/containers/WaybillForm.tsx b/src/containers/WaybillForm.tsx index 1e3276f..fedc9c4 100644 --- a/src/containers/WaybillForm.tsx +++ b/src/containers/WaybillForm.tsx @@ -10,27 +10,25 @@ const WaybillForm: React.FC = ({ setFieldValue, values }) => { return (
-
- - -
+ +
); }; diff --git a/src/containers/WaybillPanel.tsx b/src/containers/WaybillPanel.tsx new file mode 100644 index 0000000..ca40169 --- /dev/null +++ b/src/containers/WaybillPanel.tsx @@ -0,0 +1,44 @@ +import React from 'react'; +import { useHistory } from 'react-router-dom'; +import Input from '../components/Input'; +import Button from '../components/Button'; +import Paper from '../components/Paper'; +import { post } from '../requests'; +import { PanelProps } from './Service/ServiceContext'; + + +const WaybillPanel: React.FC = ({ item }) => { + const history = useHistory(); + + const handleExecute = () => post(`/waybills/${item._id}/execute`) + .then(() => history.push('/waybills')); + + const handleCancel = () => post(`/waybills/${item._id}/cancel`) + .then(() => history.push('/waybills')); + + const executed = item.status === 'executed'; + const total = item.product.price * item.quantity; + + return ( +
+

+ Итоговая сумма: ${total} +

+
+ + +
+ { + executed + ? + : + } +
+ ); +}; + +export default WaybillPanel; diff --git a/src/index.tsx b/src/index.tsx index e637e68..68f3222 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -12,12 +12,14 @@ import Service from './containers/Service/Service'; import { ServiceProvider } from './containers/Service/ServiceContext'; import services from './services'; import WaybillForm from './containers/WaybillForm'; +import WaybillPanel from './containers/WaybillPanel'; import ContractorForm from './containers/ContractorForm'; import ProductForm from './containers/ProductForm'; services[0].Form = ProductForm; services[1].Form = ContractorForm; services[2].Form = WaybillForm; +services[2].Panel = WaybillPanel; const navigation = [ -- cgit v1.2.3