web/components/AppsComponent/CategoriesData/page.jsx

199 lines
6.4 KiB
React
Raw Normal View History

2024-01-10 11:04:28 +03:30
"use client";
2024-02-16 19:53:36 +03:30
import FilterCategory from "@comp/Category/FilterCategory/page";
import ListProdocts from "@comp/Category/ListProdocts/page";
import FilterCategoryMobile from "@comp/Category/Mobile/FilterCategoryMobile/page";
import ListProductsMobile from "@comp/Category/Mobile/ListProductsMobile/page";
import PaginationCategoory from "@comp/Category/PaginationCategoory/page";
import Footer from "@comp/Footer/page";
import Navbar from "@comp/Navbar/page";
2024-02-05 01:40:57 +03:30
import AppContext from "@ctx/AppContext";
2024-02-16 19:36:30 +03:30
import { useContext, useEffect, useState } from "react";
2024-02-16 18:23:02 +03:30
import InfiniteScroll from "react-infinite-scroll-component";
2024-02-05 01:40:57 +03:30
2024-02-27 21:23:20 +03:30
export default function CategoriesData({ params }) {
console.log("params", params.id[0]);
2024-02-05 01:40:57 +03:30
const CTX = useContext(AppContext);
2024-02-16 18:23:02 +03:30
const pageGetProducts = CTX.state.pageGetProducts;
2024-04-13 03:33:45 +03:30
const stopProducts = CTX.state.stopProducts;
2024-02-16 18:23:02 +03:30
const pager = CTX.state.pager;
2024-04-13 03:33:45 +03:30
const productsLength = CTX.state.products?.length ?? 0;
console.log("stopProducts-handleInfiniteNextFetchProducts", productsLength);
2024-02-27 21:23:20 +03:30
const filter = CTX.state.filter;
2024-02-16 18:23:02 +03:30
2024-02-27 21:23:20 +03:30
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;
2024-02-05 01:40:57 +03:30
const fetchBarnds = async () => {
2024-02-27 21:23:20 +03:30
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/brand${
params.id[0] != 0 ? `?categoryId=${params.id[0]}` : ""
}`
2024-02-27 21:23:20 +03:30
);
2024-02-05 01:40:57 +03:30
const brands = await res.json();
CTX.setBrands(brands);
2024-01-10 11:04:28 +03:30
};
2024-02-27 21:23:20 +03:30
const decodedName = decodeURIComponent(params.id[1]);
2024-02-16 18:23:02 +03:30
const handleInfiniteNextFetchProducts = () => {
2024-02-27 21:23:20 +03:30
// Increment the page number
const nextPage = pageGetProducts + 1;
2024-04-13 03:33:45 +03:30
console.log("runeed-handleInfiniteNextFetchProducts", nextPage);
2024-02-27 21:23:20 +03:30
// Fetch products for the next page
2024-04-13 03:33:45 +03:30
2024-02-16 18:23:02 +03:30
CTX.fetchProducts(
2024-02-27 21:23:20 +03:30
nextPage,
params.id[0] != 0 ? params.id[0] : "",
2024-02-16 18:23:02 +03:30
selectedBrands,
isChecked,
rangePrice,
rangePrice,
sortBy != -1 ? sortBy : "",
2024-02-16 18:23:02 +03:30
isRangePrice
);
2024-02-27 21:23:20 +03:30
// Update the pageGetProducts state for the next fetch
CTX.setPageGetProducts(nextPage);
};
2024-04-13 03:33:45 +03:30
const [loading, setLoading] = useState(true);
const getData = async () => {
setLoading(true);
await CTX.fetchProducts(0, params.id[0] != 0 ? params.id[0] : "");
await fetchBarnds();
setLoading(false);
};
2024-02-05 01:40:57 +03:30
useEffect(() => {
2024-02-16 18:23:02 +03:30
window.scrollTo({
top: 0,
behavior: "smooth", // Optional: smooth scrolling behavior
});
2024-04-13 03:33:45 +03:30
getData();
2024-02-05 01:40:57 +03:30
}, []);
2024-01-10 11:04:28 +03:30
2024-02-16 18:23:02 +03:30
console.log(pager?.totalPage > 1, pager?.totalPage);
2024-01-10 11:04:28 +03:30
return (
<>
<div className=" pb-20">
<Navbar theme={1} />
2024-02-16 18:23:02 +03:30
{!CTX.state.isMobile && (
<div className="xs:hidden lg:block">
2024-02-27 21:23:20 +03:30
<div className="text-right px-8 py-5">
2024-04-12 19:00:01 +03:30
<h1 className="font-medium ">{decodedName.replace(/-/g, " ")}</h1>
2024-02-27 21:23:20 +03:30
</div>
<div className="pl-20 pr-10 grid lg:grid-cols-8 xl:grid-cols-5 rtl mt-10 ">
2024-02-16 18:23:02 +03:30
<FilterCategory
id={params.id}
isChecked={isChecked}
selectedBrands={selectedBrands}
rangePrice={rangePrice}
sortBy={sortBy}
isRangePrice={isRangePrice}
2024-02-27 21:23:20 +03:30
filter={filter}
2024-02-16 18:23:02 +03:30
/>
2024-01-10 11:04:28 +03:30
2024-02-16 18:23:02 +03:30
<div className="lg:col-span-6 xl:col-span-4 ">
2024-04-13 03:33:45 +03:30
{loading && <>در حال بارگیری</>}
2024-02-16 18:23:02 +03:30
{true ? (
2024-04-13 03:33:45 +03:30
<>
{!loading && (
<InfiniteScroll
dataLength={productsLength}
next={handleInfiniteNextFetchProducts}
hasMore={!stopProducts}
>
<ListProdocts
id={params.id}
isChecked={isChecked}
selectedBrands={selectedBrands}
rangePrice={rangePrice}
sortBy={sortBy}
isRangePrice={isRangePrice}
/>
</InfiniteScroll>
)}
</>
2024-02-16 18:23:02 +03:30
) : (
<div className="flex justify-center py-5">
<div className="bg-white shadow mt-5 w-fit rounded-full text-sm p-4">
چیزی یافت نشد
</div>
</div>
)}
</div>
</div>
</div>
)}
{CTX.state.isMobile && (
<div className="xs:block lg:hidden ">
<FilterCategoryMobile
2024-02-05 01:40:57 +03:30
id={params.id}
isChecked={isChecked}
selectedBrands={selectedBrands}
rangePrice={rangePrice}
sortBy={sortBy}
isRangePrice={isRangePrice}
2024-01-10 11:04:28 +03:30
/>
2024-02-27 21:23:20 +03:30
<div className="text-right px-8 py-5">
2024-04-12 19:00:01 +03:30
<h1 className="font-medium text-sm ">
{decodedName.replace(/-/g, " ")}
</h1>
2024-02-27 21:23:20 +03:30
</div>
2024-02-16 18:23:02 +03:30
<div>
2024-04-13 03:33:45 +03:30
{loading && <>در حال بارگیری</>}
2024-02-16 18:23:02 +03:30
{true ? (
2024-04-13 03:33:45 +03:30
<>
{!loading && (
<InfiniteScroll
dataLength={productsLength}
next={handleInfiniteNextFetchProducts}
hasMore={!stopProducts}
scrollThreshold={0.5}
>
<ListProductsMobile
id={params.id}
isChecked={isChecked}
selectedBrands={selectedBrands}
rangePrice={rangePrice}
sortBy={sortBy}
isRangePrice={isRangePrice}
/>
</InfiniteScroll>
)}
</>
2024-02-16 18:23:02 +03:30
) : (
<div className="flex justify-center py-5">
<div className="bg-white shadow mt-5 w-fit rounded-full text-sm p-4">
چیزی یافت نشد
</div>
</div>
)}
</div>
</div>
)}
2024-01-10 11:04:28 +03:30
</div>
2024-02-16 18:23:02 +03:30
{pager?.totalPage > 1 && (
<PaginationCategoory
id={params.id}
isChecked={isChecked}
selectedBrands={selectedBrands}
rangePrice={rangePrice}
sortBy={sortBy}
isRangePrice={isRangePrice}
/>
)}
2024-02-05 01:40:57 +03:30
2024-01-10 11:04:28 +03:30
<Footer />
</>
);
2024-02-05 01:40:57 +03:30
}