-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnextRouter.ts
More file actions
119 lines (110 loc) · 2.96 KB
/
Copy pathnextRouter.ts
File metadata and controls
119 lines (110 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { useRouter } from 'next/router'
import { useCallback, useMemo } from 'react'
import {
Query,
ParseQuery,
useAbstractQuery,
AbstractQuery,
AbstractQueryOptions,
Router,
} from '../engine/query'
import {
PaginationQuery,
useAbstractQueryAndPagination,
} from '../engine/pagination'
import { RouterWithHistoryOptions } from './shared'
const DEFAULT_NEXT_ROUTER_OPTIONS: RouterWithHistoryOptions = {
setQueryMethod: 'replace',
}
const useNextRouter = (
options: undefined | Partial<RouterWithHistoryOptions>,
): Router => {
const nextRouter = useRouter()
const mergedOptions = useMemo(
() => ({ ...DEFAULT_NEXT_ROUTER_OPTIONS, ...options }),
[options],
)
const getQuery = useCallback(
(defaultQuery: Query) => {
const query: Query = {}
for (const [key, value] of Object.entries(nextRouter.query)) {
if (value !== undefined) {
query[key] = value
}
}
return { ...defaultQuery, ...query }
},
[nextRouter.query],
)
const setQuery = useCallback(
(query: Partial<Query>, defaultQuery: Query) => {
const newQuery = { ...nextRouter.query, ...query }
const newQueryWithoutDefaults = Object.fromEntries(
Object.entries(newQuery).filter(
([key, value]) => value !== defaultQuery[key],
),
)
if (mergedOptions.setQueryMethod === 'push') {
void nextRouter.push({ query: newQueryWithoutDefaults }, undefined, {
shallow: true,
})
} else {
void nextRouter.replace({ query: newQueryWithoutDefaults }, undefined, {
shallow: true,
})
}
},
[nextRouter, mergedOptions],
)
return useMemo(
() => ({
getQuery,
setQuery,
}),
[getQuery, setQuery],
)
}
export const useQuery = <
TQuery extends AbstractQuery,
TDefaultQuery extends Partial<TQuery>,
>(
parseQuery: ParseQuery<TQuery>,
defaultQuery?: TDefaultQuery,
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
) => {
const router = useNextRouter(options)
return useAbstractQuery(router, parseQuery, defaultQuery, options)
}
export const useQueryAndPagination = <
TQuery extends AbstractQuery,
TDefaultQuery extends Partial<TQuery>,
>(
parseQuery: ParseQuery<TQuery>,
defaultQuery?: TDefaultQuery,
defaultPagination?: Partial<PaginationQuery>,
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
) => {
const router = useNextRouter(options)
return useAbstractQueryAndPagination(
router,
parseQuery,
defaultQuery,
defaultPagination,
options,
)
}
export const usePagination = (
defaultPagination?: Partial<PaginationQuery>,
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
) => {
const { page, size, setPage, setSize, setPagination, resetPagination } =
useQueryAndPagination(() => ({}), {}, defaultPagination, options)
return {
page,
size,
setPage,
setSize,
setPagination,
resetPagination,
}
}