From 81e0a54e5805e659fb152fee3ce6481eea42479f Mon Sep 17 00:00:00 2001 From: marzban-dev Date: Thu, 30 Jan 2025 01:09:28 +0330 Subject: [PATCH] Create hooks for comment api --- .../api/product/useCreateComment.ts | 30 +++++++++++++++++++ .../composables/api/product/useGetComments.ts | 29 ++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 frontend/composables/api/product/useCreateComment.ts create mode 100644 frontend/composables/api/product/useGetComments.ts diff --git a/frontend/composables/api/product/useCreateComment.ts b/frontend/composables/api/product/useCreateComment.ts new file mode 100644 index 0000000..9443850 --- /dev/null +++ b/frontend/composables/api/product/useCreateComment.ts @@ -0,0 +1,30 @@ +// imports + +import { useMutation } from "@tanstack/vue-query"; +import { API_ENDPOINTS } from "~/constants"; + +// types + +export type CreateCommentRequest = { + content: string +}; + +const useCreateComment = (id: number | string | undefined) => { + + // state + + const { $axios: axios } = useNuxtApp(); + + // method + + const handleCreateComment = async (variables: CreateCommentRequest) => { + const { data } = await axios.post(`${API_ENDPOINTS.product.create_comment}/${id}`, variables); + return data; + }; + + return useMutation({ + mutationFn: (variables: CreateCommentRequest) => handleCreateComment(variables) + }); +}; + +export default useCreateComment; diff --git a/frontend/composables/api/product/useGetComments.ts b/frontend/composables/api/product/useGetComments.ts new file mode 100644 index 0000000..ad74c20 --- /dev/null +++ b/frontend/composables/api/product/useGetComments.ts @@ -0,0 +1,29 @@ +// imports + +import { useQuery } from "@tanstack/vue-query"; +import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; + +// types + +export type GetCommentsResponse = ApiPaginated; + +const useGetComments = (id: string | number | undefined, page: Ref) => { + + // state + + const { $axios: axios } = useNuxtApp(); + + // methods + + const handleGetComments = async () => { + const { data } = await axios.get(`${API_ENDPOINTS.product.comments}/${id}`); + return data; + }; + + return useQuery({ + queryKey: [QUERY_KEYS.comments, id, page], + queryFn: () => handleGetComments() + }); +}; + +export default useGetComments;