diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-26 00:44:51 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-26 00:44:51 +0300 |
commit | f1cdf02e9c0dca3354f4eebd38edd2ba12075893 (patch) | |
tree | c164c722d879869024a1dcd07fec225eb5f5bf47 | |
parent | 062f10a25d43b875d187cf582b2ecf96d075ec26 (diff) | |
download | commercel-ui-f1cdf02e9c0dca3354f4eebd38edd2ba12075893.tar.gz |
fix: improve typings
-rw-r--r-- | src/components/ListTable.tsx | 4 | ||||
-rw-r--r-- | src/containers/Page.tsx | 7 | ||||
-rw-r--r-- | src/containers/Service/ServiceContext.tsx | 13 | ||||
-rw-r--r-- | src/containers/Service/ServiceItem.tsx | 4 | ||||
-rw-r--r-- | src/hooks/useAPIClient.ts | 2 | ||||
-rw-r--r-- | src/services/account/index.ts | 8 | ||||
-rw-r--r-- | src/services/constants.ts | 11 | ||||
-rw-r--r-- | src/services/contractors/ContractorPanel.tsx | 6 | ||||
-rw-r--r-- | src/services/contractors/index.ts | 3 | ||||
-rw-r--r-- | src/services/index.ts | 4 | ||||
-rw-r--r-- | src/services/products/index.ts | 3 | ||||
-rw-r--r-- | src/services/transfers/TransfersUpload.tsx | 3 | ||||
-rw-r--r-- | src/services/transfers/index.ts | 9 | ||||
-rw-r--r-- | src/services/transforms.ts | 21 | ||||
-rw-r--r-- | src/services/waybills/index.ts | 13 |
15 files changed, 61 insertions, 50 deletions
diff --git a/src/components/ListTable.tsx b/src/components/ListTable.tsx index 4bbe633..79e1ecd 100644 --- a/src/components/ListTable.tsx +++ b/src/components/ListTable.tsx @@ -1,10 +1,10 @@ import React from 'react'; import _ from 'lodash'; -interface Field { +export interface Field { key: string; label: string; - transform?: (text: string) => string; + transform?: (value: string) => JSX.Element | string; } interface Props { diff --git a/src/containers/Page.tsx b/src/containers/Page.tsx index d3a087a..73b122e 100644 --- a/src/containers/Page.tsx +++ b/src/containers/Page.tsx @@ -1,10 +1,7 @@ import React from 'react'; import Paper from '../components/Paper'; -import Button, { Props as ButtonProps } from '../components/Button'; - -export interface Action extends ButtonProps { - name: string; -} +import Button from '../components/Button'; +import { Action } from './Service/ServiceContext'; interface Props { title?: string; diff --git a/src/containers/Service/ServiceContext.tsx b/src/containers/Service/ServiceContext.tsx index 75ac0fb..0cccc93 100644 --- a/src/containers/Service/ServiceContext.tsx +++ b/src/containers/Service/ServiceContext.tsx @@ -1,6 +1,11 @@ import React from 'react'; import { FormikProps } from 'formik'; -import { Action } from '../Page'; +import { Props as ButtonProps } from '../../components/Button'; +import { Field } from '../../components/ListTable'; + +export interface Action extends ButtonProps { + name: string; +} export interface PanelProps { item: any; @@ -10,9 +15,9 @@ export interface PanelProps { export interface ServiceParams { route: string; name: string; - nameSingular: string; - tableFields: any[]; - default: Record<string, any>; + nameSingular?: string; + tableFields: Field[]; + default?: Record<string, any>; routes?: Record<string, React.FC>; actions?: Action[]; rowLink?: (item: any) => string; diff --git a/src/containers/Service/ServiceItem.tsx b/src/containers/Service/ServiceItem.tsx index f833227..0e24895 100644 --- a/src/containers/Service/ServiceItem.tsx +++ b/src/containers/Service/ServiceItem.tsx @@ -2,11 +2,11 @@ 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 Page from '../Page'; import hooks from '../../hooks/useAPIClient'; import useQuery from '../../hooks/useQuery'; import { post, patch, del } from '../../requests'; -import ServiceContext from './ServiceContext'; +import ServiceContext, { Action } from './ServiceContext'; interface Params { id: string; diff --git a/src/hooks/useAPIClient.ts b/src/hooks/useAPIClient.ts index adb6d3f..b523d1f 100644 --- a/src/hooks/useAPIClient.ts +++ b/src/hooks/useAPIClient.ts @@ -8,7 +8,7 @@ const fetcher = (endpoint: string) => get(endpoint).then(response => response.da const hooks: any = {}; -const registerServiceHooks = <Item = any>(service: string) => { +const registerServiceHooks = <Item = any>(service: string): void => { const useList = (query = '', options = {}): Response<Item[]> => { return useSWR(`/${service}${query}`, fetcher, options); }; diff --git a/src/services/account/index.ts b/src/services/account/index.ts index 5b5eb10..7cc28b4 100644 --- a/src/services/account/index.ts +++ b/src/services/account/index.ts @@ -1,8 +1,10 @@ -const service = { +import { ServiceParams } from '../../containers/Service/ServiceContext'; + +const service: ServiceParams = { route: 'account', name: 'Рассчётный счёт', tableFields: [ - { key: 'date', label: 'Дата', transform: (date: string) => new Date(date).toLocaleDateString() }, + { key: 'date', label: 'Дата', transform: date => new Date(date).toLocaleDateString() }, { key: 'amount', label: 'Сумма' }, ], actions: [ @@ -11,7 +13,7 @@ const service = { route: '/transfers/upload', }, ], - rowLink: (item: any) => `/transfers?date=${item.date}`, + rowLink: item => `/transfers?date=${item.date}`, }; export default service; diff --git a/src/services/constants.ts b/src/services/constants.ts deleted file mode 100644 index 1461d9c..0000000 --- a/src/services/constants.ts +++ /dev/null @@ -1,11 +0,0 @@ -export const operationNames = { - in: 'Приход', - out: 'Расход', -}; - -export const waybillStatusNames = { - waiting: 'Ожидание', - executed: 'Проведена', - cancelled: 'Отменена', -}; - diff --git a/src/services/contractors/ContractorPanel.tsx b/src/services/contractors/ContractorPanel.tsx index ce94c48..a3650ef 100644 --- a/src/services/contractors/ContractorPanel.tsx +++ b/src/services/contractors/ContractorPanel.tsx @@ -1,13 +1,9 @@ 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'; -const ContractorPanel: React.FC<PanelProps> = ({ item, mutate }) => { - const history = useHistory(); - +const ContractorPanel: React.FC<PanelProps> = ({ item }) => { return ( <div className="lg:m-4 p-4 flex flex-col lg:pl-16 lg:border-l"> <span className="text-lg mb-10"> diff --git a/src/services/contractors/index.ts b/src/services/contractors/index.ts index 124e338..c9e71a1 100644 --- a/src/services/contractors/index.ts +++ b/src/services/contractors/index.ts @@ -1,7 +1,8 @@ import Form from './ContractorForm'; import Panel from './ContractorPanel'; +import { ServiceParams } from '../../containers/Service/ServiceContext'; -const service = { +const service: ServiceParams = { route: 'contractors', name: 'Контрагенты', nameSingular: 'Контрагент', diff --git a/src/services/index.ts b/src/services/index.ts index 7f559f2..9d3471b 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -12,9 +12,9 @@ const services = [ waybills, transfers, account, -] as ServiceParams[]; +]; -services.forEach((service: any) => registerServiceHooks(service.route)); +services.forEach(service => registerServiceHooks(service.route)); export default services; diff --git a/src/services/products/index.ts b/src/services/products/index.ts index 7e1509a..8d42c56 100644 --- a/src/services/products/index.ts +++ b/src/services/products/index.ts @@ -1,6 +1,7 @@ import Form from './ProductForm'; +import { ServiceParams } from '../../containers/Service/ServiceContext'; -const service = { +const service: ServiceParams = { route: 'products', name: 'Товары', nameSingular: 'Товар', diff --git a/src/services/transfers/TransfersUpload.tsx b/src/services/transfers/TransfersUpload.tsx index 1c81b08..18a33b8 100644 --- a/src/services/transfers/TransfersUpload.tsx +++ b/src/services/transfers/TransfersUpload.tsx @@ -3,7 +3,8 @@ import { useHistory } from 'react-router-dom'; import { Form, Formik } from 'formik'; import Button from '../../components/Button'; import Input from '../../components/Input'; -import Page, { Action } from '../../containers/Page'; +import Page from '../../containers/Page'; +import { Action } from '../../containers/Service/ServiceContext'; import { post } from '../../requests'; const TransfersUpload: React.FC = () => { diff --git a/src/services/transfers/index.ts b/src/services/transfers/index.ts index 09f6e04..51be7e3 100644 --- a/src/services/transfers/index.ts +++ b/src/services/transfers/index.ts @@ -1,16 +1,17 @@ import Form from './TransferForm'; import UploadPage from './TransfersUpload'; +import { transformOperation } from '../transforms'; +import { ServiceParams } from '../../containers/Service/ServiceContext'; -import { operationNames } from '../constants'; -const service = { +const service: ServiceParams = { route: 'transfers', name: 'Переводы', nameSingular: 'Перевод', tableFields: [ - { key: 'date', label: 'Дата', transform: (date: string) => new Date(date).toLocaleDateString() }, + { key: 'date', label: 'Дата', transform: date => new Date(date).toLocaleDateString() }, { key: 'contractor.name', label: 'Контрагент' }, - { key: 'operation', label: 'Операция', transform: (op: 'in' | 'out') => operationNames[op] }, + { key: 'operation', label: 'Операция', transform: transformOperation }, { key: 'amount', label: 'Сумма' }, ], actions: [ diff --git a/src/services/transforms.ts b/src/services/transforms.ts new file mode 100644 index 0000000..2eea46f --- /dev/null +++ b/src/services/transforms.ts @@ -0,0 +1,21 @@ +import _ from 'lodash'; +import { Field } from '../components/ListTable'; + +type Transformer = Field['transform']; + +const transformer = (hash: Record<string, string | JSX.Element>): Transformer => { + return value => _.get(hash, value, value); +}; + + +export const transformOperation = transformer({ + in: 'Приход', + out: 'Расход', +}); + +export const transformStatus = transformer({ + waiting: 'Ожидание', + executed: 'Проведена', + cancelled: 'Отменена', +}); + diff --git a/src/services/waybills/index.ts b/src/services/waybills/index.ts index f8a4be6..4e50f54 100644 --- a/src/services/waybills/index.ts +++ b/src/services/waybills/index.ts @@ -1,18 +1,15 @@ import Form from './WaybillForm'; import Panel from './WaybillPanel'; -import { waybillStatusNames, operationNames } from '../constants'; +import { transformOperation, transformStatus } from '../transforms'; +import { ServiceParams } from '../../containers/Service/ServiceContext'; -const service = { +const service: ServiceParams = { route: 'waybills', name: 'Накладные', nameSingular: 'Накладная', tableFields: [ - { - key: 'status', - label: 'Статус', - transform: (status: 'waiting' | 'executed' | 'cancelled') => waybillStatusNames[status], - }, - { key: 'operation', label: 'Операция', transform: (op: 'in' | 'out') => operationNames[op] }, + { key: 'status', label: 'Статус', transform: transformStatus }, + { key: 'operation', label: 'Операция', transform: transformOperation }, { key: 'total', label: 'Сумма' }, { key: 'contractor.name', label: 'Контрагент' }, ], |