'use client' import { useState, useRef } from 'react' import { nanoid } from '@reduxjs/toolkit' import { useCreateReviewMutation } from '@/store/services' import { ratingStatus, reviewSchema } from 'utils' import { SubmitHandler, useFieldArray, useForm } from 'react-hook-form' import { yupResolver } from '@hookform/resolvers/yup' import { Icons, TextField, DisplayError, SubmitModalBtn, Modal, HandleResponse } from 'components' const ReviewModal = props => { //? Props const { isShow, onClose, productTitle, prdouctID } = props //? Refs const positiveRef = useRef(null) const negativeRef = useRef(null) //? State const [rating, setRating] = useState(1) //? Form Hook const { handleSubmit, register, formState: { errors: formErrors }, reset, control, } = useForm({ resolver: yupResolver(reviewSchema), defaultValues: { comment: '', title: '', positivePoints: [], negativePoints: [], rating: 1, product: '', }, }) const { fields: positivePointsFields, append: appentPositivePoint, remove: removePositivePoint, } = useFieldArray({ name: 'positivePoints', control, }) const { fields: negativePointsFields, append: appendNegativePoint, remove: removeNegativePoint, } = useFieldArray({ name: 'negativePoints', control, }) //? Create Review Query const [createReview, { isSuccess, isLoading, data, isError, error }] = useCreateReviewMutation() //? Handlers const handleAddPositivePoint = () => { if (positiveRef.current) { appentPositivePoint({ id: nanoid(), title: positiveRef.current.value }) positiveRef.current.value = '' } } const handleAddNegativePoint = () => { if (negativeRef.current) { appendNegativePoint({ id: nanoid(), title: negativeRef.current.value }) negativeRef.current.value = '' } } const submitHander = data => createReview({ body: { ...data, rating, product: prdouctID }, }) //? Render(s) return ( <> {/* Handle Create Review Response */} {(isSuccess || isError) && ( { onClose() reset() setRating(1) }} onError={() => { onClose() reset() setRating(1) }} /> )} 留下你对 {productTitle} 商品的评价
{/* rating */}
评分!:‌ {ratingStatus[rating]}
{ setRating(+e.target.value) }} />
{Array(5) .fill('_') .map((_, i) => ( ))}
{/* title */} {/* positivePoints */}
{positivePointsFields.length > 0 && (
{positivePointsFields.map((field, index) => (
{field.title}
))}
)}
{/* negativePoints */}
{negativePointsFields.length > 0 && (
{negativePointsFields.map((field, index) => (
{field.title}
))}
)}
{/* comment */}