test cmment

master
حسین معصومی پور 2024-09-15 15:37:32 +03:30
parent 7fc2737439
commit 2e7a2d1740
1 changed files with 49 additions and 3 deletions

View File

@ -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 ? (
<Image
src={`${process.env.NEXT_PUBLIC_STORAGE_URL}/${
product.files && product.files[0].fileLocation
product?.files && product?.files[0].fileLocation
}`}
width={350}
height={350}
className=" mx-auto !object-cover"
onClick={() => 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)}
/>
<div className="flex justify-end">
<div
className="flex justify-end"
onClick={() => handleCreateReview()}
>
<button className="btn btn-primary text-sm py-3 rounded-3xl">
ارسال دیدگاه ( برای ارسال دیدگاه باید ابتدا ورود کنید){" "}
</button>