import { type FC, useEffect, useState } from 'react' import { MessageText1 } from 'iconsax-react' import Textarea from '@/components/Textarea' import Button from '@/components/Button' import { formatOptionalDate } from '@/helpers/func' import { toast } from 'react-toastify' import { extractErrorMessage } from '@/config/func' import { useDeleteReviewReply, useReplyToReview } from '../hooks/useReviewData' import type { ReviewReply } from '../types/Types' interface ReviewReplySectionProps { reviewId: string replies: ReviewReply[] } const ReviewReplySection: FC = ({ reviewId, replies }) => { const existingReply = replies[0] const [comment, setComment] = useState(existingReply?.comment || '') const replyMutation = useReplyToReview() const deleteMutation = useDeleteReviewReply() useEffect(() => { setComment(existingReply?.comment || '') }, [existingReply?.comment, existingReply?.id]) const hasChanges = comment.trim() !== (existingReply?.comment || '').trim() const canSave = comment.trim().length > 0 && hasChanges const handleSave = () => { if (!canSave) return replyMutation.mutate( { id: reviewId, comment: comment.trim() }, { onSuccess: () => { toast.success(existingReply ? 'پاسخ با موفقیت به‌روزرسانی شد' : 'پاسخ با موفقیت ثبت شد') }, onError: (error) => { toast.error(extractErrorMessage(error)) }, } ) } const handleDelete = () => { deleteMutation.mutate(reviewId, { onSuccess: () => { setComment('') toast.success('پاسخ حذف شد') }, onError: (error) => { toast.error(extractErrorMessage(error)) }, }) } return (
پاسخ رستوران {replies.length > 0 && ( ({replies.length}) )}
{replies.length > 0 && (
{replies.map((reply) => (
پاسخ {reply.createdAt && ( {formatOptionalDate(reply.createdAt, { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', })} )}

{reply.comment || '-'}

))}
)}