forked from shakacode/rescript-dnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaferIdentifiersAndMultipleContainersGuide.res
201 lines (180 loc) · 4.83 KB
/
SaferIdentifiersAndMultipleContainersGuide.res
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
module MakeId = () => {
module Id: {
type t
} = {
type t = int
}
type t = Id.t
external make: int => t = "%identity"
external toInt: t => int = "%identity"
let toString = x => x->toInt->Int.toString
let eq = (x1, x2) => x1->toInt == x2->toInt
let cmp = (x1, x2) => Pervasives.compare(x1->toInt, x2->toInt)
module Comparable = Belt.Id.MakeComparable({
type t = Id.t
let cmp = cmp
})
module Map = {
type t<'t> = Map.t<Id.t, 't, Comparable.identity>
let make = () => Map.make(~id=module(Comparable))
}
}
module TodoId = MakeId()
module TodoListId = MakeId()
module DraggableItem = {
type t = TodoId.t
let eq = TodoId.eq
let cmp = TodoId.cmp
}
module DroppableContainer = {
type t = TodoListId.t
let eq = TodoListId.eq
let cmp = TodoListId.cmp
}
module Todos = Dnd.Make(DraggableItem, DroppableContainer)
type todo = {
id: TodoId.t,
title: string,
todoListId: TodoListId.t,
}
type todoList = {
id: TodoListId.t,
title: string,
todos: array<TodoId.t>,
}
type state = {
todoListIndex: array<TodoListId.t>,
todoListMap: TodoListId.Map.t<todoList>,
todoMap: TodoId.Map.t<todo>,
}
type action = ReorderTodos(Dnd.result<DraggableItem.t, DroppableContainer.t>)
let reducer = (state, action) =>
switch action {
| ReorderTodos(Some(SameContainer(todoId, placement))) =>
let todo = state.todoMap->Map.getExn(todoId)
{
...state,
todoListMap: // Updating todos index of todo list
state.todoListMap->Map.update(todo.todoListId, todoList =>
todoList->Option.map(todoList => {
...todoList,
todos: todoList.todos->ArrayExt.reinsert(
~value=todoId,
~place=switch placement {
| Before(id) => #Before(id)
| Last => #Last
},
),
})
),
}
| ReorderTodos(Some(NewContainer(todoId, targetTodoListId, placement))) =>
let todo = state.todoMap->Map.getExn(todoId)
{
...state,
todoMap: // Updating todoListId of dropped todo
state.todoMap->Map.update(todoId, todo =>
todo->Option.map(todo => {...todo, todoListId: targetTodoListId})
),
todoListMap: state.todoListMap
// Removing todoId from old todo list index
// since todo was dropped onto another container
->Map.update(todo.todoListId, todoList =>
todoList->Option.map(todoList => {
...todoList,
todos: todoList.todos->Array.keep(todoId' =>
todoId'->TodoId.toInt != todoId->TodoId.toInt
),
})
)
// Inserting todoId into the todos index of the target todo list
->Map.update(targetTodoListId, todoList =>
todoList->Option.map(todoList => {
...todoList,
todos: todoList.todos->ArrayExt.insert(
~value=todoId,
~place=switch placement {
| Before(id) => #Before(id)
| Last => #Last
},
),
})
),
}
| ReorderTodos(None) => state
}
let initialState = {
todoListIndex: [TodoListId.make(1), TodoListId.make(2)],
todoListMap: TodoListId.Map.make()
->Map.set(
TodoListId.make(1),
{
id: TodoListId.make(1),
title: "Todo list #1",
todos: [TodoId.make(1), TodoId.make(2)],
},
)
->Map.set(
TodoListId.make(2),
{
id: TodoListId.make(2),
title: "Todo list #2",
todos: [TodoId.make(3), TodoId.make(4)],
},
),
todoMap: TodoId.Map.make()
->Map.set(
TodoId.make(1),
{
id: TodoId.make(1),
title: "Todo #1",
todoListId: TodoListId.make(1),
},
)
->Map.set(
TodoId.make(2),
{
id: TodoId.make(2),
title: "Todo #2",
todoListId: TodoListId.make(1),
},
)
->Map.set(
TodoId.make(3),
{
id: TodoId.make(3),
title: "Todo #3",
todoListId: TodoListId.make(2),
},
)
->Map.set(
TodoId.make(4),
{
id: TodoId.make(4),
title: "Todo #4",
todoListId: TodoListId.make(2),
},
),
}
@react.component
let make = () => {
let (state, dispatch) = reducer->React.useReducer(initialState)
<Todos.DndManager onReorder={result => ReorderTodos(result)->dispatch}>
{state.todoListIndex
->Array.map(todoListId => {
let todoList = state.todoListMap->Map.getExn(todoListId)
<Todos.DroppableContainer id=todoListId axis=Y key={todoListId->TodoListId.toString}>
<h2> {todoList.title->React.string} </h2>
{todoList.todos
->Array.mapWithIndex((index, todoId) => {
let todo = state.todoMap->Map.getExn(todoId)
<Todos.DraggableItem id=todoId key={todoId->TodoId.toString} containerId=todoListId index>
#Children(todo.title->React.string)
</Todos.DraggableItem>
})
->React.array}
</Todos.DroppableContainer>
})
->React.array}
</Todos.DndManager>
}