diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-26 01:06:44 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-26 01:06:44 +0300 |
commit | 865b41114060765308d560181f4996c0aa7a3e74 (patch) | |
tree | 578d3089e05131445b0a8b6d5e69a3a76aaf7e73 /src/containers | |
parent | 91e1a3b4ccaa822097e4adfea5f51056b010fdd6 (diff) | |
download | commercel-ui-865b41114060765308d560181f4996c0aa7a3e74.tar.gz |
refactor: move Service to lib/
Diffstat (limited to 'src/containers')
-rw-r--r-- | src/containers/Page.tsx | 2 | ||||
-rw-r--r-- | src/containers/Service/Service.tsx | 25 | ||||
-rw-r--r-- | src/containers/Service/ServiceContext.tsx | 37 | ||||
-rw-r--r-- | src/containers/Service/ServiceItem.tsx | 59 | ||||
-rw-r--r-- | src/containers/Service/ServiceList.tsx | 35 |
5 files changed, 1 insertions, 157 deletions
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 ( - <Switch> - <Route exact path={path} component={ServiceList} /> - <Route path={`${path}/add`} component={ServiceItem} /> - {_.map(service.routes, (component, route) => ( - <Route path={`${path}/${route}`} component={component} key={route} /> - ))} - <Route path={`${path}/:id`} component={ServiceItem} /> - </Switch> - ); -}; - -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<string, any>; - routes?: Record<string, React.FC>; - actions?: Action[]; - rowLink?: (item: any) => string; - Form?: React.FC<FormikProps<any>>; - Panel?: React.FC<PanelProps>; -} - -const ServiceContext = React.createContext<ServiceParams>({ - 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<Params>(); - 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 ( - <Page - title={id ? item?.name : `Новый ${service.nameSingular}`} - actions={actions} - className="grid lg:grid-cols-2" - > - {(!id || item) && ( - <Formik - initialValues={_.defaults(item, query, service.default)} - onSubmit={onSubmit} - children={service.Form} - /> - )} - {item && service.Panel && <service.Panel item={item} mutate={mutate} />} - </Page> - ); -}; - -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 ( - <Page title={service.name} actions={actions}> - <ListTable items={data} fields={service.tableFields} handleRowClick={handleRowClick} /> - </Page> - ); -}; - -export default ServiceList; |