-
Notifications
You must be signed in to change notification settings - Fork 4
/
LaneEditor.tsx
342 lines (313 loc) · 10.4 KB
/
LaneEditor.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
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
341
342
import * as React from "react";
import { LaneData, CustomListData } from "../interfaces";
import { Button } from "library-simplified-reusable-components";
import TextWithEditMode from "./TextWithEditMode";
import EditableInput from "./EditableInput";
import LaneCustomListsEditor from "./LaneCustomListsEditor";
import TrashIcon from "./icons/TrashIcon";
import XCloseIcon from "./icons/XCloseIcon";
import VisibleIcon from "./icons/VisibleIcon";
import HiddenIcon from "./icons/HiddenIcon";
export interface LaneEditorProps extends React.Props<LaneEditor> {
editOrCreate?: string;
library: string;
lane?: LaneData;
customLists: CustomListData[];
responseBody?: string;
editLane: (data: FormData) => Promise<void>;
deleteLane?: (lane: LaneData) => Promise<void>;
toggleLaneVisibility: (
lane: LaneData,
shouldBeVisible: boolean
) => Promise<void>;
findParentOfLane: (lane: LaneData) => LaneData | null;
}
export interface LaneEditorState {
filter: string;
name: string;
customListIds: number[];
inheritParentRestrictions: boolean;
}
/** Right panel of the lanes page for editing a single lane. */
export default class LaneEditor extends React.Component<
LaneEditorProps,
LaneEditorState
> {
private laneNameRef = React.createRef<TextWithEditMode>();
private laneCustomListsRef = React.createRef<LaneCustomListsEditor>();
constructor(props) {
super(props);
this.state = {
filter: "owned",
name: this.props.lane && this.props.lane.display_name,
customListIds: (this.props.lane && this.props.lane.custom_list_ids) || [],
inheritParentRestrictions:
this.props.lane && this.props.lane.inherit_parent_restrictions,
};
this.changeName = this.changeName.bind(this);
this.changeCustomLists = this.changeCustomLists.bind(this);
this.changeFilter = this.changeFilter.bind(this);
this.changeInheritParentRestrictions = this.changeInheritParentRestrictions.bind(
this
);
this.delete = this.delete.bind(this);
this.save = this.save.bind(this);
this.reset = this.reset.bind(this);
this.visibilityToggle = this.visibilityToggle.bind(this);
this.renderInfo = this.renderInfo.bind(this);
}
filterLists(lists?: CustomListData[]) {
lists = lists || this.props.customLists || [];
const { filter } = this.state;
let selectedFilter = null;
if (filter === "owned") {
selectedFilter = (list) => list.is_owner;
} else if (filter === "shared-in") {
selectedFilter = (list) => !list.is_owner;
} else if (filter === "shared-out") {
selectedFilter = (list) => list.is_owner && list.is_shared;
}
return selectedFilter ? lists.filter(selectedFilter) : lists;
}
hasCustomLists() {
return !!this.props.lane?.custom_list_ids?.length;
}
render(): JSX.Element {
const { lane } = this.props;
const parent = this.props.findParentOfLane(lane);
return (
<div className="lane-editor">
<div className="lane-editor-header">
<div>
<div className="save-or-edit">
<TextWithEditMode
text={lane?.display_name}
placeholder="name"
onUpdate={this.changeName}
ref={this.laneNameRef}
aria-label="Enter a name for this lane"
/>
{lane && <h4>ID-{lane.id}</h4>}
</div>
<span className="lane-buttons">
{lane && this.hasCustomLists() && this.props.deleteLane && (
<Button
className="danger delete-lane"
callback={this.delete}
content={
<span>
Delete lane <TrashIcon />
</span>
}
/>
)}
{this.visibilityToggle()}
<Button
className="save-lane"
callback={this.save}
content="Save lane"
/>
{this.hasChanges() && (
<Button
className="cancel-changes inverted"
callback={this.reset}
content="Cancel changes"
/>
)}
</span>
</div>
<div className="lane-details">
{this.renderInfo(parent)}
{this.hasCustomLists() && parent && (
<EditableInput
type="checkbox"
name="inherit_parent_restrictions"
checked={this.state.inheritParentRestrictions}
label="Inherit restrictions from parent lane"
onChange={this.changeInheritParentRestrictions}
/>
)}
</div>
</div>
{this.renderCustomListsEditor()}
</div>
);
}
renderCustomListsEditor(): JSX.Element {
const { editOrCreate, lane } = this.props;
if (editOrCreate === "edit") {
if (!lane) {
// We're editing a lane, but the lane data hasn't loaded yet. Don't render anything until
// we know if this is a custom lane.
return null;
}
if (!this.hasCustomLists()) {
return (
<div>
This lane was automatically generated. Its contents cannot be
edited.
</div>
);
}
}
return (
<div className="lane-editor-body">
<LaneCustomListsEditor
allCustomLists={this.props.customLists}
customListIds={this.state.customListIds}
filter={this.state.filter}
filteredCustomLists={this.filterLists()}
changeFilter={this.changeFilter}
onUpdate={this.changeCustomLists}
ref={this.laneCustomListsRef}
/>
</div>
);
}
renderInfo(parent?: LaneData): JSX.Element {
const isVisible = (lane: LaneData) => lane.visible;
if (this.props.lane) {
let visibility;
if (isVisible(this.props.lane)) {
visibility = (
<div>
This lane is currently visible. <VisibleIcon />
</div>
);
} else if (parent && !isVisible(parent)) {
visibility = (
<div>
This lane's parent is currently hidden. <HiddenIcon />
</div>
);
} else {
visibility = (
<div>
This lane is currently hidden. <HiddenIcon />
</div>
);
}
return visibility;
} else {
const laneLevel = parent
? `sublane of ${parent.display_name}`
: "top-level lane";
return <div>New {laneLevel}</div>;
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({
name: nextProps.lane && nextProps.lane.display_name,
customListIds: (nextProps.lane && nextProps.lane.custom_list_ids) || [],
inheritParentRestrictions:
nextProps.lane && nextProps.lane.inherit_parent_restrictions,
});
}
hasChanges(): boolean {
const nameChanged =
this.props.lane && this.props.lane.display_name !== this.state.name;
let listsChanged = false;
if (
this.props.lane &&
this.props.lane.custom_list_ids.length !== this.state.customListIds.length
) {
listsChanged = true;
} else {
const propsListIds =
(this.props.lane && this.props.lane.custom_list_ids.sort()) || [];
const stateListIds = this.state.customListIds.sort();
for (let i = 0; i < propsListIds.length; i++) {
if (propsListIds[i] !== stateListIds[i]) {
listsChanged = true;
break;
}
}
}
let inheritParentRestrictionsChanged = false;
if (
this.props.lane &&
this.props.lane.inherit_parent_restrictions !==
this.state.inheritParentRestrictions
) {
inheritParentRestrictionsChanged = true;
}
return nameChanged || listsChanged || inheritParentRestrictionsChanged;
}
changeName(name: string) {
this.setState(Object.assign({}, this.state, { name }));
}
changeCustomLists(customListIds: number[]) {
this.setState(Object.assign({}, this.state, { customListIds }));
}
changeFilter(filter) {
this.setState({ filter });
}
changeInheritParentRestrictions() {
const inheritParentRestrictions = !this.state.inheritParentRestrictions;
this.setState(Object.assign({}, this.state, { inheritParentRestrictions }));
}
delete() {
if (this.props.deleteLane && this.props.lane) {
this.props.deleteLane(this.props.lane);
}
}
visibilityToggle() {
// The lane's visibility cannot be changed if it has a hidden parent. In all other cases--i.e.
// if 1) the lane does not have a parent or 2) the lane has a parent which is visible--we render
// the hide/show button.
const parent = this.props.findParentOfLane(this.props.lane);
const canToggle =
this.props.lane && (!parent || (parent && parent.visible));
if (canToggle) {
return (
<Button
className={this.props.lane.visible ? "hide-lane" : "show-lane"}
content={`${this.props.lane.visible ? "Hide" : "Show"} lane`}
callback={() =>
this.props.toggleLaneVisibility(
this.props.lane,
!this.props.lane.visible
)
}
/>
);
}
}
save() {
const parent = this.props.findParentOfLane(this.props.lane);
const data = new (window as any).FormData();
if (this.props.lane) {
data.append("id", this.props.lane.id);
}
if (parent) {
data.append("parent_id", parent.id);
}
const name = this.laneNameRef.current.getText();
data.append("display_name", name);
const listIds = this.laneCustomListsRef.current?.getCustomListIds();
if (listIds) {
data.append("custom_list_ids", JSON.stringify(listIds));
}
data.append(
"inherit_parent_restrictions",
this.state.inheritParentRestrictions
);
this.props.editLane(data).then(() => {
// If a new lane was created, go to its edit page.
if (!this.props.lane && this.props.responseBody) {
window.location.href =
"/admin/web/lanes/" +
this.props.library +
"/edit/" +
this.props.responseBody;
}
});
}
reset() {
this.laneNameRef.current.reset();
this.laneCustomListsRef.current?.reset(this.props.lane.custom_list_ids);
const inheritParentRestrictions =
this.props.lane && this.props.lane.inherit_parent_restrictions;
this.setState(Object.assign({}, this.state, { inheritParentRestrictions }));
}
}