-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathTableFilterProfiles.tsx
More file actions
269 lines (247 loc) · 8.16 KB
/
TableFilterProfiles.tsx
File metadata and controls
269 lines (247 loc) · 8.16 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
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import cn from "classnames";
import { getFilterProfiles } from "../../selectors/tableFilterProfilesSelectors";
import {
FilterProfile,
createFilterProfile,
removeFilterProfile,
} from "../../slices/tableFilterProfilesSlice";
import {
goToPage,
} from "../../thunks/tableThunks";
import { getFilters } from "../../selectors/tableFilterSelectors";
import { FilterData, loadFilterProfile } from "../../slices/tableFilterSlice";
import { useAppDispatch, useAppSelector } from "../../store";
import { useHotkeys } from "react-hotkeys-hook";
import { availableHotkeys } from "../../configs/hotkeysConfig";
import ButtonLikeAnchor from "./ButtonLikeAnchor";
import { ParseKeys } from "i18next";
import { Resource } from "../../slices/tableSlice";
import { LuSettings, LuX } from "react-icons/lu";
/**
* This component renders the table filter profiles in the upper right corner when clicked on settings icon of the
* table filters.
*/
const TableFiltersProfiles = ({
showFilterSettings,
setFilterSettings,
loadResource,
resource,
}: {
showFilterSettings: boolean,
setFilterSettings: (_: boolean) => void,
loadResource: () => Promise<void>,
resource: Resource,
}) => {
const dispatch = useAppDispatch();
const profiles = useAppSelector(state => getFilterProfiles(state));
const filterMap = useAppSelector(state => getFilters(state, resource));
// State for switching between list of profiles and saving/editing dialog
const [settingsMode, setSettingsMode] = useState(true);
// State for helping saving and editing profiles
const [profileName, setProfileName] = useState("");
const [profileDescription, setProfileDescription] = useState("");
const [currentlyEditing, setCurrentlyEditing] = useState<FilterProfile | null>(null);
const [validName, setValidName] = useState(false);
const { t } = useTranslation();
useHotkeys(
availableHotkeys.general.CLOSE_MODAL.sequence,
() => setFilterSettings(false),
{ description: t(availableHotkeys.general.CLOSE_MODAL.description) ?? undefined },
[setFilterSettings],
);
const currentProfiles = profiles.filter(
profile => profile.resource === resource,
);
// todo: Maybe saving to storage is needed
const saveProfile = () => {
if (validName) {
const filterProfile = {
name: profileName,
description: profileDescription,
filterMap: filterMap,
resource: resource,
};
dispatch(createFilterProfile(filterProfile));
}
setSettingsMode(!settingsMode);
resetStateValues();
};
const editFilterProfile = (profile: FilterProfile) => {
setSettingsMode(false);
setCurrentlyEditing(profile);
setProfileName(profile.name);
setProfileDescription(profile.description);
dispatch(removeFilterProfile(profile));
setValidName(true);
};
const cancelEditProfile = () => {
// This holds the value of the profile being edited (in edit mode), and by cancelling the process, the profile won't vanish!
if (currentlyEditing) {
dispatch(createFilterProfile(currentlyEditing));
}
setSettingsMode(true);
resetStateValues();
};
const closeFilterSetting = () => {
if (currentlyEditing) {
cancelEditProfile();
}
setFilterSettings(!showFilterSettings);
};
const resetStateValues = () => {
setProfileName("");
setProfileDescription("");
setCurrentlyEditing(null);
setValidName(false);
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const itemName = e.target.name;
const itemValue = e.target.value;
if (itemName === "name") {
const isDuplicated = profiles.some(
profile => profile.name === itemValue,
);
if (!isDuplicated) {
setValidName(true);
} else {
setValidName(false);
}
setProfileName(itemValue);
}
if (itemName === "description") {
setProfileDescription(itemValue);
}
};
const chooseFilterProfile = async (filterMap: FilterData[]) => {
dispatch(loadFilterProfile(filterMap));
// No matter what, we go to page one.
dispatch(goToPage(0));
// Reload resources when filters are removed
await loadResource();
};
return (
<>
{/* Show filter profiles dialog if settings icon in TableFilters is clicked*/}
{showFilterSettings && (
<div className="btn-dd filter-settings-dd df-profile-filters">
{/* depending on settingsMode show list of all saved profiles or the chosen profile to edit*/}
{settingsMode ? (
// if settingsMode is true the list with all saved profiles is shown
<div className="filters-list">
<header>
<ButtonLikeAnchor
className="icon close"
onClick={closeFilterSetting}
>
<LuX className="icon close"/>
</ButtonLikeAnchor>
<h4>{t("TABLE_FILTERS.PROFILES.FILTERS_HEADER")}</h4>
</header>
<ul>
{currentProfiles.length === 0 ? (
// if no profiles saved yet
<li>{t("TABLE_FILTERS.PROFILES.EMPTY")}</li>
) : (
// repeat for each profile in profiles filtered for currently shown resource (else-case)
currentProfiles.map((profile, key) => (
<li key={key}>
<ButtonLikeAnchor
onClick={() => chooseFilterProfile(profile.filterMap)}
tooltipText={profile.description as ParseKeys}
>
{profile.name.substr(0, 70)}
</ButtonLikeAnchor>
{/* Settings icon to edit profile */}
<ButtonLikeAnchor
onClick={() => editFilterProfile(profile)}
tooltipText="TABLE_FILTERS.PROFILES.EDIT"
>
<LuSettings className="icon edit"/>
</ButtonLikeAnchor>
{/* Remove icon to remove profile */}
<ButtonLikeAnchor
onClick={() => dispatch(removeFilterProfile(profile))}
tooltipText="TABLE_FILTERS.PROFILES.REMOVE"
>
<LuX className="icon remove"/>
</ButtonLikeAnchor>
</li>
))
)}
</ul>
{/* Save the currently selected filter options as new profile */}
{/* settingsMode is switched and save dialog is opened*/}
<div className="input-container">
<div className="btn-container">
<ButtonLikeAnchor
className="save"
onClick={() => setSettingsMode(!settingsMode)}
>
{t("TABLE_FILTERS.PROFILES.ADD").substr(0, 70)}
</ButtonLikeAnchor>
</div>
</div>
</div>
) : (
// if settingsMode is false then show editing dialog of selected filter profile
<div className="filter-details">
<header>
<ButtonLikeAnchor
onClick={closeFilterSetting}
>
<LuX className="icon close"/>
</ButtonLikeAnchor>
<h4>{t("TABLE_FILTERS.PROFILES.FILTER_HEADER")}</h4>
</header>
{/* Input form for save/editing profile*/}
<div className="edit-details">
<label>
{t("TABLE_FILTERS.PROFILES.NAME")}{" "}
<i className="required">*</i>
</label>
{/* Input for name of the filter profile*/}
<input
required
name="name"
type="text"
value={profileName}
onChange={e => handleChange(e)}
placeholder={t("TABLE_FILTERS.PROFILES.NAME_PLACEHOLDER")}
autoFocus={true}
/>
<label>{t("TABLE_FILTERS.PROFILES.DESCRIPTION")}</label>
{/* Input for a description of the filter profile*/}
<textarea
value={profileDescription}
name="description"
onChange={e => handleChange(e)}
placeholder={t(
"TABLE_FILTERS.PROFILES.DESCRIPTION_PLACEHOLDER",
)}
/>
</div>
<div className="input-container">
{/* Buttons for saving and canceling editing */}
<div className="btn-container">
<ButtonLikeAnchor onClick={cancelEditProfile} className="cancel">
{t("CANCEL")}
</ButtonLikeAnchor>
<ButtonLikeAnchor
onClick={saveProfile}
className={cn("save", { disabled: !validName })}
aria-disabled={!validName}
>
{t("SAVE")}
</ButtonLikeAnchor>
</div>
</div>
</div>
)}
</div>
)}
</>
);
};
export default TableFiltersProfiles;