дописал каталог

This commit is contained in:
Joe_Peach committed 2025-12-05 00:10:47 +03:00
1 parent 223ec865e0
commit e517c58d57
4 files changed
+67 -27

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
import React from "react";
import styles from "./Pagination.module.css";
import { useAppSelector, useAppDispatch } from "../../app/hooks";
import { setPage, fetchProductsByCategory } from "./productsSlice";
interface Props { categoryId: number }
const Pagination: React.FC<Props> = ({ categoryId }) => {
const dispatch = useAppDispatch();
const { total, page, limit } = useAppSelector(s => s.products);
const totalPages = Math.ceil(total / limit);
if (totalPages <= 1) return null;
const go = (p:number) => {
dispatch(setPage(p));
dispatch(fetchProductsByCategory({ categoryId, page: p, limit }));
window.scrollTo({ top: 200, behavior: "smooth" });
}
return (
<div className={styles.wrap}>
<button className={styles.go} onClick={() => go(1)} disabled={page===1}>В НАЧАЛО</button>
<div className={styles.pages}>
{Array.from({length: totalPages}, (_,i) => i+1).map(p => (
<button key={p} className={`${styles.page} ${p===page?styles.active:""}`} onClick={() => go(p)}>{p}</button>
))}
</div>
<button className={styles.go} onClick={() => go(totalPages)} disabled={page===totalPages}>В КОНЕЦ</button>
</div>
);
};
export default Pagination;
+4 -5
View File
@@ -1,13 +1,12 @@
import React from "react";
import type { Product } from "../../types/product";
import ProductCard from "./ProductCard";
import styles from "./Catalog.module.css";
import { useAppSelector } from "../../app/hooks";
interface Props {
products: Product[];
}
const ProductGrid: React.FC<Props> = ({ products }) => {
const ProductGrid: React.FC = () => {
const {items: products} = useAppSelector(p => p.products)
return (
<div className={styles.grid}>
{products.map((p) => (
@@ -35,3 +35,5 @@ const SidebarCategories: React.FC = () => {
</aside>
)
}
export default SidebarCategories;
+27 -22
View File
@@ -1,31 +1,36 @@
import React, { useEffect } from "react";
import { useParams } from "react-router-dom";
import type { RootState } from "../../app/store";
import { getCategoryProducts } from "../../features/catalog/productsSlice";
import CatalogSidebar from "../../features/catalog/catalogeSidebar"
import React from "react";
import styles from "./ProductsPage.module.css";
import SidebarCategories from "../../features/catalog/SidebarCategories";
import CategoryCards from "../../features/catalog/CategoryCards";
import Filters from "../../features/catalog/CategoryFilters";
import ProductGrid from "../../features/catalog/ProductGrid";
import styles from "../../features/catalog/catalog.module.css";
import { useAppDispatch, useAppSelector } from "../../app/hooks";
import Pagination from "../../features/catalog/Pagination";
import { useAppSelector } from "../../app/hooks";
const CatalogPage: React.FC = () => {
const { category } = useParams<{ category: string }>();
const dispatch = useAppDispatch()
const { loading, filtered, error } = useAppSelector((s: RootState) => s.catalog);
useEffect(() => {
if (category) dispatch(getCategoryProducts(category));
}, [category]);
const ProductsPage: React.FC = () => {
const { selectedCategoryId } = useAppSelector(s => s.categories);
return (
<div className={styles.catalogWrapper}>
<CatalogSidebar />
<div className={styles.productsSection}>
{loading && <p>Загрузка...</p>}
{error && <p>Ошибка: {error}</p>}
<ProductGrid products={filtered} />
<div className={styles.wrapper}>
<SidebarCategories />
<div className={styles.content}>
{!selectedCategoryId ? (
<CategoryCards onSelect={(id) => {}} />
) : (
<>
<div className={styles.leftControls}>
<Filters />
</div>
<div className={styles.productsArea}>
<ProductGrid />
<Pagination categoryId={selectedCategoryId} />
</div>
</>
)}
</div>
</div>
);
};
export default CatalogPage;
export default ProductsPage;