server/assets/js/react/utils/queries.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import axios from "axios"
2023-12-06 02:46:27 +00:00
import { useQuery, useInfiniteQuery } from "@tanstack/react-query"
import type { Coach } from "../types/Coach"
import type { Language } from "../types/Language"
import { type SearchParams, toQueryParams } from "../types/SearchParams"
2023-12-06 02:46:27 +00:00
export const useCoachesInfiniteQuery = (searchParams: SearchParams) => {
const queryParams = toQueryParams(searchParams)
2023-12-06 02:46:27 +00:00
return useInfiniteQuery({
queryKey: ["coaches", queryParams],
2023-12-06 02:46:27 +00:00
queryFn: async ({ pageParam = 1 }) => {
const response = await axios.get<{ data: Coach[] }>("/api/coaches/", {
params: {
page_no: pageParam,
page_size: 15,
...queryParams,
2023-12-06 02:46:27 +00:00
},
})
return response.data.data
},
getNextPageParam: (lastPage, _allPages, lastPageParam) => {
2023-12-06 02:46:27 +00:00
if (lastPage.length === 0) {
return undefined
}
return lastPageParam + 1
},
initialPageParam: 1,
})
}
2023-12-06 02:46:27 +00:00
export const useLanguagesQuery = () => {
return useQuery({
2023-12-06 02:46:27 +00:00
queryKey: ["languages"],
queryFn: async () => {
const response = await axios.get<{ data: Language[] }>("/api/languages/")
return response.data.data
},
})
}