summaryrefslogtreecommitdiff
path: root/src/hooks
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-20 20:37:22 +0300
committereug-vs <eug-vs@keemail.me>2021-03-20 20:37:22 +0300
commit786dc90d89dbb2deaef0282301d88b1568d56a33 (patch)
tree703424338bcb9ec79e825c924509974612519fb9 /src/hooks
parent2d3ec1727b8ef51b8894116d353551134453744e (diff)
downloadcommercel-ui-786dc90d89dbb2deaef0282301d88b1568d56a33.tar.gz
feat: handle query params and add Account page
Diffstat (limited to 'src/hooks')
-rw-r--r--src/hooks/useAPIClient.ts22
1 files changed, 19 insertions, 3 deletions
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;