summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-20 17:52:29 +0300
committereug-vs <eug-vs@keemail.me>2021-03-20 17:52:29 +0300
commit5f1bf64ed38fdda9143b75f0ab847eebe55eaaaa (patch)
tree92ca75a3eb1df3b753566dc6ca148360d6e0d06b
parent839c9ccc0e12b7d15611df72baf9acaea19e8480 (diff)
downloadcommercel-ui-5f1bf64ed38fdda9143b75f0ab847eebe55eaaaa.tar.gz
feat: allow extending Services
-rw-r--r--src/containers/Service/Service.tsx8
-rw-r--r--src/containers/Service/ServiceContext.tsx3
-rw-r--r--src/containers/Service/ServiceList.tsx10
-rw-r--r--src/containers/TransferForm.tsx2
4 files changed, 20 insertions, 3 deletions
diff --git a/src/containers/Service/Service.tsx b/src/containers/Service/Service.tsx
index 162fce0..b527531 100644
--- a/src/containers/Service/Service.tsx
+++ b/src/containers/Service/Service.tsx
@@ -1,16 +1,22 @@
-import React from 'react';
+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>
);
diff --git a/src/containers/Service/ServiceContext.tsx b/src/containers/Service/ServiceContext.tsx
index f9e1605..68ff907 100644
--- a/src/containers/Service/ServiceContext.tsx
+++ b/src/containers/Service/ServiceContext.tsx
@@ -6,12 +6,15 @@ export interface PanelProps {
mutate: (item: any) => void;
}
+type Route = Record<string, React.FC>;
+
export interface ServiceParams {
route: string;
name: string;
nameSingular: string;
tableFields: any[];
default: Record<string, any>;
+ routes?: Route[];
Form?: React.FC<FormikProps>;
Panel?: React.FC<PanelProps>;
}
diff --git a/src/containers/Service/ServiceList.tsx b/src/containers/Service/ServiceList.tsx
index b7054eb..7d30c14 100644
--- a/src/containers/Service/ServiceList.tsx
+++ b/src/containers/Service/ServiceList.tsx
@@ -11,7 +11,15 @@ const ServiceList: React.FC = () => {
const history = useHistory();
const { data } = hooks[service.route].useList();
- const actions = [{ name: 'Добавить', route: `/${service.route}/add` }];
+ const actions = [
+ ...(
+ service.actions?.map(action => ({
+ ...action,
+ route: `/${service.route}/${action.route}`,
+ })) || []
+ ),
+ { name: 'Добавить', route: `/${service.route}/add` },
+ ];
const handleRowClick = (index: number) => {
const item = data && data[index];
diff --git a/src/containers/TransferForm.tsx b/src/containers/TransferForm.tsx
index afb2dff..801150e 100644
--- a/src/containers/TransferForm.tsx
+++ b/src/containers/TransferForm.tsx
@@ -13,7 +13,7 @@ const TransferForm: React.FC<FormikProps> = ({ setFieldValue, values }) => {
const { data: contractors } = hooks.contractors.useList();
if (!values.date) setFieldValue('date', moment().format('YYYY-MM-DD'));
- if (!values.contractorId && contractors) setFieldValue('contractorId', contractors[0]._id);
+ if (!values.contractorId && contractors?.length) setFieldValue('contractorId', contractors[0]._id);
return (
<Form id="form">