diff options
author | eug-vs <eug-vs@keemail.me> | 2021-03-20 20:37:22 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-03-20 20:37:22 +0300 |
commit | 786dc90d89dbb2deaef0282301d88b1568d56a33 (patch) | |
tree | 703424338bcb9ec79e825c924509974612519fb9 | |
parent | 2d3ec1727b8ef51b8894116d353551134453744e (diff) | |
download | commercel-ui-786dc90d89dbb2deaef0282301d88b1568d56a33.tar.gz |
feat: handle query params and add Account page
-rw-r--r-- | src/containers/Service/ServiceList.tsx | 25 | ||||
-rw-r--r-- | src/containers/TransfersUpload.tsx | 3 | ||||
-rw-r--r-- | src/hooks/useAPIClient.ts | 22 | ||||
-rw-r--r-- | src/services.js | 31 |
4 files changed, 58 insertions, 23 deletions
diff --git a/src/containers/Service/ServiceList.tsx b/src/containers/Service/ServiceList.tsx index 7d30c14..a0536b5 100644 --- a/src/containers/Service/ServiceList.tsx +++ b/src/containers/Service/ServiceList.tsx @@ -1,29 +1,28 @@ import React, { useContext } from 'react'; -import { useHistory } from 'react-router-dom'; +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 { data } = hooks[service.route].useList(); + const location = useLocation(); + const { data } = hooks[service.route].useList(location.search); - const actions = [ - ...( - service.actions?.map(action => ({ - ...action, - route: `/${service.route}/${action.route}`, - })) || [] - ), - { name: 'Добавить', route: `/${service.route}/add` }, - ]; + const actions = service.actions || [{ + name: 'Добавить', + route: `/${service.route}/add`, + }]; const handleRowClick = (index: number) => { const item = data && data[index]; - history.push(`/${service.route}/${item?._id}`); + const route = service.rowLink + ? service.rowLink(item) + : `/${service.route}/${item?._id}`; + + history.push(route); }; return ( diff --git a/src/containers/TransfersUpload.tsx b/src/containers/TransfersUpload.tsx index 8239ab9..bddd0ad 100644 --- a/src/containers/TransfersUpload.tsx +++ b/src/containers/TransfersUpload.tsx @@ -15,8 +15,7 @@ const TransfersUpload: React.FC = () => { reader.readAsDataURL(file); reader.onload = (e: any) => { const uri = e.target.result; - post('/uploads', { uri }) - .then(() => history.push('/transfers')); + post('/uploads', { uri }).then(history.goBack); }; }; diff --git a/src/hooks/useAPIClient.ts b/src/hooks/useAPIClient.ts index 8f3a077..1fa9896 100644 --- a/src/hooks/useAPIClient.ts +++ b/src/hooks/useAPIClient.ts @@ -8,12 +8,12 @@ type Response<T> = responseInterface<T, Error>; const fetcher = (endpoint: string) => get(endpoint).then(response => response.data); const createServiceHooks = <Item = any>(service: string) => { - const useList = (options = {}): Response<Item[]> => { - return useSWR(`/${service}`, fetcher, options); + const useList = (query = '', options = {}): Response<Item[]> => { + return useSWR(`/${service}${query}`, fetcher, options); }; const useItem = (_id: string): Response<Item> => { - const { data: preloadedItems } = useList({ revalidateOnMount: false }); + const { data: preloadedItems } = useList('', { revalidateOnMount: false }); const result = useSWR(_id && `/${service}/${_id}`, fetcher); if (!result.data && result.isValidating) { // If we are waiting for the first result, check if we can maybe @@ -54,4 +54,20 @@ const hooks = services.reduce((acc, { route }) => { }, {}); +hooks.account = { + useList: () => { + const { data: transfers } = useSWR('/transfers', fetcher); + const dates = _.groupBy(transfers, 'date'); + const dataUnsorted = _.map(dates, (dateTransfers, date) => ({ + date, + amount: _ + .sumBy(dateTransfers, transfer => transfer.amount * (transfer.operation === 'in' ? 1 : -1)) + .toFixed(1), + })); + const data = _.sortBy(dataUnsorted, 'date').reverse(); + return { data }; + }, +}; + + export default hooks as any; diff --git a/src/services.js b/src/services.js index 7187519..dfb166e 100644 --- a/src/services.js +++ b/src/services.js @@ -68,16 +68,37 @@ const services: ServiceParams[] = [ { key: 'operation', label: 'Операция', transform: op => operationNames[op] }, { key: 'amount', label: 'Сумма' }, ], - actions: [{ - name: 'Загрузить выписку', - route: 'upload', - variant: 'outlined', - }], + actions: [ + { + name: 'Загрузить выписку', + route: '/transfers/upload', + variant: 'outlined', + }, + { + name: 'Добавить', + route: '/transfers/add', + }, + ], default: { operation: 'in', records: [], }, }, + { + route: 'account', + name: 'Рассчётный счёт', + tableFields: [ + { key: 'date', label: 'Дата', transform: date => new Date(date).toLocaleDateString() }, + { key: 'amount', label: 'Сумма' }, + ], + actions: [ + { + name: 'Загрузить выписку', + route: '/transfers/upload', + }, + ], + rowLink: item => `/transfers?date=${item.date}`, + }, ]; export default services; |