"use client"; import "../../style/globals.css"; import "../../style/fontiran.css"; import "swiper/css"; import "react-image-gallery/styles/css/image-gallery.css"; import Chapar from "plugins/Chapar"; import AppContext from "@ctx/AppContext"; import { useEffect, useState } from "react"; import "react-toastify/dist/ReactToastify.css"; import { ToastContainer } from "react-toastify"; import Loading from "plugins/Loading/page"; export default function RootLayout({ children }) { const [cart, setCart] = useState([]); const [products, setProducts] = useState([]); const [navData, setNavData] = useState([]); const [brands, setBrands] = useState([]); const [loading, setLoading] = useState(false); const [closeNavbar, setCloseNavbar] = useState(false); const [bottomSheetCart, setBottomSheetCart] = useState(false); const AddItemToCart = (id, persianName, cost) => { console.log("persianName", persianName); setCart((prevCart) => { // Check if the item is already in the cart const existingItem = prevCart.find((item) => item.id === id); if (existingItem) { // If the item is already in the cart, update its count return prevCart.map((item) => item.id === id ? { ...item, count: item.count + 1 } : item ); } else { // If the item is not in the cart, add it with a count of 1 return [...prevCart, { id, count: 1, persianName, cost }]; } }); }; const RemoveItemFromCart = (id) => { setCart((prevCart) => { // Check if the item is already in the cart const existingItem = prevCart.find((item) => item.id === id); if (existingItem) { // If the item is already in the cart if (existingItem.count === 1) { // If the item count is 1, remove it from the cart return prevCart.filter((item) => item.id !== id); } else { // If the item count is greater than 1, update its count return prevCart.map((item) => item.id === id ? { ...item, count: item.count - 1 } : item ); } } else { // If the item is not in the cart, do nothing return prevCart; } }); }; const fetchNavData = async (id) => { const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/product/category?sortByMain=true` ); const post = await res.json(); setNavData(post); }; const fetchProducts = async ( id, selectedBrands, isChecked, minPrice, maxPrice, sort, isRangePrice ) => { const brandIds = selectedBrands?.map((brand) => brand.id); const queryString = `page=0${id ? `&categoryId=${id}` : ""}${ brandIds?.length > 0 ? `&brandIds=${brandIds?.join("&brandIds=")}` : "" }${isChecked ? `&isActive=${isChecked}` : ""}${ isRangePrice ? `&minPrice=${minPrice}` : "" }${isRangePrice ? `&maxPrice=${maxPrice}` : ""}${ !!sort ? `&sortBy=${sort}` : "" }`; const cleanQueryString = decodeURIComponent( queryString.replace(/\%20/g, " ") ); const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/product?${cleanQueryString}` ); const post = await res.json(); setProducts(post); }; useEffect(() => { fetchNavData(); }, []); return ( {children} ); }