"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?.englishName}{" "}
اصالت کالای
بمب امروز
{product?.warranty}
{e}
{data?.product?.persianName}{" "}
گارانتی{" "} اصالت{" "} و{" "} سلامت فیزیکی کالا
مشخصات محصول
مشخصات
نقد و بررسی
دیدگاه مخاطبان
مخاطبان
{e.title}: {" "} {e.value}{" "}
{e?.content}
{e?.children[0]?.content}