diff options
Diffstat (limited to 'src/hooks/useAPIClient.ts')
-rw-r--r-- | src/hooks/useAPIClient.ts | 22 |
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; |