-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtodos.tsx
More file actions
260 lines (244 loc) · 7.33 KB
/
todos.tsx
File metadata and controls
260 lines (244 loc) · 7.33 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
import { createMemo, createSignal, For, Show } from "solid-js";
import { useServerTodos } from "~/lib/todos";
import {
createClientEventLog,
createEventComputed,
createEventProjection,
} from "../../socket/events";
import { createSocketMemo } from "../../socket/lib/shared";
import { CompleteIcon, IncompleteIcon } from "./icons";
export type TodosFilter = "all" | "active" | "completed" | undefined;
export type Todo = {
id: number;
title: string;
completed: boolean;
};
export function TodoApp(props: { filter: TodosFilter; listId?: string }) {
const filter = () => props.filter;
const [editingTodoId, setEditingId] = createSignal();
const setEditing = ({
id,
pending,
}: {
id?: number;
pending?: () => boolean;
}) => {
if (!pending || !pending()) setEditingId(id);
};
let inputRef!: HTMLInputElement;
const serverTodos = useServerTodos(createSocketMemo(() => props.listId));
const { events, appendEvent } = createClientEventLog(serverTodos);
const todos = createEventProjection(
events,
(acc, e) => {
if (e.type === "todo-added") {
acc.push({ id: e.id, title: e.title, completed: false });
}
if (e.type === "todo-toggled") {
const todo = acc.find((t) => t.id === e.id);
if (todo) todo.completed = !todo.completed;
}
if (e.type === "todo-deleted") {
const index = acc.findIndex((note) => note.id === e.id);
if (index !== -1) acc.splice(index, 1);
}
if (e.type === "todo-edited") {
const todo = acc.find((t) => t.id === e.id);
if (todo) todo.title = e.title;
}
return acc;
},
[] as Todo[]
);
const filteredTodos = createMemo(() => {
if (filter() === "active") return todos.filter((t) => !t.completed);
if (filter() === "completed") return todos.filter((t) => t.completed);
return todos;
});
const remainingTodos = createEventProjection(
events,
(acc, e) => {
if (e.type === "todo-added") {
acc.push(e.id);
}
if (e.type === "todo-toggled") {
acc.includes(e.id) ? acc.splice(acc.indexOf(e.id), 1) : acc.push(e.id);
}
if (e.type === "todo-deleted") {
acc.includes(e.id) && acc.splice(acc.indexOf(e.id), 1);
}
return acc;
},
[] as number[]
);
const totalCount = createEventComputed(
events,
(acc, e) => {
if (e.type === "todo-added") {
acc++;
}
if (e.type === "todo-deleted") {
acc--;
}
return acc;
},
0
);
const toggleAll = (completed: boolean) =>
Promise.all(
todos
.filter((t) => t.completed !== completed)
.map((t) => appendEvent({ type: "todo-toggled", id: t.id }))
);
const clearCompleted = () =>
Promise.all(
todos
.filter((t) => t.completed)
.map((t) => appendEvent({ type: "todo-deleted", id: t.id }))
);
return (
<>
<header class="header">
<h1>todos</h1>
<form
onSubmit={async (e) => {
e.preventDefault();
if (!inputRef.value.trim()) e.preventDefault();
setTimeout(() => (inputRef.value = ""));
const title = (
new FormData(e.currentTarget).get("title") as string
).trim();
const id = todos.length + 1;
if (title.length)
await appendEvent({ type: "todo-added", title, id });
}}
>
<input
name="title"
class="new-todo"
placeholder="What needs to be done?"
ref={inputRef}
autofocus
/>
</form>
</header>
<section class="main">
<Show when={todos.length > 0}>
<button
class={`toggle-all ${remainingTodos.length ? "checked" : ""}`}
onClick={() => toggleAll(!!remainingTodos.length)}
>
❯
</button>
</Show>
<ul class="todo-list">
<For each={filteredTodos()}>
{(todo) => {
return (
<li
class="todo"
classList={{
editing: editingTodoId() === todo.id,
completed: todo.completed,
}}
>
<div>
<button
class="toggle"
onClick={() =>
appendEvent({
type: "todo-toggled",
id: todo.id,
})
}
>
{todo.completed ? <CompleteIcon /> : <IncompleteIcon />}
</button>
<label onDblClick={() => setEditing({ id: todo.id })}>
{todo.title}
</label>
<button
class="destroy"
onClick={() =>
appendEvent({
type: "todo-deleted",
id: todo.id,
})
}
/>
</div>
<Show when={editingTodoId() === todo.id}>
<form
onSubmit={(e) => {
e.preventDefault();
const title = new FormData(e.currentTarget).get(
"title"
) as string;
appendEvent({
type: "todo-edited",
id: todo.id,
title,
});
setEditing({});
}}
>
<input
name="title"
class="edit"
value={todo.title}
onBlur={(e) => {
if (todo.title !== e.currentTarget.value) {
e.currentTarget.form!.requestSubmit();
} else setEditing({});
}}
/>
</form>
</Show>
</li>
);
}}
</For>
</ul>
</section>
<footer class="footer">
<span class="todo-count">
<strong>{remainingTodos.length}</strong>{" "}
{remainingTodos.length === 1 ? " item " : " items "} left
</span>
<ul class="filters">
<li>
<a
href="?show=all"
classList={{
selected: !filter() || filter() === "all",
}}
>
All
</a>
</li>
<li>
<a
href="?show=active"
classList={{ selected: filter() === "active" }}
>
Active
</a>
</li>
<li>
<a
href="?show=completed"
classList={{ selected: filter() === "completed" }}
>
Completed
</a>
</li>
</ul>
<Show when={remainingTodos.length !== totalCount()}>
<button class="clear-completed" onClick={() => clearCompleted()}>
Clear completed
</button>
</Show>
</footer>
</>
);
}