-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqueryParamsUtils.jsx
More file actions
59 lines (53 loc) · 1.52 KB
/
queryParamsUtils.jsx
File metadata and controls
59 lines (53 loc) · 1.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
/* This functions are used to serialize and deserialize URL params, to values for ordering and filter
Data in tables about filter and ordering are saved as object, but in the url is a string.
These funcitions helps to convert from obj to string and vice versa
*/
/**
* This function is used to convert filters from obj to string
* @argument {Array<Object>} filters
* @return {Object}
*/
const serializeFilterParams = (filters) =>
filters.reduce(
(acc, { id, value, }) => ({ ...acc, [id]: value, }),
{}
);
/**
* This function is used to convert filters from string to obj
* @argument {Object} filters
* @return {Array<Object>}
*/
const deserializeFilterParams = (filters) =>
Array.from(
Object.entries(filters).map(([filterKey, filterValue]) => ({
id: filterKey,
value: filterValue,
}))
);
/**
* This function is used to convert ordering key from obj to string
* @argument {Array<Object>} sortBy
* @return {String}
*/
const serializeSortByParams = (sortBy) =>
sortBy
.map(({ id, desc, }) => `${desc ? "-" : ""}${id.split("-")[0]}`)
.join(",");
/**
* This function is used to convert ordering key from string to obj
* @argument {String} filters
* @return {Array<Object>}
*/
const deserializeSortByParams = (sortByStr) =>
Array.from(
sortByStr.split(",").map((str) => ({
id: str.charAt(0) === "-" ? str.slice(1) : str,
desc: str.charAt(0) === "-",
}))
);
export {
serializeFilterParams,
deserializeFilterParams,
serializeSortByParams,
deserializeSortByParams
};