91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { useAuthToken } from "../hooks/use-root-data";
|
|
import { createCommentReviewManagmentApi } from "~/utils/api-client-factory";
|
|
|
|
export function useServiceGetComments(page: number, productId: string) {
|
|
const response = useQuery({
|
|
retry: 1,
|
|
retryDelay: 3000,
|
|
refetchOnWindowFocus: false,
|
|
queryKey: ["getCommentsService", productId, page],
|
|
queryFn: async () => {
|
|
try {
|
|
const commentApi = createCommentReviewManagmentApi();
|
|
|
|
// The API doesn't directly support pagination in the typed client,
|
|
// so we need to make a custom fetch request
|
|
return await commentApi.apiCommentV1CommentList({
|
|
productId: productId,
|
|
page: page,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching comments:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
});
|
|
|
|
return { ...response };
|
|
}
|
|
|
|
export function useServiceCreateComment() {
|
|
const token = useAuthToken();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (commentData: {
|
|
product: string;
|
|
comment: string;
|
|
parent?: string | null;
|
|
}) => {
|
|
const commentApi = createCommentReviewManagmentApi(token);
|
|
return await commentApi.apiCommentV1CommentCreate({
|
|
data: commentData,
|
|
});
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["getCommentsService"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useServiceSubmitRating() {
|
|
const token = useAuthToken();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (ratingData: { product_id: string; rating: number }) => {
|
|
const commentApi = createCommentReviewManagmentApi(token);
|
|
return await commentApi.apiCommentV1ReviewCreate({
|
|
data: {
|
|
productId: ratingData.product_id,
|
|
rating: ratingData.rating,
|
|
},
|
|
});
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["getProductRatingService"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useServiceGetProductRating(productId: string) {
|
|
const response = useQuery({
|
|
retry: 1,
|
|
retryDelay: 3000,
|
|
refetchOnWindowFocus: false,
|
|
queryKey: ["getProductRatingService", productId],
|
|
queryFn: async () => {
|
|
try {
|
|
const commentApi = createCommentReviewManagmentApi();
|
|
return await commentApi.apiCommentV1ReviewList({
|
|
productId: productId,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching product rating:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
});
|
|
|
|
return { ...response };
|
|
}
|