From 97b2a4eb8af2f9af592bdba34d496c7f2f5514c3 Mon Sep 17 00:00:00 2001 From: "[dyad]" Date: Wed, 10 Sep 2025 13:24:45 +0200 Subject: [PATCH] [dyad] wrote 1 file(s) --- src/hooks/useDashboardData.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/hooks/useDashboardData.ts b/src/hooks/useDashboardData.ts index 35a729e..0e8a400 100644 --- a/src/hooks/useDashboardData.ts +++ b/src/hooks/useDashboardData.ts @@ -26,7 +26,7 @@ export function useDashboardData() { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const fetchDashboardStats = useCallback(async () => { + const fetchDashboardStats = useCallback(async (abortSignal: AbortSignal) => { setLoading(true); setError(null); try { @@ -36,6 +36,11 @@ export function useDashboardData() { await pb.admins.authWithPassword('david@wfsales.co.za', '1234567890'); console.log('Re-authenticated as admin successfully.'); } catch (authError: any) { + // Check if the error is due to abortion + if (authError.name === 'AbortError') { + console.log('Authentication request aborted.'); + return; + } console.error('Failed to re-authenticate as admin:', authError); setError(`Authentication failed: ${authError.message || authError.toString()}`); setLoading(false); @@ -46,6 +51,7 @@ export function useDashboardData() { // Fetch all stock items const stockRecords = await pb.collection('stock_items').getFullList({ sort: '-created', + signal: abortSignal, // Pass the abort signal }); let calculatedTotalStockValue = 0; @@ -64,6 +70,7 @@ export function useDashboardData() { const thirtyDaysAgo = subDays(new Date(), 30).toISOString(); const bookOutRecordsLast30Days = await pb.collection('book_out_records').getFullList({ filter: `date_booked_out >= "${thirtyDaysAgo}"`, + signal: abortSignal, // Pass the abort signal }); let calculatedStockOutLast30Days = 0; @@ -112,6 +119,7 @@ export function useDashboardData() { // Process all book out records for 'stockOut' const allBookOutRecords = await pb.collection('book_out_records').getFullList({ sort: '-created', + signal: abortSignal, // Pass the abort signal }); allBookOutRecords.forEach((record: any) => { @@ -138,6 +146,11 @@ export function useDashboardData() { setMonthlyActivityData(sortedMonthlyData); } catch (err: any) { + // Check if the error is due to abortion, if so, ignore it + if (err.name === 'AbortError') { + console.log('Dashboard data fetch aborted.'); + return; + } console.error("Failed to fetch dashboard data:", err); setError(`Failed to load dashboard data: ${err.message || err.toString()}`); setStats({ @@ -153,7 +166,12 @@ export function useDashboardData() { }, []); useEffect(() => { - fetchDashboardStats(); + const abortController = new AbortController(); + fetchDashboardStats(abortController.signal); + + return () => { + abortController.abort(); // Abort any pending requests on unmount or re-run + }; }, [fetchDashboardStats]); return { stats, monthlyActivityData, loading, error, refresh: fetchDashboardStats };