-
Notifications
You must be signed in to change notification settings - Fork 56
/
FilterAside.tsx
149 lines (134 loc) · 4.3 KB
/
FilterAside.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
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
import React from "react";
import ReactGA from "react-ga";
import { Sidebar } from "primereact/sidebar";
import Form from "react-bootstrap/Form";
import { Card } from "primereact/card";
import {
PanelSettings,
FilterFieldSet,
CategoryField,
ActivePreset,
SHOW_CUSTOM,
} from "./Cms";
import FilterService from "./FilterService";
import FilterPropertyTable from "./FilterPropertyTable";
type PropsType = {
filterFields: { current: FilterFieldSet; untouched: FilterFieldSet };
updateFilterFields: (
updatedFilterFields: FilterFieldSet,
preset: ActivePreset
) => void;
showAside: boolean;
toggleAside: () => void;
cookiesAccepted: boolean;
};
export const FilterAside = (props: PropsType): JSX.Element => {
const { filterFields, updateFilterFields, showAside, toggleAside } = props;
const [panelSettings, setPanelSettings] = React.useState<PanelSettings>({
showModifiedOnly: false,
fieldFilterString: "",
});
const handlePanelSettingsChange = (
event: React.ChangeEvent<HTMLInputElement>
) => {
if (event.target.name === "showModifiedOnly") {
setPanelSettings({
showModifiedOnly: event.target.checked,
fieldFilterString: panelSettings.fieldFilterString,
});
} else {
setPanelSettings({
showModifiedOnly: panelSettings.showModifiedOnly,
fieldFilterString: event.target.value,
});
}
};
const handleSpecialFieldChange = (
event: React.ChangeEvent<HTMLInputElement>
) => {
const updatedFilterFields = Object.assign({}, filterFields.current);
const fieldKey = event.target.name;
const valueArray = updatedFilterFields.special[fieldKey].values;
const value = event.target.value;
if (event.target.checked) {
if (!valueArray.includes(value)) {
valueArray.push(value);
}
} else {
const valueIndex = valueArray.indexOf(value);
if (valueIndex !== -1) {
valueArray.splice(valueIndex, 1);
}
}
updatedFilterFields.special[fieldKey].values = valueArray;
updateFilterFields(updatedFilterFields, SHOW_CUSTOM);
};
const handleBasicFieldChange = (
event: React.ChangeEvent<HTMLInputElement>,
fieldKey: string,
categoryKey?: string
) => {
// Clone object, otherwise react won't re-render
const updatedFilterFields = Object.assign({}, filterFields.current);
if (categoryKey) {
(updatedFilterFields.basic[categoryKey] as CategoryField)[
fieldKey
].value = event.target.value;
} else {
updatedFilterFields.basic[fieldKey].value = event.target.value;
}
if (props.cookiesAccepted) {
ReactGA.event({
category: "Filter",
action: `${categoryKey || ""}/${fieldKey}: ${event.target.value}`,
});
}
updateFilterFields(updatedFilterFields, SHOW_CUSTOM);
};
const filteredFilterFields = FilterService.getFilteredFilterFields(
panelSettings,
filterFields
);
return (
<Sidebar visible={showAside} onHide={() => toggleAside()}>
<Form>
{showAside ? (
<Card title="Filter for properties" className="mt-5">
<div className="p-3">
<Form.Control
type="text"
name="fieldFilterString"
value={panelSettings.fieldFilterString}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
handlePanelSettingsChange(e)
}
/>
</div>
<div className="p-3 score-checkboxes">
{" "}
<input
type="checkbox"
name="showModifiedOnly"
value={panelSettings.fieldFilterString}
checked={panelSettings.showModifiedOnly}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
handlePanelSettingsChange(e)
}
id="showModifiedOnly"
/>
<label htmlFor="showModifiedOnly">
Show modified properties only
</label>
</div>
</Card>
) : null}
</Form>
<FilterPropertyTable
filterFields={filteredFilterFields}
specialFieldChangeHandler={handleSpecialFieldChange}
basicFieldChangeHandler={handleBasicFieldChange}
/>
</Sidebar>
);
};
export default FilterAside;