All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 13m46s
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
// src/hooks/queries/useWatchedItemsQuery.ts
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import * as apiClient from '../../services/apiClient';
|
|
import { queryKeys } from '../../config/queryKeys';
|
|
import type { MasterGroceryItem } from '../../types';
|
|
|
|
/**
|
|
* Query hook for fetching the user's watched items.
|
|
*
|
|
* 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 watched items data, loading state, and error state
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const { data: watchedItems, isLoading, error } = useWatchedItemsQuery(!!user);
|
|
* ```
|
|
*/
|
|
export const useWatchedItemsQuery = (enabled: boolean) => {
|
|
return useQuery({
|
|
queryKey: queryKeys.watchedItems(),
|
|
queryFn: async (): Promise<MasterGroceryItem[]> => {
|
|
const response = await apiClient.fetchWatchedItems();
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({
|
|
message: `Request failed with status ${response.status}`,
|
|
}));
|
|
throw new Error(error.message || 'Failed to fetch watched items');
|
|
}
|
|
|
|
return response.json();
|
|
},
|
|
enabled,
|
|
// Keep data fresh for 1 minute since users actively manage watched items
|
|
staleTime: 1000 * 60,
|
|
});
|
|
};
|