Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 43s
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
// src/hooks/queries/useMasterItemsQuery.ts
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import * as apiClient from '../../services/apiClient';
|
|
import type { MasterGroceryItem } from '../../types';
|
|
|
|
/**
|
|
* Query hook for fetching all master grocery items.
|
|
*
|
|
* Master items are the canonical list of grocery items that users can watch
|
|
* and that flyer items are mapped to. This data changes infrequently, so it's
|
|
* cached with a longer stale time.
|
|
*
|
|
* @returns Query result with master items data, loading state, and error state
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const { data: masterItems, isLoading, error } = useMasterItemsQuery();
|
|
* ```
|
|
*/
|
|
export const useMasterItemsQuery = () => {
|
|
return useQuery({
|
|
queryKey: ['master-items'],
|
|
queryFn: async (): Promise<MasterGroceryItem[]> => {
|
|
const response = await apiClient.fetchMasterItems();
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({
|
|
message: `Request failed with status ${response.status}`,
|
|
}));
|
|
throw new Error(error.message || 'Failed to fetch master items');
|
|
}
|
|
|
|
return response.json();
|
|
},
|
|
// Master items change infrequently, keep data fresh for 10 minutes
|
|
staleTime: 1000 * 60 * 10,
|
|
// Cache for 30 minutes
|
|
gcTime: 1000 * 60 * 30,
|
|
});
|
|
};
|