-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
11 changed files
with
130 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * as taskModel from './tasks'; | ||
export * as taskQueryModel from './query-config'; | ||
export * as taskPaginationModel from './pagination-config'; |
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,39 @@ | ||
import { createStore, createEvent } from 'effector'; | ||
import { useUnit } from 'effector-react'; | ||
import { persist } from 'effector-storage'; | ||
import { createStorageAdapter } from '@/shared/utils'; | ||
|
||
export interface PaginationConfig { | ||
page: number; | ||
size: number; | ||
} | ||
|
||
const setPaginationConfig = createEvent<Partial<PaginationConfig>>(); | ||
|
||
const resetPaginationConfig = createEvent(); | ||
|
||
export const $paginationConfig = createStore<PaginationConfig>({ | ||
page: 1, | ||
size: 10, | ||
}) | ||
.on(setPaginationConfig, (store, payload) => ({ ...store, ...payload })) | ||
.reset(resetPaginationConfig); | ||
|
||
persist({ | ||
key: 'pagination-config', | ||
store: $paginationConfig, | ||
adapter: createStorageAdapter(), | ||
}); | ||
|
||
const usePagination = () => { | ||
return useUnit($paginationConfig); | ||
}; | ||
|
||
export const events = { | ||
setPaginationConfig, | ||
resetPaginationConfig, | ||
}; | ||
|
||
export const selectors = { | ||
usePagination, | ||
}; |
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,5 +1,6 @@ | ||
export * from './tasks-filter'; | ||
export * from './filtered-tasks'; | ||
export * from './tasks-pagination'; | ||
export * from './task-view'; | ||
export * from './task-create-form'; | ||
export * from './task-edit-form'; |
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,2 @@ | ||
export const pageSizes = ['10', '25', '50']; | ||
export const defaultPageSize = 10; |
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 @@ | ||
export * from './ui'; |
43 changes: 43 additions & 0 deletions
43
src/features/task/ui/tasks-pagination/ui/TasksPagination.tsx
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,43 @@ | ||
import { Center, Group, Pagination, SegmentedControl } from '@mantine/core'; | ||
import { pageSizes, defaultPageSize } from '../const'; | ||
import { taskModel, taskPaginationModel } from '@/entities/task'; | ||
import { tryParseInt } from '@/shared/utils'; | ||
|
||
export const TasksPagination = () => { | ||
const count = taskModel.selectors.useTasksCount(); | ||
const pagination = taskPaginationModel.selectors.usePagination(); | ||
const page = pagination.page ?? 1; | ||
const size = pagination.size ?? defaultPageSize; | ||
const total = Math.ceil(count / size); | ||
|
||
const onPageChange = (value: number) => { | ||
taskPaginationModel.events.setPaginationConfig({ | ||
page: value, | ||
}); | ||
}; | ||
|
||
const onSizeChange = (value: string) => { | ||
taskPaginationModel.events.setPaginationConfig({ | ||
page: 1, | ||
size: tryParseInt(value, defaultPageSize), | ||
}); | ||
}; | ||
|
||
if (count <= defaultPageSize) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Center> | ||
<Group> | ||
<Pagination value={page} total={total} onChange={onPageChange} /> | ||
<SegmentedControl | ||
value={size.toString()} | ||
defaultValue={defaultPageSize.toString()} | ||
data={pageSizes} | ||
onChange={onSizeChange} | ||
/> | ||
</Group> | ||
</Center> | ||
); | ||
}; |
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 @@ | ||
export * from './TasksPagination'; |
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