"use client"; import CardCart from "@comp/Cards/CardCart/page"; import NavBarDownCart from "@comp/Carts/component/NavBarDownCart/page"; import Navbar from "@comp/Navbar/page"; import AppContext from "@ctx/AppContext"; import { useRouter } from "next/navigation"; import Chapar from "plugins/Chapar"; import PersianNumber from "plugins/PersianNumber"; import { useContext, useEffect } from "react"; import { toast } from "react-toastify"; const CartData = () => { const CTX = useContext(AppContext); const router = useRouter(); const cart = CTX.state.cart; const calculateTotalCost = cart.reduce( (total, item) => total + parseInt(item.cost) * item.count, 0 ); const calculateTotalCostWithDiscount = cart.reduce( (total, item) => total + parseInt(item.costWithDiscount) * item.count, 0 ); const handleGoCheckOut = async () => { // Check if the user is authorized based on the presence of a token in local storage const token = localStorage.getItem("token"); if (token) { // If token exists, proceed to checkout const productsToSend = cart.map((item) => ({ productId: item.id, count: parseInt(item.count), })); try { const data = await Chapar.post( `${process.env.NEXT_PUBLIC_API_URL}/order/bag/add`, JSON.stringify(productsToSend), { headers: { Authorization: localStorage.getItem("token"), }, } ); CTX.setCheckOutData(data); } catch ({ error, status }) { toast.error(`${error?.response?.data?.message}`, { position: "bottom-right", closeOnClick: true, }); } router.push("/cart/checkout"); // Redirect to the checkout Page } else { // If token does not exist, redirect to login router.push("/login"); // Redirect to the login Page } }; useEffect(() => { CTX.setBottomSheetCartOpen(false); }, []); return ( <>

محصولات انتخاب شده

محصول

{cart?.map((e, index) => ( ))}

حساب نهایی

{/*

محصول

*/}

قیمت

{" "} تومان

تخفیف محصول

{" "} تومان

قابل پرداخت

{" "} تومان
handleGoCheckOut()} permissionGoPay={!!cart.length > 0} />
); }; export default CartData;