From 5f1bf64ed38fdda9143b75f0ab847eebe55eaaaa Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 20 Mar 2021 17:52:29 +0300 Subject: feat: allow extending Services --- src/containers/Service/Service.tsx | 8 +++++++- src/containers/Service/ServiceContext.tsx | 3 +++ src/containers/Service/ServiceList.tsx | 10 +++++++++- src/containers/TransferForm.tsx | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) (limited to 'src') 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 ( + {_.map(service.routes, (component, route) => ( + + ))} ); 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; + export interface ServiceParams { route: string; name: string; nameSingular: string; tableFields: any[]; default: Record; + routes?: Route[]; Form?: React.FC; Panel?: React.FC; } 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 = ({ 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 (
-- cgit v1.2.3