1 | import { useMemo } from 'react';
|
---|
2 | // types
|
---|
3 | import { Invoice } from 'mvpmasters-shared';
|
---|
4 | // db
|
---|
5 | import useSWR from 'swr';
|
---|
6 | import { endpoints, fetcher } from 'src/utils/axios';
|
---|
7 |
|
---|
8 | interface InvoiceFilters {
|
---|
9 | name?: string;
|
---|
10 | service?: string[];
|
---|
11 | status?: string;
|
---|
12 | startDate?: string | null;
|
---|
13 | endDate?: string | null;
|
---|
14 | }
|
---|
15 |
|
---|
16 | export function useGetInvoices(params: InvoiceFilters = {}) {
|
---|
17 | const collectionName = endpoints.invoice;
|
---|
18 |
|
---|
19 | const searchParams = new URLSearchParams();
|
---|
20 | if (params.name) searchParams.set('name', params.name);
|
---|
21 | if (params.status) searchParams.set('status', params.status);
|
---|
22 | if (params.startDate) searchParams.set('startDate', params.startDate);
|
---|
23 | if (params.endDate) searchParams.set('endDate', params.endDate);
|
---|
24 | if (params.service?.length) {
|
---|
25 | params.service.forEach((service) => searchParams.append('service', service));
|
---|
26 | }
|
---|
27 |
|
---|
28 | const queryString = searchParams.toString();
|
---|
29 | const endpoint = queryString ? `${collectionName}?${queryString}` : collectionName;
|
---|
30 |
|
---|
31 | const { data, isLoading, error, isValidating } = useSWR(
|
---|
32 | endpoint,
|
---|
33 | () => fetcher<Invoice[]>(endpoint),
|
---|
34 | {
|
---|
35 | revalidateOnFocus: false,
|
---|
36 | }
|
---|
37 | );
|
---|
38 |
|
---|
39 | const memoizedValue = useMemo(
|
---|
40 | () => ({
|
---|
41 | invoices: data || [],
|
---|
42 | invoicesLoading: isLoading,
|
---|
43 | invoicesError: error,
|
---|
44 | invoicesValidating: isValidating,
|
---|
45 | invoicesEmpty: !isLoading && !data?.length,
|
---|
46 | }),
|
---|
47 | [data, error, isLoading, isValidating]
|
---|
48 | );
|
---|
49 |
|
---|
50 | return memoizedValue;
|
---|
51 | }
|
---|
52 |
|
---|
53 | // export function useGetInvoice({ id }: { id: string }) {
|
---|
54 | // const collectionName = collections.invoice;
|
---|
55 |
|
---|
56 | // const { data, isLoading, error, isValidating } = useSWR(
|
---|
57 | // [collectionName, id],
|
---|
58 | // () => documentFetcher<Invoice>(collectionName, id),
|
---|
59 | // {
|
---|
60 | // revalidateOnFocus: false,
|
---|
61 | // }
|
---|
62 | // );
|
---|
63 |
|
---|
64 | // const memoizedValue = useMemo(
|
---|
65 | // () => ({
|
---|
66 | // currentInvoice: data || null,
|
---|
67 | // currentInvoiceLoading: isLoading,
|
---|
68 | // currentInvoiceError: error,
|
---|
69 | // currentInvoiceValidating: isValidating,
|
---|
70 | // currentInvoiceEmpty: !isLoading && !data,
|
---|
71 | // }),
|
---|
72 | // [data, error, isLoading, isValidating]
|
---|
73 | // );
|
---|
74 |
|
---|
75 | // return memoizedValue;
|
---|
76 | // }
|
---|