initial commit
This commit is contained in:
59
apps/frontend/src/lib/queries.ts
Normal file
59
apps/frontend/src/lib/queries.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { getQuotes, fetchQuotes, getQuoteHistory, getDailyMinMax, getDailyQuotes } from "./api";
|
||||
|
||||
export const quoteKeys = {
|
||||
all: ["quotes"] as const,
|
||||
history: (type: string, startDate: string, endDate: string) =>
|
||||
["quotes", type, "history", startDate, endDate] as const,
|
||||
minMax: (type: string, startDate: string, endDate: string) =>
|
||||
["quotes", type, "minMax", startDate, endDate] as const,
|
||||
daily: (type: string, startDate: string, endDate: string) =>
|
||||
["quotes", type, "daily", startDate, endDate] as const,
|
||||
};
|
||||
|
||||
export function useQuotes() {
|
||||
return useQuery({
|
||||
queryKey: quoteKeys.all,
|
||||
queryFn: getQuotes,
|
||||
staleTime: 30_000,
|
||||
refetchInterval: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useFetchQuotes() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: fetchQuotes,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: quoteKeys.all });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useQuoteHistory(type: string, startDate: string, endDate: string) {
|
||||
return useQuery({
|
||||
queryKey: quoteKeys.history(type, startDate, endDate),
|
||||
queryFn: () => getQuoteHistory(type, startDate, endDate),
|
||||
staleTime: 30_000,
|
||||
refetchInterval: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useDailyMinMax(type: string, startDate: string, endDate: string) {
|
||||
return useQuery({
|
||||
queryKey: quoteKeys.minMax(type, startDate, endDate),
|
||||
queryFn: () => getDailyMinMax(type, startDate, endDate),
|
||||
staleTime: 30_000,
|
||||
refetchInterval: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useDailyQuotes(type: string, startDate: string, endDate: string) {
|
||||
return useQuery({
|
||||
queryKey: quoteKeys.daily(type, startDate, endDate),
|
||||
queryFn: () => getDailyQuotes(type, startDate, endDate),
|
||||
staleTime: 30_000,
|
||||
refetchInterval: 60_000,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user