From d81c12c358b0bf076a8b18e24b5e2e37b7238511 Mon Sep 17 00:00:00 2001 From: marzban-dev Date: Thu, 30 Jan 2025 02:43:35 +0330 Subject: [PATCH] Add articles composable api --- .../composables/api/blog/useGetArticles.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 frontend/composables/api/blog/useGetArticles.ts diff --git a/frontend/composables/api/blog/useGetArticles.ts b/frontend/composables/api/blog/useGetArticles.ts new file mode 100644 index 0000000..0ccfde0 --- /dev/null +++ b/frontend/composables/api/blog/useGetArticles.ts @@ -0,0 +1,38 @@ +// imports + +import { useQuery } from "@tanstack/vue-query"; +import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; + +// types + +export type GetArticlesResponse = ApiPaginated; + +const useGetArticles = ( + page: Ref, + search: Ref +) => { + + // state + + const { $axios: axios } = useNuxtApp(); + + // methods + + const handleGetArticles = async () => { + const { data } = await axios.get(`${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;