добавил компонент перебора категорий сбоку
This commit is contained in:
1 parent
88ff1d8b99
commit
c1adee646f
1 file changed
+27
-2
@@ -5,8 +5,33 @@ import { fetchCategories, setSelectedCategory } from "./categoriesSlice";
|
|||||||
import { fetchProductsByCategory, setFilters, setPage } from "./productsSlice";
|
import { fetchProductsByCategory, setFilters, setPage } from "./productsSlice";
|
||||||
|
|
||||||
const SidebarCategories: React.FC = () => {
|
const SidebarCategories: React.FC = () => {
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const { items, selectedCategoryId, loading } = useAppSelector(s => s.categories);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(fetchCategories());
|
||||||
|
}, [dispatch]);
|
||||||
|
|
||||||
|
const onClick = (catId: number) => {
|
||||||
|
dispatch(setSelectedCategory(catId));
|
||||||
|
dispatch(setFilters({})); // сброс фильтров
|
||||||
|
dispatch(setPage(1));
|
||||||
|
dispatch(fetchProductsByCategory({ categoryId: catId, page: 1, limit: 9 }));
|
||||||
|
};
|
||||||
return(
|
return(
|
||||||
<>
|
<aside className={styles.sidebar}>
|
||||||
</>
|
<h3 className={styles.title}>КАТЕГОРИИ ТОВАРОВ</h3>
|
||||||
|
{loading ? <p>Загрузка...</p> : (
|
||||||
|
<ul className={styles.list}>
|
||||||
|
{items.map(cat => (
|
||||||
|
<li key={cat.id}
|
||||||
|
className={`${styles.item} ${selectedCategoryId === cat.id ? styles.active : ""}`}
|
||||||
|
onClick={() => onClick(cat.id)}>
|
||||||
|
{cat.name}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</aside>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user