diff --git a/components/AppsComponent/CartData/page.jsx b/components/AppsComponent/CartData/page.jsx new file mode 100644 index 0000000..fddcf37 --- /dev/null +++ b/components/AppsComponent/CartData/page.jsx @@ -0,0 +1,181 @@ +"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; diff --git a/src/app/categories/[id]/page.jsx b/components/AppsComponent/CategoriesData/page.jsx similarity index 79% rename from src/app/categories/[id]/page.jsx rename to components/AppsComponent/CategoriesData/page.jsx index c416e9e..9577b61 100644 --- a/src/app/categories/[id]/page.jsx +++ b/components/AppsComponent/CategoriesData/page.jsx @@ -11,31 +11,39 @@ import AppContext from "@ctx/AppContext"; import { useContext, useEffect, useState } from "react"; import InfiniteScroll from "react-infinite-scroll-component"; -export default function Page({ params }) { - // const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/product?page=0`); - // const products = await res.json(); +export default function CategoriesData({ params }) { + console.log("params", params.id[0]); + const CTX = useContext(AppContext); const pageGetProducts = CTX.state.pageGetProducts; const stopGetTasks = CTX.state.stopGetTasks; const pager = CTX.state.pager; + const filter = CTX.state.filter; - const [isChecked, setIsChecked] = useState(false); - const [selectedBrands, setSelectedBrands] = useState([]); - const [rangePrice, setRangePrice] = useState([1000, 100]); - const [isRangePrice, setIsRangePrice] = useState(false); - const [sortBy, setSortBy] = useState(null); + const isChecked = CTX.state.isChecked; + const selectedBrands = CTX.state.selectedBrands; + const rangePrice = CTX.state.rangePrice; + const isRangePrice = CTX.state.isRangePrice; + const sortBy = CTX.state.sortBy; const fetchBarnds = async () => { - const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/brand`); + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/brand?categoryId=${params.id[0]}` + ); const brands = await res.json(); CTX.setBrands(brands); }; + const decodedName = decodeURIComponent(params.id[1]); + const handleInfiniteNextFetchProducts = () => { - CTX.setPageGetProducts((e) => e + 1); + // Increment the page number + const nextPage = pageGetProducts + 1; + + // Fetch products for the next page CTX.fetchProducts( - pageGetProducts + 1, - params.id, + nextPage, + params.id[0], selectedBrands, isChecked, rangePrice, @@ -43,14 +51,16 @@ export default function Page({ params }) { sortBy, isRangePrice ); - }; + // Update the pageGetProducts state for the next fetch + CTX.setPageGetProducts(nextPage); + }; useEffect(() => { window.scrollTo({ top: 0, behavior: "smooth", // Optional: smooth scrolling behavior }); - CTX.fetchProducts(0, params.id); + CTX.fetchProducts(0, params.id[0]); fetchBarnds(); }, []); @@ -62,18 +72,18 @@ export default function Page({ params }) { {!CTX.state.isMobile && (
-
+
+

{decodedName}

+
+
@@ -88,7 +98,6 @@ export default function Page({ params }) { isChecked={isChecked} selectedBrands={selectedBrands} rangePrice={rangePrice} - setSortBy={setSortBy} sortBy={sortBy} isRangePrice={isRangePrice} /> @@ -110,17 +119,14 @@ export default function Page({ params }) { - +
+

{decodedName}

+
{true ? ( { + const CTX = useContext(AppContext); + const [shippingData, setShippingData] = useState([]); + const [shippingId, setShippingID] = useState(null); + const [addressData, setAddressData] = useState([]); + const [addressId, setAddressId] = useState(null); + const [permissionGoPay, setPermissionGoPay] = useState(false); + + const router = useRouter(); + const checkOutData = CTX.state.checkOutData; + + const body = { + addressId: addressData[addressId]?.id, + + orderId: checkOutData?.id, + shippingId: shippingData[shippingId]?.id, + }; + + console.log(body); + + const GetShippingData = async () => { + try { + const data = await Chapar.get( + `${process.env.NEXT_PUBLIC_API_URL}/warehouse/shipping?page=0` + ); + + setShippingData(data); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + } + }; + + const handleShippingID = async (index) => { + if (addressId !== null) { + setShippingID(index); + try { + const data = await Chapar.post( + `${process.env.NEXT_PUBLIC_API_URL}/order/bag/shipping/${checkOutData?.id}`, + JSON.stringify({ + addressId: addressData[addressId]?.id, + + orderId: checkOutData?.id, + shippingId: shippingData[index]?.id, + }), + { + headers: { + Authorization: localStorage.getItem("token"), + }, + } + ); + CTX.setCheckOutData(data); + setPermissionGoPay(true); + console.log(data); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + } + } else { + toast.error(`ابتدا آدرس را انتحاب کنید`, { + position: "bottom-right", + closeOnClick: true, + }); + } + }; + + const handleGoPayment = async () => { + try { + const data = await Chapar.post( + `${process.env.NEXT_PUBLIC_API_URL}/order/bag/payment/${checkOutData?.id}?paymentMethod=1`, + + { + headers: { + Authorization: localStorage.getItem("token"), + }, + } + ); + + router.push(data?.paymentUrl); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + } + }; + + useEffect(() => { + if (shippingData.length <= 0) { + GetShippingData(); + } + + if (CTX.state.addressData <= 0) { + CTX.fetchAddressUser(); + } + }, [checkOutData]); + + useEffect(() => { + setAddressData(CTX.state.addressData); + }, [CTX.state.addressData]); + + useEffect(() => { + if (CTX.state.checkOutData.length <= 0) { + router.push("/cart"); + } + + setPermissionGoPay(false); + }, []); + + return ( + <> +
+ + +
+
+

آدس ها

+ +
CTX.setBottomSheetAddressOpen(true)} + > +

+ افزودن آدرس جدید +

+
+
+ + {addressData.map((e, index) => ( +
setAddressId(index)} + key={index} + > +
+ + + + +
+
+

{e.address}

+
+
+ ))} + +
+

زمان و نحوه ارسال

+ +
+
+
+ + + +
+

+ سریع ترین زمان ارسال +

+
+
+
+ +
+ {shippingData?.map((e, index) => ( +
handleShippingID(index)} + key={index} + > +
+ + + + +
+
+

+ {e.name}{" "} +

+

+ {" "} + حداکثر + + روز کاری + | هزینه ارسال + + هزار تومان +

+
+
+ ))} +
+ +
+

روش پرداخت

+
+ +
+
+
+ +
+
+

+ پرداخت آنلاین +

+

زرین پال

+
+
+ +
+
+ +
+
+

+ پرداخت آنلاین (به زودی) +

+

آسان پرداخت

+
+
+ +
{ + if (checkOutData?.discountCode == "") { + CTX.setBottomSheetDiscountOpen(true); + } + }} + > +
+
+ + + +
+
+

+ افزودن کد تخفیف{" "} +

+

+ کد تخفیف خود را وارد کنید +

+
+
+ + {checkOutData?.discountCode == "" ? ( +
+ + +
+ ) : ( +
+

+ کد تخفیف ثبت شد{" "} +

+
+ )} +
+
+ +
+
+

حساب نهایی

+ +
+

مشاهده اقلام

+
+
+ +
+
+

قیمت

+ +
+

+ +

+ + {" "} + تومان + +
+
+ +
+

تخفیف محصول

+ +
+

+ +

+ + {" "} + تومان + +
+
+ +
+

+ هزینه بسته بندی +

+ +
+

+ +

+ + {" "} + تومان + +
+
+ +
+

هزینه ارسال

+ +
+

+ +

+ + {" "} + تومان + +
+
+ +
+

+ قابل پرداخت +

+ +
+

+ +

+ + {" "} + تومان + +
+
+
+
+
+ handleGoPayment()} + permissionGoPay={permissionGoPay} + /> +
+ + + + ); +}; + +export default CheckoutData; diff --git a/components/AppsComponent/FaqData/page.jsx b/components/AppsComponent/FaqData/page.jsx new file mode 100644 index 0000000..c37ac8a --- /dev/null +++ b/components/AppsComponent/FaqData/page.jsx @@ -0,0 +1,81 @@ +"use client"; + +import Footer from "@comp/Footer/page"; +import Navbar from "@comp/Navbar/page"; +import { useEffect, useState } from "react"; + +const FaqData = () => { + const [faq, setFaq] = useState([]); + const [faqSelect, setFaqSelect] = useState(0); + + const fetchNavData = async (id) => { + const res = await fetch(`https://jsonplaceholder.typicode.com/comments`); + const post = await res.json(); + setFaq(post); + }; + + useEffect(() => { + fetchNavData(); + }, []); + return ( + <> +
+
+ + +
+
+
+

+ {" "} + , +

+
+
+
+ +
+
+

+ پرسش‌های متداول +

+
+
+
+
+ +
+ {faq?.map((e, index) => ( +
setFaqSelect(index)} + key={index} + > +
+ + {faqSelect == index ? "-" : "+"} + +

+ {e.name} +

+
+ {faqSelect == index && ( + <> +
+

+ {e.body} +

+ + )} +
+ ))} +
+ +