From cb5a9a8e21574c3568b01c36cf97cb0057981b02 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 7 Sep 2025 15:53:46 +0330 Subject: [PATCH] add question --- src/app/product/components/AddComment.tsx | 4 +- src/app/product/components/AddQuestion.tsx | 89 ++++++++++++++++++++++ src/app/product/components/Questions.tsx | 8 +- src/app/product/hooks/useProductData.ts | 6 ++ src/app/product/service/Service.ts | 10 ++- src/app/product/types/Types.ts | 5 ++ 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 src/app/product/components/AddQuestion.tsx diff --git a/src/app/product/components/AddComment.tsx b/src/app/product/components/AddComment.tsx index f7c5869..0bc6228 100644 --- a/src/app/product/components/AddComment.tsx +++ b/src/app/product/components/AddComment.tsx @@ -12,7 +12,7 @@ import Rate from 'rc-rate' import 'rc-rate/assets/index.css' import { Textarea } from '@/components/ui/textarea' import { useParams } from 'next/navigation' -import { useAddComment, useGetDetailProduct } from '../hooks/useProductData' +import { useAddComment } from '../hooks/useProductData' import { toast } from '@/components/Toast' import { extractErrorMessage } from '@/helpers/errorUtils' @@ -32,7 +32,6 @@ const AddComment: FC = (props) => { const [currentAdvantage, setCurrentAdvantage] = useState('') const [currentDisadvantage, setCurrentDisadvantage] = useState('') const [rating, setRating] = useState(0) - const { refetch } = useGetDetailProduct(id as string) const { mutate: addComment, isPending } = useAddComment() const validationSchema = yup.object({ @@ -100,7 +99,6 @@ const AddComment: FC = (props) => { addComment(data, { onSuccess: (res) => { toast(res?.results?.message, 'success') - refetch() close() }, onError: (error: Error) => { diff --git a/src/app/product/components/AddQuestion.tsx b/src/app/product/components/AddQuestion.tsx new file mode 100644 index 0000000..85e437b --- /dev/null +++ b/src/app/product/components/AddQuestion.tsx @@ -0,0 +1,89 @@ +import DefaulModal from '@/components/DefaulModal' +import { Button } from '@/components/ui/button' +import { FC } from 'react' +import { useForm } from 'react-hook-form' +import { AddQuestionProps } from '../types/Types' +import * as yup from 'yup' +import { yupResolver } from '@hookform/resolvers/yup' +import { Textarea } from '@/components/ui/textarea' +import { useParams } from 'next/navigation' +import { useAddQuestion } from '../hooks/useProductData' +import { toast } from '@/components/Toast' +import { extractErrorMessage } from '@/helpers/errorUtils' + +type Props = { + open: boolean, + close: () => void, +} + +const AddQuestion: FC = (props) => { + const { open, close } = props + const { id } = useParams() + const { mutate: addQuestion, isPending } = useAddQuestion() + + const validationSchema = yup.object({ + content: yup.string().required('متن سوال خود را وارد کنید'), + productId: yup.string().optional() + }) + + const { + register, + handleSubmit, + formState: { errors }, + reset + } = useForm({ + resolver: yupResolver(validationSchema), + defaultValues: { + content: '', + productId: id as string + } + }) + + const onSubmit = (data: AddQuestionProps) => { + addQuestion(data, { + onSuccess: (res) => { + toast(res?.results?.message, 'success') + reset() + close() + }, + onError: (error: Error) => { + toast(extractErrorMessage(error), 'error') + } + }) + } + + return ( + +
+ {/* متن سوال */} +
+ +