This commit is contained in:
39
src/hooks/queries/useBestSalePricesQuery.ts
Normal file
39
src/hooks/queries/useBestSalePricesQuery.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
// src/hooks/queries/useBestSalePricesQuery.ts
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { fetchBestSalePrices } from '../../services/apiClient';
|
||||
import type { WatchedItemDeal } from '../../types';
|
||||
|
||||
/**
|
||||
* Query hook for fetching the best sale prices for the user's watched items.
|
||||
*
|
||||
* Returns deals where watched items are currently on sale, sorted by best price.
|
||||
* This data is user-specific and requires authentication.
|
||||
*
|
||||
* @param enabled - Whether the query should run (typically based on auth status)
|
||||
* @returns Query result with best sale prices data, loading state, and error state
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* const { data: deals, isLoading, error } = useBestSalePricesQuery(!!user);
|
||||
* ```
|
||||
*/
|
||||
export const useBestSalePricesQuery = (enabled: boolean = true) => {
|
||||
return useQuery({
|
||||
queryKey: ['best-sale-prices'],
|
||||
queryFn: async (): Promise<WatchedItemDeal[]> => {
|
||||
const response = await fetchBestSalePrices();
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({
|
||||
message: `Request failed with status ${response.status}`,
|
||||
}));
|
||||
throw new Error(error.message || 'Failed to fetch best sale prices');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
enabled,
|
||||
// Prices update when flyers change, keep fresh for 2 minutes
|
||||
staleTime: 1000 * 60 * 2,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user