-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactRouter.ts
More file actions
121 lines (112 loc) · 3.37 KB
/
Copy pathreactRouter.ts
File metadata and controls
121 lines (112 loc) · 3.37 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
import { useLocation, useNavigate } from 'react-router-dom'
import { useCallback, useMemo } from 'react'
import {
Query,
AbstractQuery,
ParseQuery,
AbstractQueryOptions,
useAbstractQuery,
PaginationQuery,
useAbstractQueryAndPagination,
Router,
} from '../engine'
import { QUERY_ARRAY_SEPARATOR, 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 = {}
for (const [key, value] of new URLSearchParams(search).entries()) {
const decodedValues = value
.split(QUERY_ARRAY_SEPARATOR)
.map((v) => decodeURIComponent(v))
query[key] =
decodedValues.length === 1
? (decodedValues[0] as string)
: decodedValues
}
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 === defaultQuery[key]) {
urlSearchParams.delete(key)
} else if (value !== undefined) {
let values: string[]
if (Array.isArray(value)) {
values = value
} else {
values = [value]
}
const encodedValues = values
.map((v) => encodeURIComponent(v))
.join(QUERY_ARRAY_SEPARATOR)
urlSearchParams.set(key, encodedValues)
}
}
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 { page, size, setPage, setSize, setPagination, resetPagination } =
useQueryAndPagination(() => ({}), {}, defaultPagination, options)
return { page, size, setPage, setSize, setPagination, resetPagination }
}