-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterBar.jsx
178 lines (165 loc) · 4.21 KB
/
FilterBar.jsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
useLocation,
useNavigate,
useSearchParams,
Link,
} from "react-router-dom";
import { useState, useEffect, useContext } from "react";
import { UserContext } from "./contexts/UserContext";
import { getTopicList } from "./apiFunctions";
import { capitaliseFirstLetter } from "./utils";
import SortBar from "./SortBar";
const FilterBar = () => {
const { pathname: path } = useLocation();
const { user, setUser } = useContext(UserContext);
const [searchParams, setSearchParams] = useSearchParams();
const [isLoading, setIsLoading] = useState(true);
const [topicList, setTopicList] = useState([]);
const [buttonDisable, setButtonDisable] = useState(true);
const [buttonClear, setButtonClear] = useState(false);
const [topicValue, setTopicValue] = useState("");
const [authorValue, setAuthorValue] = useState("");
const navigate = useNavigate();
const articleView = /\/articles\/[0-9]+/i.test(path);
const loginView = path === "/login";
const newParams = Object.fromEntries([...searchParams]);
useEffect(() => {
setIsLoading(true);
getTopicList().then((topics) => {
setTopicList((currentTopics) => {
const topicURL = searchParams.get("topic") || "";
if (
topicURL &&
!topics.find((topic) => topic.slug === topicURL)
) {
return [{ slug: topicURL }, ...topics];
}
return topics;
});
setIsLoading(false);
});
}, [topicValue]);
useEffect(() => {
const newAuthorValue = searchParams.get("author") || "";
setAuthorValue(newAuthorValue);
if (newAuthorValue) {
setClearButton();
}
const newTopic = searchParams.get("topic") || "";
setTopicValue(newTopic);
}, [searchParams]);
const authorChanged = (event) => {
const currentValue = event.target.value;
if (currentValue !== "") {
currentValue === newParams.author
? setClearButton()
: setSearchButton(false);
} else if (newParams.author) {
setClearButton();
} else {
setButtonDisable(true);
}
setAuthorValue(currentValue);
};
const searchAuthor = (event) => {
event.preventDefault();
if (authorValue) {
newParams.author = authorValue;
} else {
delete newParams.author;
}
setSearchParams(newParams);
};
const updateTopicFilter = (event) => {
const currentValue = event.target.value;
if (currentValue) {
newParams.topic = event.target.value;
} else {
delete newParams.topic;
}
setSearchParams(newParams);
};
const actionSearchClear = (event) => {
if (buttonClear) {
event.preventDefault();
delete newParams.author;
setSearchParams(newParams);
setAuthorValue("");
setSearchButton();
}
};
const setSearchButton = (disabled = true) => {
setButtonDisable(disabled);
setButtonClear(false);
};
const setClearButton = () => {
setButtonClear(true);
setButtonDisable(false);
};
return (
<div
id={articleView || loginView ? "filterBackBar1" : "filterBackBar2"}
>
{articleView || loginView ? (
<Link id="backToArticles" to="/">
← Articles
</Link>
) : (
<div id="topicFilter">
<label id="filterLabel" htmlFor="topicSelector">
Filter articles
</label>
<select
id="topicSelector"
name="topicFilter"
disabled={isLoading}
value={topicValue}
onChange={updateTopicFilter}
>
<option value="">All topics</option>
{topicList.map((topic) => {
return (
<option key={topic.slug} value={topic.slug}>
{capitaliseFirstLetter(topic.slug)}
</option>
);
})}
</select>
</div>
)}
{!articleView && !loginView && (
<form id="authorSearchForm" onSubmit={searchAuthor}>
<input
type="text"
id="authorSearchField"
placeholder="All authors"
autoComplete="off"
value={authorValue}
onChange={authorChanged}
/>
<button
id="authorFilterButton"
className="brandedButton"
disabled={buttonDisable}
onClick={actionSearchClear}
>
{buttonClear ? "Clear" : "Search"}
</button>
</form>
)}
{loginView && user ? (
<button
id="logoutButton"
onClick={() => {
setUser(null);
navigate(-1);
}}
>
Logout
</button>
) : null}
{!articleView && !loginView && <SortBar />}
</div>
);
};
export default FilterBar;