import { Star, X, Reply } from "lucide-react"; import { useState } from "react"; import { Button } from "~/components/ui/button"; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle, } from "~/components/ui/drawer"; import AppTextArea from "~/components/AppTextArea"; import { useServiceCreateComment } from "~/requestHandler/use-comments-hooks"; import { useRootData } from "~/hooks/use-root-data"; import { useToast } from "~/hooks/use-toast"; import { useIsOwnerProduct } from "~/requestHandler/use-product-hooks"; export interface ReviewProps { id?: string; productId: string; name: string; rating: number; date: string; comment: string; sellerReply?: { date: string; comment: string; }; isLast: boolean; } export const Review = ({ id, productId, name, rating, isLast, date, comment, sellerReply, }: ReviewProps) => { const [isReplyDrawerOpen, setIsReplyDrawerOpen] = useState(false); const [replyComment, setReplyComment] = useState(""); const [error, setError] = useState(null); const { toast } = useToast(); const { user } = useRootData(); const createCommentMutation = useServiceCreateComment(); const { data: isOwnerProduct } = useIsOwnerProduct(productId); const isOwner = isOwnerProduct?.isOwner; const authToken = user?.access_token ? user.access_token.replace(/^"|"$/g, "") : ""; const handleReplySubmit = async () => { // Check if user is authenticated if (!authToken) { setError("لطفا ابتدا وارد حساب کاربری خود شوید."); return; } // Form validation if (!replyComment || replyComment.trim().length < 1) { setError("لطفا پاسخ خود را وارد کنید."); return; } if (!id) { setError("خطا در شناسایی نظر."); return; } setError(null); try { await createCommentMutation.mutateAsync({ product: productId, comment: replyComment, parent: id, }); // Reset form and close drawer on success setReplyComment(""); setIsReplyDrawerOpen(false); // Show success toast toast({ title: "پاسخ شما با موفقیت ثبت شد", description: "پاسخ شما بعد از بررسی به زودی اضافه خواهد شد.", variant: "default", duration: 3000, }); } catch { setError("خطا در ثبت پاسخ. لطفا مجددا تلاش کنید."); } }; return ( <>

{name}

{rating}

{date}

{/* Reply button */} {isOwner && (
)}

{comment}

{sellerReply && (

پاسخ فروشنده

{sellerReply.date}

{sellerReply.comment}

)}
{/* Reply Drawer */}
setIsReplyDrawerOpen(false)} /> پاسخ شما
{/* Original comment display */}

{name}

{rating}

{date}

{comment}

{/* Reply input */} setReplyComment(e.target.value)} inputPlaceholder="پاسخ خود را در این قسمت وارد کنید..." inputDir="rtl" /> {/* Error message */} {error && (
{error}
)} {/* Submit button */}
); };