From 865b41114060765308d560181f4996c0aa7a3e74 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 26 Mar 2021 01:06:44 +0300 Subject: refactor: move Service to lib/ --- src/containers/Page.tsx | 2 +- src/containers/Service/Service.tsx | 25 ------------ src/containers/Service/ServiceContext.tsx | 37 ----------------- src/containers/Service/ServiceItem.tsx | 59 ---------------------------- src/containers/Service/ServiceList.tsx | 35 ----------------- src/index.tsx | 4 +- src/lib/Service.tsx | 25 ++++++++++++ src/lib/ServiceContext.tsx | 37 +++++++++++++++++ src/lib/ServiceItem.tsx | 59 ++++++++++++++++++++++++++++ src/lib/ServiceList.tsx | 35 +++++++++++++++++ src/services/account/index.ts | 2 +- src/services/contractors/ContractorPanel.tsx | 2 +- src/services/contractors/index.ts | 2 +- src/services/products/index.ts | 2 +- src/services/transfers/TransfersUpload.tsx | 2 +- src/services/transfers/index.ts | 2 +- src/services/waybills/WaybillPanel.tsx | 2 +- src/services/waybills/index.ts | 2 +- 18 files changed, 167 insertions(+), 167 deletions(-) delete mode 100644 src/containers/Service/Service.tsx delete mode 100644 src/containers/Service/ServiceContext.tsx delete mode 100644 src/containers/Service/ServiceItem.tsx delete mode 100644 src/containers/Service/ServiceList.tsx create mode 100644 src/lib/Service.tsx create mode 100644 src/lib/ServiceContext.tsx create mode 100644 src/lib/ServiceItem.tsx create mode 100644 src/lib/ServiceList.tsx (limited to 'src') diff --git a/src/containers/Page.tsx b/src/containers/Page.tsx index 73b122e..ae9e231 100644 --- a/src/containers/Page.tsx +++ b/src/containers/Page.tsx @@ -1,7 +1,7 @@ import React from 'react'; import Paper from '../components/Paper'; import Button from '../components/Button'; -import { Action } from './Service/ServiceContext'; +import { Action } from '../lib/ServiceContext'; interface Props { title?: string; diff --git a/src/containers/Service/Service.tsx b/src/containers/Service/Service.tsx deleted file mode 100644 index b527531..0000000 --- a/src/containers/Service/Service.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import React, { useContext } from 'react'; -import { Route, Switch, useRouteMatch } from 'react-router-dom'; -import _ from 'lodash'; -import ServiceList from './ServiceList'; -import ServiceItem from './ServiceItem'; -import ServiceContext from './ServiceContext'; - - -const Service: React.FC = () => { - const { path } = useRouteMatch(); - const service = useContext(ServiceContext); - - return ( - - - - {_.map(service.routes, (component, route) => ( - - ))} - - - ); -}; - -export default Service; diff --git a/src/containers/Service/ServiceContext.tsx b/src/containers/Service/ServiceContext.tsx deleted file mode 100644 index 0cccc93..0000000 --- a/src/containers/Service/ServiceContext.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import React from 'react'; -import { FormikProps } from 'formik'; -import { Props as ButtonProps } from '../../components/Button'; -import { Field } from '../../components/ListTable'; - -export interface Action extends ButtonProps { - name: string; -} - -export interface PanelProps { - item: any; - mutate: (item: any) => void; -} - -export interface ServiceParams { - route: string; - name: string; - nameSingular?: string; - tableFields: Field[]; - default?: Record; - routes?: Record; - actions?: Action[]; - rowLink?: (item: any) => string; - Form?: React.FC>; - Panel?: React.FC; -} - -const ServiceContext = React.createContext({ - route: '', - name: '', - nameSingular: '', - tableFields: [], - default: {}, -}); - -export const ServiceProvider = ServiceContext.Provider; -export default ServiceContext; diff --git a/src/containers/Service/ServiceItem.tsx b/src/containers/Service/ServiceItem.tsx deleted file mode 100644 index 0e24895..0000000 --- a/src/containers/Service/ServiceItem.tsx +++ /dev/null @@ -1,59 +0,0 @@ -import React, { useContext } from 'react'; -import { useParams, useHistory } from 'react-router-dom'; -import { Formik } from 'formik'; -import _ from 'lodash'; -import Page from '../Page'; -import hooks from '../../hooks/useAPIClient'; -import useQuery from '../../hooks/useQuery'; -import { post, patch, del } from '../../requests'; -import ServiceContext, { Action } from './ServiceContext'; - -interface Params { - id: string; -} - -const ServiceItem: React.FC = () => { - const service = useContext(ServiceContext); - const history = useHistory(); - const query = useQuery(); - 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[] = _.compact([ - { name: 'Назад', variant: 'outlined', onClick: history.goBack }, - id && { 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 deleted file mode 100644 index f716b24..0000000 --- a/src/containers/Service/ServiceList.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import React, { useContext } from 'react'; -import { useHistory, useLocation } from 'react-router-dom'; -import Page from '../Page'; -import ListTable from '../../components/ListTable'; -import hooks from '../../hooks/useAPIClient'; -import ServiceContext from './ServiceContext'; - -const ServiceList: React.FC = () => { - const service = useContext(ServiceContext); - const history = useHistory(); - const location = useLocation(); - const { data } = hooks[service.route].useList(location.search); - - const actions = service.actions || [{ - name: 'Добавить', - route: `/${service.route}/add${location.search}`, - }]; - - const handleRowClick = (index: number) => { - const item = data && data[index]; - const route = service.rowLink - ? service.rowLink(item) - : `/${service.route}/${item?._id}`; - - history.push(route); - }; - - return ( - - - - ); -}; - -export default ServiceList; diff --git a/src/index.tsx b/src/index.tsx index 1005a9d..2ea3201 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -8,8 +8,8 @@ import { } from 'react-router-dom'; import Header from './components/Header'; import Home from './containers/Home'; -import Service from './containers/Service/Service'; -import { ServiceProvider } from './containers/Service/ServiceContext'; +import Service from './lib/Service'; +import { ServiceProvider } from './lib/ServiceContext'; import services from './services'; const navigation = [ diff --git a/src/lib/Service.tsx b/src/lib/Service.tsx new file mode 100644 index 0000000..b527531 --- /dev/null +++ b/src/lib/Service.tsx @@ -0,0 +1,25 @@ +import React, { useContext } from 'react'; +import { Route, Switch, useRouteMatch } from 'react-router-dom'; +import _ from 'lodash'; +import ServiceList from './ServiceList'; +import ServiceItem from './ServiceItem'; +import ServiceContext from './ServiceContext'; + + +const Service: React.FC = () => { + const { path } = useRouteMatch(); + const service = useContext(ServiceContext); + + return ( + + + + {_.map(service.routes, (component, route) => ( + + ))} + + + ); +}; + +export default Service; diff --git a/src/lib/ServiceContext.tsx b/src/lib/ServiceContext.tsx new file mode 100644 index 0000000..93cac4e --- /dev/null +++ b/src/lib/ServiceContext.tsx @@ -0,0 +1,37 @@ +import React from 'react'; +import { FormikProps } from 'formik'; +import { Props as ButtonProps } from '../components/Button'; +import { Field } from '../components/ListTable'; + +export interface Action extends ButtonProps { + name: string; +} + +export interface PanelProps { + item: any; + mutate: (item: any) => void; +} + +export interface ServiceParams { + route: string; + name: string; + tableFields: Field[]; + nameSingular?: string; + default?: Record; + routes?: Record; + actions?: Action[]; + rowLink?: (item: any) => string; + Form?: React.FC>; + Panel?: React.FC; +} + +const ServiceContext = React.createContext({ + route: '', + name: '', + nameSingular: '', + tableFields: [], + default: {}, +}); + +export const ServiceProvider = ServiceContext.Provider; +export default ServiceContext; diff --git a/src/lib/ServiceItem.tsx b/src/lib/ServiceItem.tsx new file mode 100644 index 0000000..6a0a99b --- /dev/null +++ b/src/lib/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 from '../containers/Page'; +import hooks from '../hooks/useAPIClient'; +import useQuery from '../hooks/useQuery'; +import { post, patch, del } from '../requests'; +import ServiceContext, { Action } from './ServiceContext'; + +interface Params { + id: string; +} + +const ServiceItem: React.FC = () => { + const service = useContext(ServiceContext); + const history = useHistory(); + const query = useQuery(); + 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[] = _.compact([ + { name: 'Назад', variant: 'outlined', onClick: history.goBack }, + id && { name: 'Удалить', variant: 'outlined', onClick: handleDelete }, + { name: 'Сохранить', type: 'submit', form: 'form' }, + ]); + + return ( + + {(!id || item) && ( + + )} + {item && service.Panel && } + + ); +}; + +export default ServiceItem; diff --git a/src/lib/ServiceList.tsx b/src/lib/ServiceList.tsx new file mode 100644 index 0000000..74ad10b --- /dev/null +++ b/src/lib/ServiceList.tsx @@ -0,0 +1,35 @@ +import React, { useContext } from 'react'; +import { useHistory, useLocation } from 'react-router-dom'; +import Page from '../containers/Page'; +import ListTable from '../components/ListTable'; +import hooks from '../hooks/useAPIClient'; +import ServiceContext from './ServiceContext'; + +const ServiceList: React.FC = () => { + const service = useContext(ServiceContext); + const history = useHistory(); + const location = useLocation(); + const { data } = hooks[service.route].useList(location.search); + + const actions = service.actions || [{ + name: 'Добавить', + route: `/${service.route}/add${location.search}`, + }]; + + const handleRowClick = (index: number) => { + const item = data && data[index]; + const route = service.rowLink + ? service.rowLink(item) + : `/${service.route}/${item?._id}`; + + history.push(route); + }; + + return ( + + + + ); +}; + +export default ServiceList; diff --git a/src/services/account/index.ts b/src/services/account/index.ts index 7cc28b4..cbe56f5 100644 --- a/src/services/account/index.ts +++ b/src/services/account/index.ts @@ -1,4 +1,4 @@ -import { ServiceParams } from '../../containers/Service/ServiceContext'; +import { ServiceParams } from '../../lib/ServiceContext'; const service: ServiceParams = { route: 'account', diff --git a/src/services/contractors/ContractorPanel.tsx b/src/services/contractors/ContractorPanel.tsx index a3650ef..f7ed8ad 100644 --- a/src/services/contractors/ContractorPanel.tsx +++ b/src/services/contractors/ContractorPanel.tsx @@ -1,6 +1,6 @@ import React from 'react'; import Button from '../../components/Button'; -import { PanelProps } from '../../containers/Service/ServiceContext'; +import { PanelProps } from '../../lib/ServiceContext'; const ContractorPanel: React.FC = ({ item }) => { diff --git a/src/services/contractors/index.ts b/src/services/contractors/index.ts index c9e71a1..2341f3a 100644 --- a/src/services/contractors/index.ts +++ b/src/services/contractors/index.ts @@ -1,6 +1,6 @@ import Form from './ContractorForm'; import Panel from './ContractorPanel'; -import { ServiceParams } from '../../containers/Service/ServiceContext'; +import { ServiceParams } from '../../lib/ServiceContext'; const service: ServiceParams = { route: 'contractors', diff --git a/src/services/products/index.ts b/src/services/products/index.ts index 8d42c56..c869541 100644 --- a/src/services/products/index.ts +++ b/src/services/products/index.ts @@ -1,5 +1,5 @@ import Form from './ProductForm'; -import { ServiceParams } from '../../containers/Service/ServiceContext'; +import { ServiceParams } from '../../lib/ServiceContext'; const service: ServiceParams = { route: 'products', diff --git a/src/services/transfers/TransfersUpload.tsx b/src/services/transfers/TransfersUpload.tsx index aefc72c..c14a5c1 100644 --- a/src/services/transfers/TransfersUpload.tsx +++ b/src/services/transfers/TransfersUpload.tsx @@ -3,7 +3,7 @@ import { useHistory } from 'react-router-dom'; import { Form, Formik } from 'formik'; import Input from '../../components/Input'; import Page from '../../containers/Page'; -import { Action } from '../../containers/Service/ServiceContext'; +import { Action } from '../../lib/ServiceContext'; import { post } from '../../requests'; const TransfersUpload: React.FC = () => { diff --git a/src/services/transfers/index.ts b/src/services/transfers/index.ts index 51be7e3..37f7b11 100644 --- a/src/services/transfers/index.ts +++ b/src/services/transfers/index.ts @@ -1,7 +1,7 @@ import Form from './TransferForm'; import UploadPage from './TransfersUpload'; import { transformOperation } from '../transforms'; -import { ServiceParams } from '../../containers/Service/ServiceContext'; +import { ServiceParams } from '../../lib/ServiceContext'; const service: ServiceParams = { diff --git a/src/services/waybills/WaybillPanel.tsx b/src/services/waybills/WaybillPanel.tsx index 101a871..1d1701d 100644 --- a/src/services/waybills/WaybillPanel.tsx +++ b/src/services/waybills/WaybillPanel.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { useHistory } from 'react-router-dom'; import Button from '../../components/Button'; import { patch, baseURL } from '../../requests'; -import { PanelProps } from '../../containers/Service/ServiceContext'; +import { PanelProps } from '../../lib/ServiceContext'; const WaybillPanel: React.FC = ({ item, mutate }) => { diff --git a/src/services/waybills/index.ts b/src/services/waybills/index.ts index 4e50f54..8cd6c0c 100644 --- a/src/services/waybills/index.ts +++ b/src/services/waybills/index.ts @@ -1,7 +1,7 @@ import Form from './WaybillForm'; import Panel from './WaybillPanel'; import { transformOperation, transformStatus } from '../transforms'; -import { ServiceParams } from '../../containers/Service/ServiceContext'; +import { ServiceParams } from '../../lib/ServiceContext'; const service: ServiceParams = { route: 'waybills', -- cgit v1.2.3