-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathTextPopup.jsx
More file actions
340 lines (314 loc) · 9.25 KB
/
TextPopup.jsx
File metadata and controls
340 lines (314 loc) · 9.25 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React, { useState, useContext } from "react";
import PropTypes from "prop-types";
import Button from "../../Button/Button";
import RadioButton from "../../UI/RadioButton";
import "react-datepicker/dist/react-datepicker.css";
import { MdClose } from "react-icons/md";
import { MdOutlineSearch } from "react-icons/md";
import { createUseStyles } from "react-jss";
import ToggleCheckbox from "components/UI/ToggleCheckbox";
import UserContext from "contexts/UserContext";
import { selectAllCheckboxes } from "helpers/util";
const useStyles = createUseStyles(theme => ({
container: {
display: "flex",
flexDirection: "column",
maxWidth: "25rem",
color: theme.colors.secondary.darkNavy
},
searchBarWrapper: {
width: "100%",
position: "relative",
alignSelf: "center",
marginBottom: "0.5rem"
},
searchBar: {
maxWidth: "100%",
width: "100%",
padding: "12px 12px 12px 12px",
boxSizing: "border-box"
// marginRight: "0.5rem"
},
searchIcon: {
position: "absolute",
right: "16px",
top: "14px"
},
listItem: {
display: "flex",
flexDirection: "row",
alignItems: "center",
height: "2rem",
gap: "0.2em",
"&:hover": {
backgroundColor: "lightblue"
},
"& span": {
maxWidth: "25ch",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis"
}
},
toggleButton: {
marginRight: "0",
marginTop: "4px",
marginBottom: "4px",
backgroundColor: "transparent",
border: "0",
cursor: "pointer",
textDecoration: "underline",
display: "flex",
fontWeight: "normal",
color: theme.colors.secondary.darkNavy
}
}));
const TextPopup = ({
projects,
filter,
close,
header,
criteria,
setCriteria,
order,
orderBy,
setSort,
setCheckedProjectIds,
setSelectAllChecked,
droOptions
}) => {
const userContext = useContext(UserContext);
const property = header.accessor || header.id;
const loggedInUserName = `${userContext?.account?.lastName}, ${userContext?.account?.firstName}`;
const getDisplayValue = value => {
if (property === "droName" && value === "") {
return "No DRO Assigned";
}
return value;
};
const getInternalValue = displayValue => {
if (property === "droName" && displayValue === "No DRO Assigned") {
return "";
}
return displayValue;
};
const classes = useStyles();
const [newOrder, setNewOrder] = useState(
header.id !== orderBy ? null : order
);
const [selectedListItems, setSelectedListItems] = useState(
(criteria[header.id + "List"] || []).map(s => ({
value: getDisplayValue(s),
label: getDisplayValue(s)
}))
);
const [searchString, setSearchString] = useState("");
const initiallyChecked = o => criteria[header.id + "List"].includes(o);
// To build the drop-down list, we want to apply all the criteria that
// are currently selected EXCEPT the criteria we are currently editing.
const listCriteria = { ...criteria, [header.id + "List"]: [] };
const filteredProjects = projects.filter(p => filter(p, listCriteria));
// const property = header.id == "author" ? "fullname" : header.id;
let selectOptions;
if (property === "droName" && droOptions) {
selectOptions = droOptions.map(dro => dro.name);
selectOptions.push("No DRO Assigned");
} else if (property === "author" && droOptions) {
let hasLoggedInUserInList = false;
selectOptions = [
...new Set(
filteredProjects.map(p => {
const name = `${p.lastName}, ${p.firstName}`;
if (p.loginId === userContext?.account?.id) {
hasLoggedInUserInList = true;
}
return name;
})
)
]
.filter(value => value !== null && value !== loggedInUserName)
.sort((a, b) => {
return a.localeCompare(b, "en", { sensitivity: "base" });
})
.sort(
(a, b) => (initiallyChecked(b) ? 1 : 0) - (initiallyChecked(a) ? 1 : 0)
);
if (hasLoggedInUserInList) selectOptions.unshift(loggedInUserName);
} else {
selectOptions = [...new Set(filteredProjects.map(p => p[property]))]
.filter(value => value !== null && value !== "")
.sort((a, b) => {
return a.localeCompare(b, "en", { sensitivity: "base" });
})
.sort(
(a, b) => (initiallyChecked(b) ? 1 : 0) - (initiallyChecked(a) ? 1 : 0)
);
}
const filteredOptions = selectOptions
.filter(o => !!o)
.filter(opt => opt.toLowerCase().includes(searchString.toLowerCase()));
const onChangeSearchString = e => {
setSearchString(e.target.value);
};
const handleCheckboxChange = e => {
const optionValue = e.target.name;
if (!e.target.checked) {
const newSelectedListItems = selectedListItems.filter(
selectedOption => selectedOption.value !== optionValue
);
setSelectedListItems(newSelectedListItems);
} else {
const newSelectedListItems = [
...selectedListItems,
{ value: optionValue, label: optionValue }
];
setSelectedListItems(newSelectedListItems);
}
};
const isChecked = optionValue => {
const checked = selectedListItems.find(
option => option.value === optionValue
);
return !!checked;
};
const applyChanges = () => {
let selectedValues = selectedListItems.map(sli =>
getInternalValue(sli.value)
);
setCriteria({
...criteria,
[header.id + "List"]: selectedValues
});
if (newOrder) {
setSort(header.id, newOrder);
}
if (setCheckedProjectIds) setCheckedProjectIds([]);
if (setSelectAllChecked) setSelectAllChecked(false);
close();
};
const setDefault = () => {
setNewOrder(null);
setSelectedListItems([]);
if (setCheckedProjectIds) setCheckedProjectIds([]);
if (setSelectAllChecked) setSelectAllChecked(false);
};
return (
<div className={classes.container}>
<div style={{ display: "flex", justifyContent: "flex-end" }}>
<MdClose
style={{
backgroundColor: "transparent",
color: "black",
position: "absolute",
top: "0.5rem",
right: "0.5rem"
}}
alt={`Close popup`}
onClick={close}
/>
</div>
<div style={{ display: "flex", flexDirection: "column" }}>
<RadioButton
label="Sort A-Z"
value="asc"
checked={newOrder === "asc"}
onChange={() => setNewOrder("asc")}
/>
<RadioButton
label="Sort Z-A"
value="desc"
checked={newOrder === "desc"}
onChange={() => setNewOrder("desc")}
/>
<hr style={{ width: "100%" }} />
</div>
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "baseline"
}}
>
<div style={{ display: "flex" }}>
<button
className={classes.toggleButton}
onClick={() =>
selectAllCheckboxes(filteredOptions, setSelectedListItems)
}
>
Select all {filteredOptions.length}
</button>
<div style={{ display: "flex", alignItems: "center" }}>-</div>
<button
className={classes.toggleButton}
onClick={() => setSelectedListItems([])}
>
Clear
</button>
</div>
<div>{`${selectedListItems.length} selected`}</div>
</div>
<div className={classes.searchBarWrapper}>
<input
type="text"
value={searchString}
onChange={onChangeSearchString}
className={classes.searchBar}
/>
<MdOutlineSearch className={classes.searchIcon} alt="Search Icon" />
</div>
<div style={{ overflow: "auto", maxHeight: "12rem" }}>
{/* <pre>{JSON.stringify(selectedListItems, null, 2)}</pre> */}
{/* <pre>{JSON.stringify(options, null, 2)}</pre> */}
{filteredOptions.map(o => {
const checked = isChecked(o);
return (
<div key={o} className={classes.listItem}>
<ToggleCheckbox
checked={checked}
onChange={() =>
handleCheckboxChange({
target: {
name: o,
checked: !isChecked(o)
}
})
}
label={o}
/>
<span>{o === loggedInUserName ? `${o} (Me)` : o}</span>
</div>
);
})}
</div>
<hr style={{ width: "100%" }} />
<div style={{ display: "flex", justifyContent: "center" }}>
<Button onClick={setDefault} variant="outlined">
Reset
</Button>
<Button
onClick={applyChanges}
variant="contained"
color={"colorPrimary"}
>
Apply
</Button>
</div>
</div>
);
};
TextPopup.propTypes = {
projects: PropTypes.any,
filter: PropTypes.func,
close: PropTypes.func,
header: PropTypes.any,
criteria: PropTypes.any,
setCriteria: PropTypes.func,
order: PropTypes.string,
orderBy: PropTypes.string,
setSort: PropTypes.func,
setCheckedProjectIds: PropTypes.func,
setSelectAllChecked: PropTypes.func,
droOptions: PropTypes.array
};
export default TextPopup;