-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep-1.tsx
68 lines (57 loc) · 1.73 KB
/
step-1.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
import React from "react";
import { AutomergeUrl } from "@automerge/automerge-repo";
import { useHandle, useDocument } from "./api";
type Todo = {
description: string;
isDone: boolean;
};
type TodoListDoc = {
todos: Todo[];
};
const TodoListView = ({ docUrl }: { docUrl: AutomergeUrl }) => {
const handle = useHandle<TodoListDoc>(docUrl);
const doc = useDocument(handle);
const onAddTodo = () => {
handle.change((doc) => {
doc.todos.push({ isDone: false, description: "" });
});
};
const onToggleTodoAt = (indexToToggle: number) => {
handle.change(
(doc) =>
(doc.todos[indexToToggle].isDone = !doc.todos[indexToToggle].isDone)
);
};
const onEditTodoAt = (indexToEdit: number, description: string) => {
handle.change((doc) => (doc.todos[indexToEdit].description = description));
};
if (!doc) {
return;
}
return (
<div className="w-full h-full bg-gray-100 p-8">
<h1 className="text-2xl font-bold mb-4">Todos</h1>
<div className="bg-white shadow rounded-lg p-4">
{doc.todos.map((todo: any, index: number) => (
<div className="flex items-center gap-2" key={index}>
<input
type="checkbox"
checked={todo.isDone}
onChange={() => onToggleTodoAt(index)}
className="w-6 h-6"
/>
<input
className="w-full border-none p-2 text-lg focus:outline-none"
value={todo.description}
onChange={(evt) => onEditTodoAt(index, evt.target.value)}
/>
</div>
))}
<button onClick={onAddTodo} className="mt-4 w-full text-xl">
+
</button>
</div>
</div>
);
};
export default TodoListView;