init
This commit is contained in:
37
frontend/hooks/use-weather.ts
Normal file
37
frontend/hooks/use-weather.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
'use client'
|
||||
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getCurrentWeather, getForecast, getHistorical } from '@/lib/api'
|
||||
import type { CurrentWeatherResponse, ForecastResponse, HistoricalResponse } from '@/lib/types'
|
||||
|
||||
const STALE_TIME = 5 * 60 * 1000 // 5 minutes
|
||||
const REFETCH_INTERVAL = 5 * 60 * 1000 // 5 minutes
|
||||
|
||||
export function useCurrentWeather(lat?: number, lon?: number) {
|
||||
return useQuery<CurrentWeatherResponse>({
|
||||
queryKey: ['weather', 'current', lat, lon],
|
||||
queryFn: () => getCurrentWeather(lat, lon),
|
||||
staleTime: STALE_TIME,
|
||||
refetchInterval: REFETCH_INTERVAL,
|
||||
refetchOnWindowFocus: true,
|
||||
})
|
||||
}
|
||||
|
||||
export function useForecast(lat?: number, lon?: number) {
|
||||
return useQuery<ForecastResponse>({
|
||||
queryKey: ['weather', 'forecast', lat, lon],
|
||||
queryFn: () => getForecast(lat, lon),
|
||||
staleTime: STALE_TIME,
|
||||
refetchInterval: REFETCH_INTERVAL,
|
||||
refetchOnWindowFocus: true,
|
||||
})
|
||||
}
|
||||
|
||||
export function useHistorical(date: string, lat?: number, lon?: number) {
|
||||
return useQuery<HistoricalResponse>({
|
||||
queryKey: ['weather', 'historical', date, lat, lon],
|
||||
queryFn: () => getHistorical(date, lat, lon),
|
||||
staleTime: STALE_TIME,
|
||||
enabled: !!date,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user