| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- "use client";
- import { useMutation } from "@apollo/client/react";
- import { CREATE_PRODUCT_REVIEW } from "@/graphql";
- import { useCustomToast } from "./useToast";
- import type { CreateProductReviewInput } from "@/types/review";
- import {handleApolloBusinessError} from "@/lib/ApolloErrorHandler";
- export function useProductReview() {
- const { showToast } = useCustomToast();
- const [mutateAsync, { loading: isLoading, error }] = useMutation(CREATE_PRODUCT_REVIEW, {
- onCompleted: (response) => {
- const responseData = response?.createProductReview;
- if (responseData) {
- showToast("Product review created successfully", "success");
- }
- },
- onError: (error) => {
- handleApolloBusinessError(error,(err) => {
- showToast(err.message, "danger");
- });
- },
- });
- const createProductReview = async (input: CreateProductReviewInput) => {
- return await mutateAsync({
- variables: {
- input: {
- productId: input.productId,
- title: input.title,
- comment: input.comment,
- rating: input.rating,
- name: input.name,
- email: input.email,
- attachments: input.attachments || "",
- },
- },
- });
- };
- return {
- createProductReview,
- isLoading,
- error
- };
- }
|