|
1 | 1 | /** |
2 | | - * Custom hook to fetch and manage vendor risks data. |
| 2 | + * Custom hook to fetch and manage vendor risks data using TanStack Query. |
3 | 3 | * |
4 | 4 | * @param {Object} params - The parameters object. |
5 | 5 | * @param {string} [params.projectId] - The optional project ID to filter vendor risks. |
|
11 | 11 | * - `vendorRisksSummary` {Object} - The summary of vendor risks categorized by risk levels. |
12 | 12 | * - `refetchVendorRisks` {Function} - Function to manually refetch vendor risks data. |
13 | 13 | */ |
14 | | -import { useEffect, useState, useCallback, useMemo } from "react"; |
| 14 | +import { useQuery } from "@tanstack/react-query"; |
| 15 | +import { useMemo } from "react"; |
15 | 16 | import { convertToCamelCaseRiskKey } from "../tools/stringUtil"; |
16 | 17 | import { VendorRisk } from "../../domain/types/VendorRisk"; |
17 | 18 | import { getAllVendorRisks } from "../repository/vendorRisk.repository"; |
18 | 19 |
|
19 | | -const useVendorRisks = ({ projectId, vendorId }: { projectId?: string | null; vendorId?: string | null }) => { |
20 | | - const [vendorRisks, setVendorRisks] = useState<VendorRisk[]>([]); |
21 | | - const [loadingVendorRisks, setLoadingVendorRisks] = useState<boolean>(true); |
22 | | - const [error, setError] = useState<string | boolean>(false); |
| 20 | +// Query keys for vendor risks |
| 21 | +export const vendorRiskQueryKeys = { |
| 22 | + all: ['vendorRisks'] as const, |
| 23 | + lists: () => [...vendorRiskQueryKeys.all, 'list'] as const, |
| 24 | + list: (filters: { projectId?: string | null; vendorId?: string | null }) => |
| 25 | + [...vendorRiskQueryKeys.lists(), filters] as const, |
| 26 | +}; |
23 | 27 |
|
24 | | - const fetchVendorRisks = useCallback(async () => { |
25 | | - setLoadingVendorRisks(true); |
26 | | - try { |
| 28 | +const useVendorRisks = ({ projectId, vendorId }: { projectId?: string | null; vendorId?: string | null }) => { |
| 29 | + const { |
| 30 | + data: vendorRisks = [], |
| 31 | + isLoading: loadingVendorRisks, |
| 32 | + error, |
| 33 | + refetch: refetchVendorRisks, |
| 34 | + } = useQuery({ |
| 35 | + queryKey: vendorRiskQueryKeys.list({ projectId, vendorId }), |
| 36 | + queryFn: async (): Promise<VendorRisk[]> => { |
27 | 37 | const response = await getAllVendorRisks(); |
28 | | - if (response?.data) { |
29 | | - setVendorRisks(response?.data); |
30 | | - } |
31 | | - } catch (err) { |
32 | | - if (err instanceof Error) { |
33 | | - setError(`Request failed: ${err.message}`); |
34 | | - } else { |
35 | | - setError(`Request failed`); |
36 | | - } |
37 | | - } finally { |
38 | | - setLoadingVendorRisks(false); |
39 | | - } |
40 | | - }, []); |
41 | | - |
42 | | - useEffect(() => { |
43 | | - fetchVendorRisks(); |
44 | | - }, [fetchVendorRisks]); |
| 38 | + return response?.data || []; |
| 39 | + }, |
| 40 | + staleTime: 5 * 60 * 1000, // 5 minutes |
| 41 | + gcTime: 10 * 60 * 1000, // 10 minutes |
| 42 | + }); |
45 | 43 |
|
46 | 44 | // Filter risks based on projectId and vendorId |
47 | 45 | const filteredVendorRisks = useMemo(() => { |
@@ -72,10 +70,10 @@ const useVendorRisks = ({ projectId, vendorId }: { projectId?: string | null; ve |
72 | 70 |
|
73 | 71 | return { |
74 | 72 | loadingVendorRisks, |
75 | | - error, |
| 73 | + error: error ? `Request failed: ${error.message}` : false, |
76 | 74 | vendorRisks: filteredVendorRisks, |
77 | 75 | vendorRisksSummary, |
78 | | - refetchVendorRisks: fetchVendorRisks, |
| 76 | + refetchVendorRisks, |
79 | 77 | }; |
80 | 78 | }; |
81 | 79 |
|
|
0 commit comments