-
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.
Merge pull request #398 from Studio-Yandex-Practicum/enhancement-340-…
…sidebar #340-sidebar
- Loading branch information
Showing
10 changed files
with
158 additions
and
64 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,63 @@ | ||
import type { FC } from 'react' | ||
import { type FC } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { NavLink } 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 { useHandleActiveItem } from '@/features/CategoryItem/models/useHandleActiveItem' | ||
import SideBar from '@/features/SideBar' | ||
import { Routes } from '@/shared/config/routerConfig/routes' | ||
import Link from '@/shared/ui/Link/Link' | ||
import type { 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 { itemActive, branch } = useHandleActiveItem(item) | ||
|
||
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)) | ||
}} | ||
categorySidebar={{ | ||
to: `${Routes.CATEGORIES}/${item.slug}`, | ||
activeElement: itemActive, | ||
branch: branch, | ||
itemName: item.name | ||
}}> | ||
{item.branches && ( | ||
<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useSelector } from 'react-redux' | ||
import { useParams } from 'react-router-dom' | ||
|
||
import { getCategorySelector } from '@/widgets/CategoryList/selectors/selectors' | ||
import type { BranchesData, MainCategoryInfo } from '@/widgets/CategoryList/types/types' | ||
|
||
/** | ||
* Функция сравнения и вывода активной категории и подкатегории | ||
* @param {MainCategoryInfo} item - значение, которое нужно заменить; | ||
*/ | ||
export const useHandleActiveItem = (item: MainCategoryInfo) => { | ||
const { slug } = useParams() | ||
const getMainCategories = useSelector(getCategorySelector) | ||
|
||
const [itemActive, setItemActive] = useState<MainCategoryInfo | BranchesData>() | ||
const [branch, setBranch] = useState<MainCategoryInfo | BranchesData>() | ||
|
||
const activeMainCategory = getMainCategories.find(el => el.slug === slug) | ||
const activeBranchCategory = item.branches.find(el => el.slug === slug) | ||
|
||
useEffect(() => { | ||
if (activeMainCategory) { | ||
setItemActive(activeMainCategory) | ||
} else if (activeBranchCategory) { | ||
setItemActive(activeBranchCategory) | ||
setBranch(getMainCategories.find(el => el.branches.find(el => el.name === itemActive?.name))) | ||
} | ||
}, [itemActive]) | ||
return { itemActive, branch } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { BranchesData, MainCategoryInfo } from '@/widgets/CategoryList/types/types' | ||
|
||
export type TCategorySidebar = { | ||
to?: string | undefined | ||
activeElement?: MainCategoryInfo | BranchesData | ||
branch?: MainCategoryInfo | BranchesData | ||
itemName?: string | ||
} |
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
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