39 lines
878 B
TypeScript
39 lines
878 B
TypeScript
// imports
|
|
|
|
import { useQuery } from "@tanstack/vue-query";
|
|
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
|
|
|
|
// types
|
|
|
|
export type GetArticlesResponse = ApiPaginated<UserComment>;
|
|
|
|
const useGetArticles = (
|
|
page: Ref<number>,
|
|
search: Ref<string>
|
|
) => {
|
|
|
|
// state
|
|
|
|
const { $axios: axios } = useNuxtApp();
|
|
|
|
// methods
|
|
|
|
const handleGetArticles = async () => {
|
|
const { data } = await axios.get<GetArticlesResponse>(`${API_ENDPOINTS.blog.articles}`, {
|
|
params: {
|
|
offset: (page.value * 10) - 10,
|
|
limit: 10,
|
|
search: search.value.length > 0 ? search.value : undefined,
|
|
}
|
|
});
|
|
return data;
|
|
};
|
|
|
|
return useQuery({
|
|
queryKey: [QUERY_KEYS.articles, page, search],
|
|
queryFn: () => handleGetArticles()
|
|
});
|
|
};
|
|
|
|
export default useGetArticles;
|