useProductReview.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use client";
  2. import { useMutation } from "@apollo/client/react";
  3. import { CREATE_PRODUCT_REVIEW } from "@/graphql";
  4. import { useCustomToast } from "./useToast";
  5. import type { CreateProductReviewInput } from "@/types/review";
  6. import {handleApolloBusinessError} from "@/lib/ApolloErrorHandler";
  7. export function useProductReview() {
  8. const { showToast } = useCustomToast();
  9. const [mutateAsync, { loading: isLoading, error }] = useMutation(CREATE_PRODUCT_REVIEW, {
  10. onCompleted: (response) => {
  11. const responseData = response?.createProductReview;
  12. if (responseData) {
  13. showToast("Product review created successfully", "success");
  14. }
  15. },
  16. onError: (error) => {
  17. handleApolloBusinessError(error,(err) => {
  18. showToast(err.message, "danger");
  19. });
  20. },
  21. });
  22. const createProductReview = async (input: CreateProductReviewInput) => {
  23. return await mutateAsync({
  24. variables: {
  25. input: {
  26. productId: input.productId,
  27. title: input.title,
  28. comment: input.comment,
  29. rating: input.rating,
  30. name: input.name,
  31. email: input.email,
  32. attachments: input.attachments || "",
  33. },
  34. },
  35. });
  36. };
  37. return {
  38. createProductReview,
  39. isLoading,
  40. error
  41. };
  42. }