-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8a1516
commit a537d6c
Showing
7 changed files
with
133 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,72 @@ | ||
import type { FC } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { type FC, useEffect, useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { NavLink, useParams } from 'react-router-dom' | ||
|
||
import { setCategoryId } from '@/entities/Category/slice/categoryIdSlice' | ||
import { setCategorySlug } from '@/entities/Category/slice/categorySlugSlice' | ||
import styles from '@/features/CategoryItem/CategoryItem.module.scss' | ||
import SideBar from '@/features/SideBar' | ||
import { Routes } from '@/shared/config/routerConfig/routes' | ||
import Link from '@/shared/ui/Link/Link' | ||
import { getCategorySelector } from '@/widgets/CategoryList/selectors/selectors' | ||
import type { BranchesData, MainCategoryInfo } from '@/widgets/CategoryList/types/types' | ||
|
||
type Props = { | ||
name: string | ||
slug: string | ||
count: number | ||
id: number | ||
item: MainCategoryInfo | ||
} | ||
|
||
/** | ||
* Компонент единицы категории в списке категорий бокового меню | ||
* @param {string} name - название категории; | ||
* @param {string} slug - URL для страницы категориии; | ||
* @param {number} count - количество товаров в категории; | ||
* @param {number} id - id категории; | ||
* @param {MainCategoryInfo} item - главная категория с ветками; | ||
*/ | ||
export const CategoryItem: FC<Props> = ({ name, slug, count, id }) => { | ||
export const CategoryItem: FC<Props> = ({ item }) => { | ||
const dispatch = useDispatch() | ||
|
||
const { slug } = useParams() | ||
const getMainCategories = useSelector(getCategorySelector) | ||
|
||
const [itemActive, setItemActive] = useState<MainCategoryInfo | BranchesData>() | ||
const [branch, setBranch] = useState<MainCategoryInfo | BranchesData>() | ||
|
||
useEffect(() => { | ||
if (getMainCategories.find(el => el.slug === slug)) { | ||
setItemActive(getMainCategories.find(el => el.slug === slug)) | ||
} else if (item.branches.find(el => el.slug === slug)) { | ||
setItemActive(item.branches.find(el => el.slug === slug)) | ||
setBranch(getMainCategories.find(el => el.branches.find(el => el.name === itemActive?.name))) | ||
} | ||
}, [itemActive]) | ||
|
||
return ( | ||
<li className={styles['category-list__item']}> | ||
<Link | ||
to={`${Routes.CATEGORIES}/${slug}`} | ||
className={styles['category-list__link']} | ||
onClick={() => { | ||
dispatch(setCategoryId(id)) | ||
dispatch(setCategorySlug(slug)) | ||
}}> | ||
{name} ({count}) | ||
</Link> | ||
</li> | ||
<SideBar | ||
key={item.id} | ||
isVisible={true} | ||
title={`${item.name} (${item.total_count})`} | ||
onClick={() => { | ||
dispatch(setCategoryId(item.id)) | ||
dispatch(setCategorySlug(item.slug)) | ||
}} | ||
to={`${Routes.CATEGORIES}/${item.slug}`} | ||
activeElement={itemActive} | ||
branch={branch} | ||
itemName={item.name}> | ||
<ul role="list"> | ||
{item.branches.map(el => ( | ||
<li key={el.id} className={styles['category-list__item']}> | ||
<NavLink | ||
role="link" | ||
to={`${Routes.CATEGORIES}/${el.slug}`} | ||
className={({ isActive }) => | ||
isActive ? styles['category-list__link_active'] : styles['category-list__link'] | ||
} | ||
onClick={() => { | ||
dispatch(setCategoryId(el.id)) | ||
dispatch(setCategorySlug(el.slug)) | ||
}}> | ||
{el.name} ({el.products_count}) | ||
</NavLink> | ||
</li> | ||
))} | ||
</ul> | ||
</SideBar> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters