diff --git a/components/AppsComponent/ProductData/page.jsx b/components/AppsComponent/ProductData/page.jsx index a683f51..939eebd 100644 --- a/components/AppsComponent/ProductData/page.jsx +++ b/components/AppsComponent/ProductData/page.jsx @@ -9,18 +9,31 @@ import Image from "next/image"; import PersianNumber from "plugins/PersianNumber"; import logo from "../../../public/images/logo.png"; import moment from "jalali-moment"; +import Chapar from "plugins/Chapar"; +import { toast } from "react-toastify"; +import { useRouter } from "next/navigation"; const ProductData = ({ params, data }) => { console.log("data", data); const [product, setProduct] = useState([]); const [specificationsHeader, setSpecificationsHeader] = useState([]); const [productBarDetail, setProductBarDetail] = useState(0); + const [comment, setComment] = useState("0"); + + const router = useRouter(); const fetchPost = async (id) => { const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/product/${id}`); const post = await res.json(); setProduct(post.product); }; + const fetchReview = async (id) => { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/product/${id}/review?page=0` + ); + const post = await res.json(); + setProduct(post.product); + }; const displaySpecifications = (specs) => { let data = []; @@ -51,8 +64,37 @@ const ProductData = ({ params, data }) => { }); } }; + + const createProductReview = async () => { + const body = { title: "", comment, rate: "", productId: params.id[0] }; + try { + const data = await Chapar.post( + `${process.env.NEXT_PUBLIC_API_URL}/product/review`, + body + ); + + // fetchReview(); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + } + }; + + const handleCreateReview = () => { + const token = localStorage.getItem("token"); + + if (token) { + createProductReview(); + } else { + router.push("/login"); + } + }; + useEffect(() => { fetchPost(params.id[0]); + fetchReview(params.id[0]); }, []); useEffect(() => { @@ -137,13 +179,13 @@ const ProductData = ({ params, data }) => { {data?.product?.files?.length > 0 ? ( CTX.setIsOpenLightBox(true)} - alt={`${product.persianName} - ${product.englishName}`} + alt={`${product?.persianName} - ${product?.englishName}`} property loading="eager" /> @@ -528,8 +570,12 @@ const ProductData = ({ params, data }) => { id="" cols="30" rows="8" + onChange={(e) => setComment(e.target.value)} /> -
+
handleCreateReview()} + >