-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSearch.tsx
113 lines (99 loc) · 3.15 KB
/
Search.tsx
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
import React, { useMemo, useRef, useState } from "react";
import styled, { css } from "styled-components";
import Skeleton from "react-loading-skeleton";
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
import { useDebounce } from "react-use";
import { Searchbar, DropdownCascader } from "@kleros/ui-components-library";
import { isEmpty, isUndefined } from "utils/index";
import { decodeURIFilter, encodeURIFilter, useRootPath } from "utils/uri";
import { rootCourtToItems, useCourtTree } from "queries/useCourtTree";
import { landscapeStyle } from "styles/landscapeStyle";
import { responsiveSize } from "styles/responsiveSize";
const Container = styled.div`
display: flex;
flex-direction: column;
gap: ${responsiveSize(8, 16)};
${landscapeStyle(
() => css`
flex-direction: row;
`
)}
`;
const SearchBarContainer = styled.div`
width: 100%;
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 5px;
z-index: 0;
`;
const StyledSearchbar = styled(Searchbar)`
flex: 1;
flex-basis: 310px;
input {
font-size: 16px;
height: 45px;
padding-top: 0px;
padding-bottom: 0px;
}
`;
const Search: React.FC = () => {
const { page, order, filter } = useParams();
const location = useRootPath();
const decodedFilter = decodeURIFilter(filter ?? "all");
const { id: searchValue, ...filterObject } = decodedFilter;
const [search, setSearch] = useState(searchValue ?? "");
const initialRenderRef = useRef(true);
const navigate = useNavigate();
const [searchParams] = useSearchParams();
useDebounce(
() => {
if (initialRenderRef.current && isEmpty(search)) {
initialRenderRef.current = false;
return;
}
initialRenderRef.current = false;
const newFilters = isEmpty(search) ? { ...filterObject } : { ...filterObject, id: search };
const encodedFilter = encodeURIFilter(newFilters);
navigate(`${location}/${page}/${order}/${encodedFilter}?${searchParams.toString()}`);
},
500,
[search]
);
const { data: courtTreeData } = useCourtTree();
const items = useMemo(() => {
if (!isUndefined(courtTreeData)) {
const courts = [rootCourtToItems(courtTreeData.court!, "id")];
courts.push({ label: "All Courts", value: "all" });
return courts;
}
return undefined;
}, [courtTreeData]);
return (
<Container>
{items ? (
<DropdownCascader
items={items}
placeholder={"Select Court"}
onSelect={(value) => {
const { court: _, ...filterWithoutCourt } = decodedFilter;
const newFilter = value === "all" ? filterWithoutCourt : { ...decodedFilter, court: value.toString() };
navigate(`${location}/${page}/${order}/${encodeURIFilter(newFilter)}?${searchParams.toString()}`);
}}
/>
) : (
<Skeleton width={240} height={42} />
)}
<SearchBarContainer>
<StyledSearchbar
dir="auto"
type="text"
placeholder="Search By ID"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
</SearchBarContainer>
</Container>
);
};
export default Search;