Files
flyer-crawler.projectium.com/src/hooks/queries/useFlyerItemsForFlyersQuery.ts
Torben Sorensen 3d91d59b9c
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 16m53s
refactor: update API response handling across multiple queries to ensure compliance with ADR-028
- Removed direct return of json.data in favor of structured error handling.
- Implemented checks for success and data array in useActivityLogQuery, useBestSalePricesQuery, useBrandsQuery, useCategoriesQuery, useFlyerItemsForFlyersQuery, useFlyerItemsQuery, useFlyersQuery, useLeaderboardQuery, useMasterItemsQuery, usePriceHistoryQuery, useShoppingListsQuery, useSuggestedCorrectionsQuery, and useWatchedItemsQuery.
- Updated unit tests to reflect changes in expected behavior when API response does not conform to the expected structure.
- Updated package.json to use the latest version of @sentry/vite-plugin.
- Adjusted vite.config.ts for local development SSL configuration.
- Added self-signed SSL certificate and key for local development.
2026-01-17 21:45:51 -08:00

53 lines
1.8 KiB
TypeScript

// src/hooks/queries/useFlyerItemsForFlyersQuery.ts
import { useQuery } from '@tanstack/react-query';
import { fetchFlyerItemsForFlyers } from '../../services/apiClient';
import { queryKeys } from '../../config/queryKeys';
import type { FlyerItem } from '../../types';
/**
* Query hook for fetching flyer items for multiple flyers.
*
* This is used to get all items from currently valid flyers,
* which are then filtered against the user's watched items to find deals.
*
* @param flyerIds - Array of flyer IDs to fetch items for
* @param enabled - Whether the query should run
* @returns Query result with flyer items data
*
* @example
* ```tsx
* const { data: items } = useFlyerItemsForFlyersQuery(validFlyerIds, validFlyerIds.length > 0);
* ```
*/
export const useFlyerItemsForFlyersQuery = (flyerIds: number[], enabled: boolean = true) => {
return useQuery({
// Include flyerIds in the key so cache is per-set of flyers
queryKey: queryKeys.flyerItemsBatch(flyerIds),
queryFn: async (): Promise<FlyerItem[]> => {
if (flyerIds.length === 0) {
return [];
}
const response = await fetchFlyerItemsForFlyers(flyerIds);
if (!response.ok) {
const error = await response.json().catch(() => ({
message: `Request failed with status ${response.status}`,
}));
throw new Error(error.message || 'Failed to fetch flyer items');
}
const json = await response.json();
// ADR-028: API returns { success: true, data: [...] }
// If success is false or data is not an array, return empty array to prevent .map() errors
if (!json.success || !Array.isArray(json.data)) {
return [];
}
return json.data;
},
enabled: enabled && flyerIds.length > 0,
// Flyer items don't change frequently once created
staleTime: 1000 * 60 * 5, // 5 minutes
});
};