-
-
Notifications
You must be signed in to change notification settings - Fork 806
/
FilterPlays.jsx
260 lines (238 loc) · 7.9 KB
/
FilterPlays.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
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
import { Modal } from 'common';
import { useState, useEffect } from 'react';
import './search.css';
import { orderBy } from 'lodash';
import useFetchFilterData from './hooks/usePlayFilter';
import { FIELD_TEMPLATE } from './filter-template';
import { TextField, Checkbox, Autocomplete } from '@mui/material';
import { BiCheckbox, BiCheckboxChecked } from 'react-icons/bi';
import { GiSettingsKnobs } from 'react-icons/gi';
import * as _ from 'lodash';
const icon = <BiCheckbox size={30} />;
const checkedIcon = <BiCheckboxChecked size={30} />;
const FilterPlays = ({ onChange, query }) => {
const [loading, error, data] = useFetchFilterData();
const [loadedData, setLoadedData] = useState({});
const [formData, setFormData] = useState({});
const [showModal, setShowModal] = useState(false);
useEffect(() => {
if (!loading && !error) {
data.languages = [
{
name: 'JavaScript',
value: 'js',
icon: 'https://res.cloudinary.com/dgtdljyul/image/upload/v1675411496/js_jjnhvy.png'
},
{
name: 'TypeScript',
value: 'ts',
icon: 'https://res.cloudinary.com/dgtdljyul/image/upload/v1675409456/ts_yrzjge.png'
}
];
setLoadedData(data);
}
}, [query, loading, showModal]);
// This useEffect ensures that once the filter plays modal is closed, the focus is returned back to the filter plays button
useEffect(() => {
const filterPlaysBtn = document.getElementById('filterPlaysBtn');
if (!showModal && filterPlaysBtn) {
filterPlaysBtn.focus();
}
}, [showModal]);
const filterModalCloseBtnHandler = () => {
setShowModal(false);
};
const loadFilter = () => {
const newFormData = {};
FIELD_TEMPLATE.forEach((template) => {
if (template.datafield === 'text') {
const text = query && query.text ? query.text.split('+').join(' ') : '';
newFormData['text'] = decodeURIComponent(text);
} else {
if (query[template.datafield]) {
newFormData[template.datafield] = [];
const splitData = _.isArray(query[template.datafield])
? query[template.datafield]
: query[template.datafield].split(',');
splitData.forEach((data) => {
const found =
loadedData[template.datafield] &&
loadedData[template.datafield].filter((d) => {
if (template.node) {
return d[template.node][template.fieldValue] === data;
} else {
return d[template.fieldValue] === data;
}
})[0];
if (found) {
newFormData[template.datafield].push(found);
}
});
}
}
});
setFormData(newFormData);
};
const showFilterModal = () => {
if (query) {
loadFilter();
} else {
setFormData({});
}
setShowModal(true);
};
const handleFilter = () => {
const res = {};
FIELD_TEMPLATE.forEach((template) => {
if (formData[template.datafield]) {
res[template.datafield] = [];
if (_.isArray(formData[template.datafield])) {
formData[template.datafield].forEach((data) => {
res[template.datafield].push(
template.node ? data[template.node][template.fieldValue] : data[template.fieldValue]
);
});
} else {
res[template.datafield] = formData[template.datafield];
}
}
});
const finalQueryObject = {};
Object.keys(res).forEach((key) => {
if (res[key] && res[key].length) {
finalQueryObject[key] = res[key];
}
});
setShowModal(false);
onChange({ ...finalQueryObject });
};
const handleChange = (key, value) => {
setFormData((pre) => ({ ...pre, [key]: value }));
};
if (loading) {
return <p />;
}
const getOptionNode = (field, option) => {
if (field.node) {
return option[field.node];
}
return option;
};
const renderFiled = (field) => {
switch (field.type) {
case 'select':
return (
<Autocomplete
disableCloseOnSelect
freeSolo={field.freeSolo}
getOptionLabel={(option) =>
field.node ? option[field.node][field.fieldName] : option[field.fieldName]
}
id={field.datafield}
multiple={field.multiple}
options={
field.sorted
? orderBy(loadedData[field.datafield], [field.fieldName], ['asc'])
: loadedData[field.datafield]
}
renderInput={(params) => (
<TextField {...params} placeholder={field.placeholder} size="small" />
)}
renderOption={(props, option, { selected }) => (
<li {...props}>
<Checkbox
checked={selected}
checkedIcon={checkedIcon}
icon={icon}
style={{ padding: 0 }}
/>
<div className="flex md-ml-8 min-w-0 ml-2 items-center">
<span className="rounded-full">
{field.avatar && getOptionNode(field, option)[field.avatar] ? (
<span className="flex items-center justify-center md-h-12 h-8 md-w-16 w-8 mr-4">
<img
alt="avatar"
className="md-h-12 w-8 md-w-12 h-8 rounded-full"
src={getOptionNode(field, option)[field.avatar]}
/>
</span>
) : null}
</span>
<span className="break-words min-w-0">
{field.node ? option[field.node][field.fieldName] : option[field.fieldName]}
</span>
</div>
</li>
)}
size="small"
value={
!formData[field.datafield] ? (field.multiple ? [] : '') : formData[field.datafield]
}
onChange={(e, newValue) => {
let updatedval = newValue;
if (field.multiple) {
updatedval = [];
newValue.forEach((v) => {
if (_.isObject(v)) {
updatedval.push(v);
} else {
updatedval.push({
[field.fieldName || 'name']: v,
[field.fieldValue || 'value']: ''
});
}
});
}
handleChange(field.datafield, updatedval);
}}
/>
);
case 'text':
return (
<TextField
className="w-full"
size="small"
value={formData[field.datafield]}
onChange={(e) => handleChange(field.datafield, e.target.value)}
/>
);
}
};
return (
<div className="search-filter">
<Modal
cname="filter"
show={showModal}
title="Filter Plays By"
onClose={filterModalCloseBtnHandler}
onSubmit={handleFilter}
>
{FIELD_TEMPLATE.map((field, field_i) => {
return (
<div className="flex-filter-modal flex-col md:flex-row p-2" key={field_i}>
<div className="w-32">
{field.display}
{field.required ? '*' : ''}
</div>
<div className="flex-1">{renderFiled(field)}</div>
</div>
);
})}
</Modal>
<button
className="btn-filter"
id="filterPlaysBtn"
title="Filter Plays"
onClick={() => {
showFilterModal();
}}
>
{query && Object.keys(query).length > 0 ? (
<div className="badge">{Object.keys(query).length}</div>
) : null}
<GiSettingsKnobs className="icon" color="var(--color-neutral-30)" size="28px" />
</button>
</div>
);
};
export default FilterPlays;