Create hooks for comment api

This commit is contained in:
marzban-dev
2025-01-30 01:09:28 +03:30
parent 4d96254a37
commit 81e0a54e58
2 changed files with 59 additions and 0 deletions
@@ -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;
@@ -0,0 +1,29 @@
// imports
import { useQuery } from "@tanstack/vue-query";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
// types
export type GetCommentsResponse = ApiPaginated<UserComment>;
const useGetComments = (id: string | number | undefined, page: Ref<number>) => {
// state
const { $axios: axios } = useNuxtApp();
// methods
const handleGetComments = async () => {
const { data } = await axios.get<GetCommentsResponse>(`${API_ENDPOINTS.product.comments}/${id}`);
return data;
};
return useQuery({
queryKey: [QUERY_KEYS.comments, id, page],
queryFn: () => handleGetComments()
});
};
export default useGetComments;