Skip to content

Commit 015aaf6

Browse files
authored
Update pagination logic (#854)
* feat: consider pagination in empty state component * fix: reset page param if set, when filters are updated
1 parent 0b99c6a commit 015aaf6

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
import { Button } from 'nova-ui-kit'
2+
import { ReactNode } from 'react'
23
import { STRING, translate } from 'utils/language'
34
import { useFilters } from 'utils/useFilters'
5+
import { usePagination } from 'utils/usePagination'
46

57
export const EmptyState = () => {
68
const { filters, activeFilters, clearFilter } = useFilters()
9+
const { pagination, resetPage } = usePagination()
710

8-
return (
9-
<div className="flex flex-col gap-6 items-center py-24">
10-
<span className="body-base text-muted-foreground">
11-
{translate(
12-
activeFilters.length
13-
? STRING.MESSAGE_NO_RESULTS_FOR_FILTERING
14-
: STRING.MESSAGE_NO_RESULTS
15-
)}
16-
</span>
17-
{activeFilters.length ? (
11+
if (pagination.page) {
12+
return (
13+
<Container>
14+
<p>{translate(STRING.MESSAGE_NO_RESULTS_FOR_PAGE)}</p>
15+
<Button onClick={() => resetPage()}>
16+
{translate(STRING.RESET_PAGE)}
17+
</Button>
18+
</Container>
19+
)
20+
}
21+
22+
if (activeFilters.length) {
23+
return (
24+
<Container>
25+
<p>{translate(STRING.MESSAGE_NO_RESULTS_FOR_FILTERING)}</p>
1826
<Button
1927
onClick={() => filters.map((filter) => clearFilter(filter.field))}
2028
>
2129
{translate(STRING.CLEAR_FILTERS)}
2230
</Button>
23-
) : null}
24-
</div>
31+
</Container>
32+
)
33+
}
34+
35+
return (
36+
<Container>
37+
<p>{translate(STRING.MESSAGE_NO_RESULTS)}</p>
38+
</Container>
2539
)
2640
}
41+
42+
const Container = ({ children }: { children: ReactNode }) => (
43+
<div className="flex flex-col gap-6 items-center py-24">{children}</div>
44+
)

ui/src/utils/language.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export enum STRING {
2424
QUEUED,
2525
REFRESH,
2626
RESET,
27+
RESET_PAGE,
2728
RETRY,
2829
RERUN,
2930
SAVE,
@@ -165,6 +166,7 @@ export enum STRING {
165166
MESSAGE_NO_IMAGE,
166167
MESSAGE_NO_RESULTS,
167168
MESSAGE_NO_RESULTS_FOR_FILTERING,
169+
MESSAGE_NO_RESULTS_FOR_PAGE,
168170
MESSAGE_PASSWORD_FORMAT,
169171
MESSAGE_PASSWORD_UPDATED,
170172
MESSAGE_PERMISSIONS_MISSING,
@@ -293,6 +295,7 @@ const ENGLISH_STRINGS: { [key in STRING]: string } = {
293295
[STRING.QUEUED]: 'Queued',
294296
[STRING.REFRESH]: 'Refresh',
295297
[STRING.RESET]: 'Reset',
298+
[STRING.RESET_PAGE]: 'Reset page',
296299
[STRING.RETRY]: 'Retry',
297300
[STRING.RERUN]: 'Re-run',
298301
[STRING.SAVE]: 'Save',
@@ -443,7 +446,9 @@ const ENGLISH_STRINGS: { [key in STRING]: string } = {
443446
[STRING.MESSAGE_NO_IMAGE]: 'No image',
444447
[STRING.MESSAGE_NO_RESULTS]: 'No results to show.',
445448
[STRING.MESSAGE_NO_RESULTS_FOR_FILTERING]:
446-
'No results to show for the current fitering.',
449+
'No results to show for the current filtering.',
450+
[STRING.MESSAGE_NO_RESULTS_FOR_PAGE]:
451+
'No results to show for the current page.',
447452
[STRING.MESSAGE_PASSWORD_FORMAT]:
448453
'The password must contain at least 8 characters and cannot be entirely numeric.',
449454
[STRING.MESSAGE_PASSWORD_UPDATED]: 'Your password has been updated!',

ui/src/utils/useFilters.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isBefore, isValid } from 'date-fns'
22
import { useSearchParams } from 'react-router-dom'
3+
import { SEARCH_PARAM_KEY_PAGE } from './usePagination'
34

45
export const AVAILABLE_FILTERS: {
56
label: string
@@ -141,13 +142,25 @@ export const useFilters = (defaultFilters?: { [field: string]: string }) => {
141142
const addFilter = (field: string, value: string) => {
142143
if (AVAILABLE_FILTERS.some((filter) => filter.field === field)) {
143144
searchParams.set(field, value)
145+
146+
// Reset page param if set, when filters are updated
147+
if (searchParams.has(SEARCH_PARAM_KEY_PAGE)) {
148+
searchParams.delete(SEARCH_PARAM_KEY_PAGE)
149+
}
150+
144151
setSearchParams(searchParams)
145152
}
146153
}
147154

148155
const clearFilter = (field: string) => {
149156
if (AVAILABLE_FILTERS.some((filter) => filter.field === field)) {
150157
searchParams.delete(field)
158+
159+
// Reset page param if set, when filters are updated
160+
if (searchParams.has(SEARCH_PARAM_KEY_PAGE)) {
161+
searchParams.delete(SEARCH_PARAM_KEY_PAGE)
162+
}
163+
151164
setSearchParams(searchParams)
152165
}
153166
}

ui/src/utils/usePagination.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ const useSearchParam = ({
2929
}
3030
}
3131

32-
const clearValue = () => {
32+
const resetValue = () => {
3333
if (searchParams.has(key)) {
3434
searchParams.delete(key)
3535
setSearchParams(searchParams)
3636
}
3737
}
3838

39-
return { value, setValue, clearValue }
39+
return { value, setValue, resetValue }
4040
}
4141

42-
export const usePagination = ({ perPage }: { perPage?: number } = {}) => {
43-
const { value: page, setValue: setPage } = useSearchParam({
42+
const usePagination = ({ perPage }: { perPage?: number } = {}) => {
43+
const {
44+
value: page,
45+
setValue: setPage,
46+
resetValue: resetPage,
47+
} = useSearchParam({
4448
key: SEARCH_PARAM_KEY_PAGE,
4549
defaultValue: DEFAULT_PAGINATION.page,
4650
})
@@ -50,6 +54,9 @@ export const usePagination = ({ perPage }: { perPage?: number } = {}) => {
5054
page,
5155
perPage: perPage ?? DEFAULT_PAGINATION.perPage,
5256
},
57+
resetPage,
5358
setPage,
5459
}
5560
}
61+
62+
export { SEARCH_PARAM_KEY_PAGE, usePagination }

0 commit comments

Comments
 (0)