-
Notifications
You must be signed in to change notification settings - Fork 4
/
LaneCustomListsEditor.tsx
313 lines (292 loc) · 9.55 KB
/
LaneCustomListsEditor.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
import * as React from "react";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
import { CustomListData } from "../interfaces";
import AddIcon from "./icons/AddIcon";
import ShareIcon from "./icons/ShareIcon";
import TrashIcon from "./icons/TrashIcon";
import GrabIcon from "./icons/GrabIcon";
import EditableInput from "./EditableInput";
import { Button } from "library-simplified-reusable-components";
export interface LaneCustomListsEditorProps
extends React.Props<LaneCustomListsEditor> {
allCustomLists: CustomListData[];
customListIds: number[];
filter?: string;
filteredCustomLists?: CustomListData[];
changeFilter?: (value: string) => void;
onUpdate?: (customListIds: number[]) => void;
}
export interface LaneCustomListsEditorState {
draggingFrom: string | null;
}
/** Drag and drop interface for adding custom lists to a lane. */
export default class LaneCustomListsEditor extends React.Component<
LaneCustomListsEditorProps,
LaneCustomListsEditorState
> {
static defaultProps = {
filter: "owned",
filteredCustomLists: [],
};
constructor(props) {
super(props);
this.state = {
draggingFrom: null,
};
this.onDragStart = this.onDragStart.bind(this);
this.onDragEnd = this.onDragEnd.bind(this);
}
renderFilterSelect(): JSX.Element {
const filters = [
["All", ""],
["Owned", "owned"],
["Subscribed", "shared-in"],
];
return (
<fieldset>
<legend className="visuallyHidden">Select filter type</legend>
<EditableInput
elementType="select"
name="filter"
label="Show "
value={this.props.filter}
onChange={this.props.changeFilter}
>
{filters.map(([label, value]) => (
<option
key={value}
value={value}
aria-selected={this.props.filter === value}
>
{label}
</option>
))}
</EditableInput>
</fieldset>
);
}
renderListInfo(list): JSX.Element {
return (
<>
<GrabIcon />
<div className="list-info">
<div className="list-name">
{!list.is_owner && (
<ShareIcon title="This list is shared by another library." />
)}
{list.name}
</div>
<div className="list-count">Items in list: {list.entry_count}</div>
<div className="list-id">ID-{list.id}</div>
</div>
</>
);
}
render(): JSX.Element {
return (
<DragDropContext
onDragStart={this.onDragStart}
onDragEnd={this.onDragEnd}
>
<div className="lane-custom-lists-drag-and-drop">
<div className="available-lists">
<div className="droppable-header">
<h4>Available Lists ({this.listsNotInLane().length})</h4>
{this.renderFilterSelect()}
</div>
<Droppable
droppableId="available-lists"
isDropDisabled={this.state.draggingFrom !== "current-lists"}
>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
className={
snapshot.isDraggingOver
? "droppable dragging-over"
: "droppable"
}
>
{this.state.draggingFrom === "current-lists" && (
<p>Drag lists here to remove them from the lane.</p>
)}
{this.state.draggingFrom !== "current-lists" &&
this.listsNotInLane().map((list) => (
<Draggable key={list.id} draggableId={list.id}>
{(provided, snapshot) => (
<div>
<div
className={
"available-list" +
(snapshot.isDragging ? " dragging" : "")
}
ref={provided.innerRef}
style={provided.draggableStyle}
{...provided.dragHandleProps}
>
{this.renderListInfo(list)}
<div className="links">
<Button
className="inverted"
callback={() => {
this.add(list.id);
}}
content={
<span>
Add to lane <AddIcon />
</span>
}
/>
</div>
</div>
{provided.placeholder}
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</div>
<div className="current-lists">
<div className="droppable-header">
<h4>Lists in this Lane ({this.listsInLane().length})</h4>
</div>
<Droppable
droppableId="current-lists"
isDropDisabled={this.state.draggingFrom !== "available-lists"}
>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
className={
snapshot.isDraggingOver
? " droppable dragging-over"
: "droppable"
}
>
<p>Drag lists here to add them to the lane.</p>
{this.listsInLane().map((list) => (
<Draggable key={list.id} draggableId={list.id}>
{(provided, snapshot) => (
<div>
<div
className={
"current-list" +
(snapshot.isDragging ? " dragging" : "")
}
ref={provided.innerRef}
style={provided.draggableStyle}
{...provided.dragHandleProps}
>
{this.renderListInfo(list)}
<div className="links">
<Button
className="danger"
callback={() => {
this.remove(list.id);
}}
content={
<span>
Remove from lane <TrashIcon />
</span>
}
/>
</div>
</div>
{provided.placeholder}
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</div>
</div>
</DragDropContext>
);
}
listsNotInLane(): CustomListData[] {
const lists = [];
for (const list of this.props.filteredCustomLists || []) {
let found = false;
for (const listId of this.getCustomListIds()) {
if (list.id === listId) {
found = true;
break;
}
}
if (!found) {
lists.push(list);
}
}
return lists;
}
listsInLane(): CustomListData[] {
const lists = [];
for (const list of this.props.allCustomLists || []) {
for (const listId of this.getCustomListIds()) {
if (list.id === listId) {
lists.push(list);
}
}
}
return lists;
}
add(listId) {
const customListIds = this.getCustomListIds();
customListIds.push(parseInt(String(listId), 10));
this.setState({ draggingFrom: null });
if (this.props.onUpdate) {
this.props.onUpdate(customListIds);
}
}
remove(listId) {
let customListIds = this.getCustomListIds();
customListIds = customListIds.filter((id) => id !== listId);
this.setState({ draggingFrom: null });
if (this.props.onUpdate) {
this.props.onUpdate(customListIds);
}
}
reset(ids: number[]) {
this.setState({
draggingFrom: null,
});
if (this.props.onUpdate) {
this.props.onUpdate(ids);
}
}
getCustomListIds(): number[] {
return this.props.customListIds || [];
}
onDragStart(initial) {
document.body.classList.add("dragging");
const source = initial.source;
this.setState({ draggingFrom: source.droppableId });
}
onDragEnd(result) {
const draggableId = result.draggableId;
const source = result.source;
const destination = result.destination;
if (
source.droppableId === "available-lists" &&
destination &&
destination.droppableId === "current-lists"
) {
this.add(draggableId);
} else if (
source.droppableId === "current-lists" &&
destination &&
destination.droppableId === "available-lists"
) {
this.remove(draggableId);
} else {
this.setState({ draggingFrom: null });
}
document.body.classList.remove("dragging");
}
}