Files
flyer-crawler.projectium.com/src/hooks/queries/useShoppingListsQuery.ts
Torben Sorensen 2b517683fd
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m2s
progress enforcing adr-0005
2026-01-08 21:50:21 -08:00

40 lines
1.3 KiB
TypeScript

// src/hooks/queries/useShoppingListsQuery.ts
import { useQuery } from '@tanstack/react-query';
import * as apiClient from '../../services/apiClient';
import type { ShoppingList } from '../../types';
/**
* Query hook for fetching the user's shopping lists.
*
* This hook is automatically disabled when the user is not authenticated,
* and the cached data is invalidated when the user logs out.
*
* @param enabled - Whether the query should run (typically based on auth status)
* @returns Query result with shopping lists data, loading state, and error state
*
* @example
* ```tsx
* const { data: shoppingLists, isLoading, error } = useShoppingListsQuery(!!user);
* ```
*/
export const useShoppingListsQuery = (enabled: boolean) => {
return useQuery({
queryKey: ['shopping-lists'],
queryFn: async (): Promise<ShoppingList[]> => {
const response = await apiClient.fetchShoppingLists();
if (!response.ok) {
const error = await response.json().catch(() => ({
message: `Request failed with status ${response.status}`,
}));
throw new Error(error.message || 'Failed to fetch shopping lists');
}
return response.json();
},
enabled,
// Keep data fresh for 1 minute since users actively manage shopping lists
staleTime: 1000 * 60,
});
};