-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactRouter.ts
More file actions
132 lines (120 loc) · 3.52 KB
/
Copy pathreactRouter.ts
File metadata and controls
132 lines (120 loc) · 3.52 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
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useLocation, useNavigate } from 'react-router-dom'
import { useCallback, useMemo } from 'react'
import {
Query,
AbstractQuery,
ParseQuery,
AbstractQueryOptions,
useAbstractQuery,
PaginationQuery,
useAbstractQueryAndPagination,
Router,
} from '../engine'
import { RouterWithHistoryOptions } from './shared'
const DEFAULT_REACT_ROUTER_OPTIONS: RouterWithHistoryOptions = {
setQueryMethod: 'replace',
}
const useReactRouter = (
options: undefined | Partial<RouterWithHistoryOptions>,
): Router => {
const navigate = useNavigate()
const { pathname, search } = useLocation()
const mergedOptions = useMemo(
() => ({ ...DEFAULT_REACT_ROUTER_OPTIONS, ...options }),
[options],
)
const getQuery = useCallback(
(defaultQuery: Query) => {
const query: Query = {}
const urlSearchParams = new URLSearchParams(search)
for (const key of urlSearchParams.keys()) {
const value = urlSearchParams.getAll(key)
query[key] = value.length === 1 ? (value[0] as string) : value
}
return { ...defaultQuery, ...query }
},
[search],
)
const setQuery = useCallback(
(query: Partial<Query>, defaultQuery: Query) => {
const urlSearchParams = new URLSearchParams(search)
for (const [key, value] of Object.entries(query)) {
if (value !== undefined) {
const defaultValue = defaultQuery[key]
if (
defaultValue !== undefined &&
value.toString() === defaultValue.toString()
) {
urlSearchParams.delete(key)
} else {
const [firstValue, ...restValues] = Array.isArray(value)
? value
: [value]
if (firstValue !== undefined) {
urlSearchParams.set(key, firstValue)
}
for (const restValue of restValues) {
urlSearchParams.append(key, restValue)
}
}
}
}
navigate(
{ pathname, search: urlSearchParams.toString() },
{ replace: mergedOptions.setQueryMethod === 'replace' },
)
},
[mergedOptions.setQueryMethod, navigate, pathname, search],
)
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 = useReactRouter(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 = useReactRouter(options)
return useAbstractQueryAndPagination(
router,
parseQuery,
defaultQuery,
defaultPagination,
options,
)
}
export const usePagination = (
defaultPagination?: Partial<PaginationQuery>,
options?: Partial<AbstractQueryOptions & RouterWithHistoryOptions>,
) => {
const router = useReactRouter(options)
const { page, size, setPage, setSize, setPagination, resetPagination } =
useAbstractQueryAndPagination(
router,
undefined,
undefined,
defaultPagination,
options,
)
return { page, size, setPage, setSize, setPagination, resetPagination }
}