Skip to content

Commit

Permalink
remember last page number (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Jul 5, 2024
1 parent 0d351d0 commit 04e1968
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions ui2/src/cconstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ export const STORAGE_KEY_PAGINATION_MITEMS_PER_PAGE = "mitems_per_page"
export const STORAGE_KEY_PAGINATION_SITEMS_PER_PAGE = "sitems_per_page"

export const PAGINATION_PAGE_SIZES = ["5", "10", "15", "25", "50"]
export const INITIAL_PAGE_SIZE = 5
2 changes: 1 addition & 1 deletion ui2/src/components/users/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function UsersList() {
</Table.Th>
<Table.Th>Username</Table.Th>
<Table.Th>Email</Table.Th>
<Table.Th>UUID</Table.Th>
<Table.Th>ID</Table.Th>
</Table.Tr>
</Table.Thead>
<Table.Tbody>{groupRows}</Table.Tbody>
Expand Down
4 changes: 3 additions & 1 deletion ui2/src/components/users/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {Pagination, Skeleton, Group, Select} from "@mantine/core"
import {useDispatch, useSelector} from "react-redux"
import {selectPagination, fetchUsers} from "@/slices/users"
import {selectPagination, fetchUsers, selectLastPageSize} from "@/slices/users"
import classes from "./Pagination.module.css"
import {PAGINATION_PAGE_SIZES} from "@/cconstants"

export default function UserPagination() {
const dispatch = useDispatch()
const pagination = useSelector(selectPagination)
const lastPageSize = useSelector(selectLastPageSize)

const onPageNumberChange = (page: number) => {
dispatch(fetchUsers({pageNumber: page, pageSize: pagination?.pageSize}))
Expand All @@ -26,6 +27,7 @@ export default function UserPagination() {
className={classes.select}
value={`${pagination.pageSize}`}
onChange={onPageSizeChange}
defaultValue={`${lastPageSize}`}
data={PAGINATION_PAGE_SIZES}
/>
</Group>
Expand Down
6 changes: 5 additions & 1 deletion ui2/src/pages/users/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default function UsersListPage() {
}

export async function loader() {
await store.dispatch(fetchUsers({pageNumber: 1}))
const state = store.getState()

await store.dispatch(
fetchUsers({pageNumber: 1, pageSize: state.users.lastPageSize})
)

return null
}
13 changes: 9 additions & 4 deletions ui2/src/slices/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import axios from "@/httpClient"

import {RootState} from "@/app/types"
import type {CreateUser, User, UserDetails, Paginated} from "@/types"

import type {SliceStateStatus, SliceStateError} from "@/types"
import {INITIAL_PAGE_SIZE} from "@/cconstants"

export type Pagination = {
numPages: number
Expand All @@ -22,17 +22,17 @@ export type ExtraStateType = {
error: SliceStateError
selectedIds: Array<string>
pagination: Pagination | null
lastPageSize: number
}

export const extraState: ExtraStateType = {
status: "idle",
error: null,
selectedIds: [],
pagination: null
pagination: null,
lastPageSize: INITIAL_PAGE_SIZE
}

export const INITIAL_PAGE_SIZE = 5

const usersAdapter = createEntityAdapter({
selectId: (user: User) => user.id,
sortComparer: (u1, u2) => u1.username.localeCompare(u2.username)
Expand Down Expand Up @@ -66,6 +66,7 @@ const usersSlice = createSlice({
pageNumber: action.payload.page_number,
pageSize: action.payload.page_size
}
state.lastPageSize = action.payload.page_size
})
builder.addCase(fetchUser.fulfilled, (state, action) => {
const newUser = action.payload
Expand Down Expand Up @@ -175,3 +176,7 @@ export const selectUsersByIds = (state: RootState, userIds: string[]) => {
export const selectPagination = (state: RootState): Pagination | null => {
return state.users.pagination
}

export const selectLastPageSize = (state: RootState): number => {
return state.users.lastPageSize
}

0 comments on commit 04e1968

Please sign in to comment.