From 816b172704fa689693555c92fdc0d0464ba8575b Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Wed, 29 Oct 2025 16:27:25 +0330 Subject: [PATCH] questions --- src/app/product/components/Questions.tsx | 94 +++++++----------------- src/app/product/service/Service.ts | 7 +- src/app/product/types/Types.ts | 77 +++++++++++++++++-- 3 files changed, 104 insertions(+), 74 deletions(-) diff --git a/src/app/product/components/Questions.tsx b/src/app/product/components/Questions.tsx index f22d2f3..77ae42b 100644 --- a/src/app/product/components/Questions.tsx +++ b/src/app/product/components/Questions.tsx @@ -2,31 +2,25 @@ import { FC, Fragment, useMemo, useState } from 'react' import { Button } from '@/components/ui/button' -import { ArrowDown2, MessageQuestion } from 'iconsax-react' -import { Product, Question } from '@/types/product.types' +import { MessageQuestion } from 'iconsax-react' +import { Product } from '@/types/product.types' import AddQuestion from './AddQuestion' -import { PRIMARY_COLOR } from '@/config/const' +import { useGetProductQuestion } from '../hooks/useProductData' +import moment from 'moment-jalaali' interface QuestionsProps { product: Product - questions?: Question[] } -const Questions: FC = ({ questions = [] }) => { - const [sort, setSort] = useState<'new' | 'answers'>('new') - const [expandedId, setExpandedId] = useState(null) +const Questions: FC = ({ product }) => { + const { data: questionsData, isLoading } = useGetProductQuestion(product._id) const [open, setOpen] = useState(false) - const sorted = useMemo(() => { - if (sort === 'new') { - return [...questions].sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()) - } - return [...questions].sort((a, b) => b.answers.length - a.answers.length) - }, [sort, questions]) - const formatDate = (dateString: string) => { - const date = new Date(dateString) - return date.toLocaleDateString('fa-IR') - } + const sorted = useMemo(() => { + const apiQuestions = questionsData?.results?.questions || [] + if (!apiQuestions.length) return [] + return [...apiQuestions].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) + }, [questionsData?.results?.questions]) return (
@@ -37,30 +31,12 @@ const Questions: FC = ({ questions = [] }) => {
-
- مرتب سازی بر اساس: -
- - -
-
-
- {sorted.length > 0 ? ( + {isLoading ? ( +
+ در حال بارگذاری... +
+ ) : sorted.length > 0 ? ( sorted.map((question) => (
@@ -70,40 +46,24 @@ const Questions: FC = ({ questions = [] }) => {
-
- {question.question} +
+ {question.content} +
+ {moment(question.createdAt, 'jYYYY/jMM/jDD HH:mm:ss').format('jYYYY/jMM/jD')} +
-
- {question.user.name} • {formatDate(question.date)} -
- {question.answers.length === 0 && ( -
ثبت پاسخ
- )} - {question.answers.length > 0 && ( -
- {(expandedId === question._id ? question.answers : question.answers.slice(0, 1)).map((answer) => ( -
-
پاسخ
-
- {answer.answer} -
+ {question.answers && question.answers.length > 0 && ( +
+ {question.answers.map((answer, index) => ( +
+ پاسخ: + {answer.content}
))} - - {question.answers.length > 1 && ( - - )}
)}
diff --git a/src/app/product/service/Service.ts b/src/app/product/service/Service.ts index d61b111..620da37 100644 --- a/src/app/product/service/Service.ts +++ b/src/app/product/service/Service.ts @@ -3,6 +3,7 @@ import { ProductDetailResponse } from "@/types/product.types"; import { AddCommentProps, AddQuestionProps, + AddQuestionResponse, SubmitReportProps, PriceHistoryResponse, ProductQuestionsResponse, @@ -24,7 +25,9 @@ export const addComment = async (params: AddCommentProps) => { return data; }; -export const addQuestion = async (params: AddQuestionProps) => { +export const addQuestion = async ( + params: AddQuestionProps +): Promise => { const { data } = await axios.post( `/product/${params.productId}/questions`, params @@ -52,7 +55,7 @@ export const getPriceHistory = async ( export const getProductQuestion = async ( productId: number ): Promise => { - const { data } = await axios.get(`/product/${productId}/report/questions`); + const { data } = await axios.get(`/product/${productId}/questions`); return data; }; diff --git a/src/app/product/types/Types.ts b/src/app/product/types/Types.ts index 4bf3925..23ea490 100644 --- a/src/app/product/types/Types.ts +++ b/src/app/product/types/Types.ts @@ -47,13 +47,69 @@ export type PriceHistoryResponse = { results: PriceHistoryResults; }; -export type ProductQuestion = { +export type QuestionAnswer = { + content: string; +}; + +export type QuestionUser = { + fullName: string; +}; + +export type QuestionProductBrand = { + _id: string; + status: string; + title_en: string; + title_fa: string; + images: string[]; + logoUrl: string; +}; + +export type QuestionProductCategory = { + _id: string; + title_fa: string; + title_en: string; + icon: string; + imageUrl: string; + theme: string; + description: string; + leaf: boolean; + parent: string | null; +}; + +export type QuestionProductImages = { + cover: string; + list: string[]; +}; + +export type QuestionProduct = { _id: number; - title: string; - type: string; - placeholder: string | null; + url: string; + title_fa: string; + title_en: string; + seoTitle: string; + seoDescription: string | null; + source: string; + description: string; + metaDescription: string; + tags: string[]; + advantages: string[]; + disAdvantages: string[]; + imagesUrl: QuestionProductImages; + isFake: string; + voice: string | null; + specifications: unknown[]; + brand: QuestionProductBrand; + category: QuestionProductCategory; +}; + +export type ProductQuestion = { + _id: string; + content: string; + status: string; + product: QuestionProduct; + user: QuestionUser; + answers: QuestionAnswer[]; createdAt: string; - updatedAt: string; }; export type ProductQuestionsResults = { @@ -71,3 +127,14 @@ export type SaveReportTypes = { description: string; productId: string; }; + +export type AddQuestionResults = { + message: string; + content: string; +}; + +export type AddQuestionResponse = { + status: number; + success: boolean; + results: AddQuestionResults; +};