main
Last change
on this file since 299af01 was 299af01, checked in by Naum Shapkarovski <naumshapkarovski@…>, 6 weeks ago |
chore
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[299af01] | 1 | import useSWR from 'swr';
|
---|
| 2 | import axios from 'src/utils/axios';
|
---|
| 3 | import { InvoiceStatus } from 'src/schemas';
|
---|
| 4 |
|
---|
| 5 | interface StatusTotals {
|
---|
| 6 | EUR: number;
|
---|
| 7 | USD: number;
|
---|
| 8 | }
|
---|
| 9 |
|
---|
| 10 | export interface AnalyticsResults {
|
---|
| 11 | total: StatusTotals;
|
---|
| 12 | processing: StatusTotals;
|
---|
| 13 | paid: StatusTotals;
|
---|
| 14 | pending: StatusTotals;
|
---|
| 15 | overdue: StatusTotals;
|
---|
| 16 | draft: StatusTotals;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | async function fetchAnalytics(startDate: Date) {
|
---|
| 20 | const { data } = await axios.get<AnalyticsResults>('/api/invoices/totals', {
|
---|
| 21 | params: { startDate: startDate.toISOString() },
|
---|
| 22 | withCredentials: true,
|
---|
| 23 | headers: {
|
---|
| 24 | 'Content-Type': 'application/json',
|
---|
| 25 | },
|
---|
| 26 | });
|
---|
| 27 | return data;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | export function useFetchAnalytics(startDate: Date | null) {
|
---|
| 31 | const { data, error, isLoading } = useSWR(
|
---|
| 32 | startDate ? ['invoiceTotals', startDate] : null,
|
---|
| 33 | () => startDate && fetchAnalytics(startDate),
|
---|
| 34 | {
|
---|
| 35 | revalidateOnFocus: false,
|
---|
| 36 | shouldRetryOnError: false,
|
---|
| 37 | }
|
---|
| 38 | );
|
---|
| 39 |
|
---|
| 40 | return {
|
---|
| 41 | analytics: data,
|
---|
| 42 | analyticsError: error,
|
---|
| 43 | isAnalyticsLoading: isLoading,
|
---|
| 44 | };
|
---|
| 45 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.