"use client"; import Footer from "@comp/Footer/page"; import Navbar from "@comp/Navbar/page"; import GalleryBox from "plugins/Gallery/page"; import { useContext, useEffect, useState } from "react"; import AddToCart from "@comp/Cards/Components/AddToCart/page"; 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"; import InfiniteScroll from "react-infinite-scroll-component"; import BottomSheetComment from "plugins/bottomSheet/BottomSheetComment"; import AppContext from "@ctx/AppContext"; const ProductData = ({ params, data }) => { console.log("data", data); const CTX = useContext(AppContext); const [product, setProduct] = useState([]); const [review, setReview] = useState([]); const [specificationsHeader, setSpecificationsHeader] = useState([]); const [productBarDetail, setProductBarDetail] = useState(0); const [content, setContent] = useState(""); const [rate, setRate] = useState(4); const [page, setPage] = useState(0); const [hasMore, setHasMore] = useState(true); 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, currentPage, afterCreatReview) => { try { const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/product/${id}/comment?page=${currentPage}&count=10` ); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const data = await res.json(); // Assuming the API returns an array of reviews and a total count or indication if more data is available if (data.length < 10) { setHasMore(false); } if (afterCreatReview) { setReview((prevReview) => [...prevReview, ...data]); } else { setReview(data); } } catch (error) { console.error("Error fetching reviews:", error); setHasMore(false); // Stop further fetches on error } }; const fetchMoreData = () => { const nextPage = page + 1; fetchReview(params.id[0], nextPage); setPage(nextPage); }; const displaySpecifications = (specs) => { let data = []; if (specs?.length > 3) { specs?.slice(0, 3).forEach((spec) => { const { title, value } = spec; data.push(`${title}: ${value}`); }); } else { specs?.forEach((spec) => { const { title, value } = spec; data.push(`${title}: ${value}`); }); } setSpecificationsHeader(data); // You can replace }; const scrollToSection = (id) => { const element = document.getElementById(id); if (element) { const offset = -80; // Adjust this value as needed window.scrollTo({ top: element.offsetTop + offset, behavior: "smooth", }); } }; const createProductReview = async () => { const body = { title: ` نظر کاربر برای محصول {" "}${data.product?.persianName}`, content, rate: 1, productId: params.id[0], rate, }; try { const data = await Chapar.post( `${process.env.NEXT_PUBLIC_API_URL}/comment`, body ); setContent(""); fetchReview(params.id[0], 0); } 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"); } }; const handleStarClick = (value) => { setRate(value); }; useEffect(() => { fetchPost(params.id[0]); fetchReview(params.id[0], 0); }, []); useEffect(() => { displaySpecifications(product?.specifications); }, [product]); console.log("review", review); return ( <>

{data.product?.persianName}{" "}

{data?.product?.englishName}{" "}

اصالت کالای

{data?.product?.hasDiscount && (

بمب امروز

)} {!data?.product?.warranty == "" && (

{product?.warranty}

)}
{/*
*/}

{data?.product?.summery}

    {specificationsHeader.map((e, index) => (
  • {e}

  • ))}
{/* xs:sticky lg:relative xs:top-[60px] lg:top-0 ==> sticky for price=================== */}
{data?.product?.files?.length > 0 ? ( CTX.setIsOpenLightBox(true)} alt={`${product?.persianName} - ${product?.englishName}`} property loading="eager" /> ) : (
وسمه
)}

{data?.product?.persianName}{" "}

گارانتی{" "} اصالت{" "} و{" "} سلامت فیزیکی کالا

{true ? (
{" "}

تومان
) : (
{" "}

تومان
)}
{ setProductBarDetail(0); scrollToSection("section0"); }} >

مشخصات محصول

مشخصات

{ setProductBarDetail(2); scrollToSection("section2"); }} >

نقد و بررسی

{ setProductBarDetail(3); scrollToSection("section3"); }} >

دیدگاه مخاطبان

مخاطبان

مشخصات محصول

{product?.specifications?.map((e, index) => (

{e.title}: {" "} {e.value}{" "}

))}

نقد و برسی

{!!data?.product?.expertCheck ? (
) : (
چیزی یافت نشد
)}

دیدگاه مخاطبان{" "}

{review.length > 0 ? ( review.map((e) => ( <>
{/* SVG Icon */} {/* SVG Path */}

{e?.userFullName}

{e?.content}

{e?.children && e.children?.length > 0 && (
Logo

پشتیبانی فروشگاه وسمه

{e?.children[0]?.content}

)} )) ) : (
چیزی یافت نشد
)}
CTX.setBottomSheetCommentOpen(true)}> باز شدن
{product?.hasDiscount ? ( <>

{" "}

تومان
) : (
{" "}

تومان
)}