Skip to content

Commit

Permalink
#339-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Margarita22 committed May 5, 2024
1 parent d4f890a commit 545e9fc
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 66 deletions.
89 changes: 57 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"formik": "^2.4.5",
"leaflet": "^1.9.4",
"normalize-scss": "7.0.1",
"qs": "^6.12.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-input-mask": "^2.0.4",
Expand Down
2 changes: 2 additions & 0 deletions src/app/providers/StoreProvider/config/StateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ICartEntitySchema } from '@/entities/CartEntity/model/types/types'
import { IAboutUsSchema } from '@/pages/AboutUsPage/model/types/types'
import { ICartSchema } from '@/pages/CartPage/model/types'
import { IProductAmountStateSchema } from '@/features/CartEdit/model/types'
import { TNumberOfPageSchema } from '@/components/Pagination/types/types'

export interface StateSchema {
aboutUs: IAboutUsSchema
Expand All @@ -46,6 +47,7 @@ export interface StateSchema {
cartEntity: ICartEntitySchema
categoryFilters: ICategoryFiltersSchema
productAmount: IProductAmountStateSchema
pagination: TNumberOfPageSchema
}

export interface ThunkExtraArg {
Expand Down
4 changes: 3 additions & 1 deletion src/app/providers/StoreProvider/config/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { aboutUsReducer } from '@/pages/AboutUsPage/model/slice/aboutUsSlice'
import { cartReducer } from '@/pages/CartPage/model/slice'
import { cartEntityReducer } from '@/entities/CartEntity/model/slice/cartEntitySlice'
import { productAmountReducer } from '@/features/CartEdit/model/slice/productAmountSlice'
import { paginationSliceReducer } from '@/components/Pagination/slice/paginationSlice'

export type RootState = StateSchema

Expand Down Expand Up @@ -52,7 +53,8 @@ const rootReducer: ReducersMapObject<RootState> = {
cartEntity: cartEntityReducer,
cart: cartReducer,
categoryFilters: categoryFiltersSliceReducer,
productAmount: productAmountReducer
productAmount: productAmountReducer,
pagination: paginationSliceReducer
}

export function createReduxStore(initialState: RootState) {
Expand Down
16 changes: 14 additions & 2 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ interface DropdownProps extends React.HTMLProps<HTMLSelectElement> {
items: TSortOptions[]
defaultItem?: string
onSelect: React.ChangeEventHandler<HTMLSelectElement>
changeValue?: string
}

/**
* Компонент для инпута - выпадающего списка.
* @param {array} items - список элементов выпадающего списка для выбора;
* @param {string} defaultItem - выбранный по умолчанию элемент списка;
* @param {function} onSelect - функция, применяющая выбранное из списка значение;
* @param {string} changeValue - выбранное название фильтра;
*/
export const Dropdown: React.FC<DropdownProps> = ({ items, defaultItem, onSelect, ...props }) => {
export const Dropdown: React.FC<DropdownProps> = ({
items,
defaultItem,
onSelect,
changeValue,
...props
}) => {
const [selectedItem, setSelectedItem] = useState<string>(defaultItem || '')

const handleSelect = (e: ChangeEvent<HTMLSelectElement>) => {
Expand All @@ -26,7 +34,11 @@ export const Dropdown: React.FC<DropdownProps> = ({ items, defaultItem, onSelect
}

return (
<select className={styles.select} value={selectedItem} onChange={handleSelect} {...props}>
<select
className={styles.select}
value={changeValue ? changeValue : selectedItem}
onChange={handleSelect}
{...props}>
{items.map(item => (
<option value={item.name} key={item.name}>
{item.name}
Expand Down
16 changes: 13 additions & 3 deletions src/components/PageControls/PageControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type TPageControls = {
handleSortChange: ChangeEventHandler<HTMLSelectElement>
itemPerPageOptions: TSortOptions[]
sortOptions: TSortOptions[]
changeValueToFilterProducts?: string
changeValueToQuantityProducts?: string
}

/**
Expand All @@ -30,20 +32,28 @@ type TPageControls = {
* @param {function} handleSortChange - функция управления порядком сортировки карточек;
* @param {array} itemPerPageOptions - текущее кол-во отображаемых карточек на странице;
* @param {array} sortOptions - текущий выбранный порядок сортировки карточек;
* @param {string} changeValueToFilterProducts - наименование выбранного фильтра товаров по Названию/Цене/Модели
* @param {string} changeValueToQuantityProducts - наименование выбранного фильтра товаров по количеству в листинге
*/
export const PageControls: FC<TPageControls> = ({
cardView,
handleCardViewChange,
handleItemsPerPageChange,
handleSortChange,
itemPerPageOptions,
sortOptions
sortOptions,
changeValueToFilterProducts,
changeValueToQuantityProducts
}) => {
return (
<div className={styles['page-controls']}>
<div className={styles.dropdowns}>
<Dropdown items={sortOptions} onSelect={handleSortChange} />
<Dropdown items={itemPerPageOptions} onSelect={handleItemsPerPageChange} />
<Dropdown items={sortOptions} onSelect={handleSortChange} changeValue={changeValueToFilterProducts} />
<Dropdown
items={itemPerPageOptions}
onSelect={handleItemsPerPageChange}
changeValue={changeValueToQuantityProducts}
/>
</div>
<ul className={styles['cards-controls']}>
<li
Expand Down
4 changes: 2 additions & 2 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react'
import { type FC } from 'react'

import styles from './Pagination.module.scss'

Expand Down Expand Up @@ -35,7 +35,7 @@ export const Pagination: FC<TPaginationProps> = ({
{`<`}
</button>
<span className={styles['current-page']}>
Страница {currentPage} из {totalPages}
Страница {currentPage === 0 ? 1 : currentPage} из {totalPages}
</span>
<button
className={styles.button}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Pagination/selectors/selectors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RootState } from '@/app/providers/StoreProvider/config/store'

export const selectNumberOfPage = (state: RootState) => state.pagination.numberOfPage
21 changes: 21 additions & 0 deletions src/components/Pagination/slice/paginationSlice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createSlice } from '@reduxjs/toolkit'

import type { TNumberOfPageSchema } from '@/components/Pagination/types/types'

const initialState: TNumberOfPageSchema = {
numberOfPage: 1
}

const paginationSlice = createSlice({
name: 'pagination',
initialState,
reducers: {
setNumberOfPage(state, action) {
state.numberOfPage = action.payload
}
}
})

export const { reducer: paginationSliceReducer } = paginationSlice

export const { setNumberOfPage } = paginationSlice.actions
3 changes: 3 additions & 0 deletions src/components/Pagination/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type TNumberOfPageSchema = {
numberOfPage: number
}
Loading

0 comments on commit 545e9fc

Please sign in to comment.