All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 16m53s
- 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.
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
// src/hooks/queries/useSuggestedCorrectionsQuery.ts
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { getSuggestedCorrections } from '../../services/apiClient';
|
|
import { queryKeys } from '../../config/queryKeys';
|
|
import type { SuggestedCorrection } from '../../types';
|
|
|
|
/**
|
|
* Query hook for fetching user-submitted corrections (admin feature).
|
|
*
|
|
* Returns a list of pending corrections that need admin review/approval.
|
|
*
|
|
* Uses TanStack Query for automatic caching and refetching (ADR-0005 Phase 5).
|
|
*
|
|
* @returns TanStack Query result with SuggestedCorrection[] data
|
|
*/
|
|
export const useSuggestedCorrectionsQuery = () => {
|
|
return useQuery({
|
|
queryKey: queryKeys.suggestedCorrections(),
|
|
queryFn: async (): Promise<SuggestedCorrection[]> => {
|
|
const response = await getSuggestedCorrections();
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({
|
|
message: `Request failed with status ${response.status}`,
|
|
}));
|
|
throw new Error(error.message || 'Failed to fetch suggested corrections');
|
|
}
|
|
|
|
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;
|
|
},
|
|
staleTime: 1000 * 60, // 1 minute - corrections change moderately
|
|
});
|
|
};
|