forked from WildAid/o-fish-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-panel.component.js
218 lines (201 loc) · 6.47 KB
/
filter-panel.component.js
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
import React, { Component } from "react";
import { withRouter } from "react-router";
import moment from "moment";
import SearchIcon from "@material-ui/icons/Search";
import { withTranslation } from "react-i18next";
import history from "../../../root/root.history";
import FilterPart from "./filter-part.component";
import FilterLine from "./filter-line.component";
import "./filter-panel.css";
class FilterPanel extends Component {
state = { isFilterPanelShown: false, filterParts: [], searchQuery: "" };
convertFilter(filterObj) {
const filter = [];
for (var key in filterObj) {
filter.push({ name: key, value: filterObj[key] });
}
return filter;
}
componentDidMount() {
let filter = [];
if (this.props.match.params.filter) {
filter = this.convertFilter(JSON.parse(this.props.match.params.filter));
}
const { configuration } = this.props;
if (configuration && filter && filter.length) {
const flatConfig = {};
for (const section in configuration) {
configuration[section].forEach((item, i) => {
flatConfig[item.field ? item.field : item.name] = item;
});
}
const defaultFilter = [];
filter.forEach((filterPart) => {
const config = flatConfig[filterPart.name];
if (config) {
defaultFilter.push({
...config,
value: filterPart.value,
});
}
});
this.setState({ filterParts: defaultFilter });
}
}
constructFilter(filterParts) {
let filterObject = {};
const { options } = this.props;
filterParts.forEach((item) => {
switch (item.type) {
case "value":
case "risk":
filterObject[item.field ? item.field : item.name] = item.value;
break;
case "date":
if (options && options.useChartsSyntax) {
filterObject[item.field ? item.field : item.name] = {
$gt: new Date(item.value),
};
} else {
filterObject[item.field ? item.field : item.name] = moment(
item.value
).format("YYYY-MM-DD");
}
//{"$date": item.value};
break;
//TODO: Use Other field types
default:
filterObject[item.field ? item.field : item.name] = item.value;
//TODO: Use right regex method
// {"$regex": item.value, "$options": "ig"};
}
});
return filterObject;
}
setSearch = (event) => {
this.setState({ searchQuery: event.target.value });
};
showFilter = () => {
const { isFilterPanelShown } = this.state;
this.setState({
isFilterPanelShown: !isFilterPanelShown,
});
};
hideFilter = () => {
this.setState({
isFilterPanelShown: false,
});
};
handleFilterChanged = (name, value) => {
const { filterParts } = this.state;
const part = filterParts.find((item) => item.name === name);
if (part) {
part.value = value;
this.setState({
filterParts: filterParts,
});
this.applyFilterChanges();
}
};
applyFilterChanges() {
const { filterParts } = this.state;
const path = this.props.match.path;
const filter = JSON.stringify(this.constructFilter(filterParts));
history.push(path.replace(":filter", filter));
if (this.props.onFilterChanged) {
this.props.onFilterChanged(this.constructFilter(filterParts));
}
}
checkFilterPart = (part) => {
const { filterParts } = this.state;
const index = filterParts.findIndex((item) => item.name === part.name);
if (index >= 0) {
this.removeFilterPart(index);
} else {
filterParts.push(part);
this.setState({
filterParts: filterParts,
isFilterPanelShown: false,
});
if (part.type === "value" || part.type === "risk") {
this.applyFilterChanges();
}
}
};
removeFilterPart = (index) => {
const { filterParts } = this.state;
filterParts.splice(index, 1);
this.setState({
filterParts: filterParts,
isFilterPanelShown: false,
});
this.applyFilterChanges();
};
render() {
const { isFilterPanelShown, filterParts } = this.state;
const { options, configuration, t } = this.props;
const filterPartNames = filterParts.map((item) => item.name);
return (
<div className="flex-row align-center">
<div className="flex-row">
{filterParts.map((item, ind) => (
<FilterPart
key={"filterPart" + ind}
partType={item.type}
partName={item.name}
value={item.value}
title={item.partTitle ? item.partTitle : item.title}
onRemove={() => {
this.removeFilterPart(ind);
}}
onFilterChange={this.handleFilterChanged}
/>
))}
</div>
<div className="relative">
<div className="filter-btn blue-btn icon-radius d-flex flex-row align-end" onClick={this.showFilter}>
<span class="material-icons icon-font">filter_alt</span> { filterParts.length ? `(${filterParts.length})` : "" }
</div>
<div
className={
"flex-column justify-start align-stretch absolute white-bg margin-bottom box-shadow filter-panel" +
(isFilterPanelShown ? "" : " invisible")
}
>
{options && options.searchByFilter && (
<div className="search">
<div className="search-icon">
<SearchIcon />
</div>
<input
className="search-field"
type="search"
placeholder="Search"
value={this.state.searchQuery}
onChange={this.setSearch}
></input>
</div>
)}
{Object.keys(configuration).map((key) => (
<section key={key}>
<h3>{key}</h3>
{configuration[key].map((filterPart, index) => (
<FilterLine
key={index}
parts={filterPartNames}
partConfig={{
name: ("" + Math.random()).replace("0.", ""),
...filterPart,
}}
onCheck={this.checkFilterPart}
/>
))}
</section>
))}
</div>
</div>
</div>
);
}
}
export default withRouter(withTranslation("translation")(FilterPanel));