Repository navigation
Expand file tree
/
Copy pathTodoScreen.tsx
More file actions
157 lines (151 loc) · 4.65 KB
/
Copy pathTodoScreen.tsx
File metadata and controls
157 lines (151 loc) · 4.65 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
import { useEffect, useState, type FormEvent } from "react";
import type { EvenStorage } from "../live-cache";
import type { TodoItem } from "../live-state";
import type { PhoneStringKey } from "../phone-i18n";
import { PhoneIcon } from "../phone-icons";
import {
addTodo,
deleteTodo,
renameTodo,
toggleTodo,
writeTodos,
} from "../todos";
export function TodoScreen({
items,
storage,
t,
onChange,
}: {
readonly items: readonly TodoItem[];
readonly storage?: EvenStorage;
readonly t: (key: PhoneStringKey) => string;
readonly onChange: (items: readonly TodoItem[]) => void;
}) {
const [local, setLocal] = useState(items);
const [title, setTitle] = useState("");
const [editing, setEditing] = useState<string>();
const [confirming, setConfirming] = useState<string>();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string>();
useEffect(() => setLocal(items), [items]);
const commit = async (next: readonly TodoItem[]) => {
if (!storage || busy) return false;
setBusy(true);
const saved = await writeTodos(storage, next);
if (saved) {
setLocal(next);
onChange(next);
setError(undefined);
} else {
setError(t("storageFailed"));
}
setBusy(false);
return saved;
};
const submit = (event: FormEvent) => {
event.preventDefault();
try {
const next = addTodo(local, title);
void commit(next).then((saved) => {
if (saved) setTitle("");
});
} catch (caught) {
setError(caught instanceof Error && caught.message === "todo_limit"
? t("taskLimit")
: t("validationFailed"));
}
};
const remove = (id: string) => {
if (confirming !== id) {
setConfirming(id);
setError(t("confirmDelete"));
return;
}
try {
void commit(deleteTodo(local, id)).then((saved) => {
if (saved) setConfirming(undefined);
});
} catch {
setError(t("validationFailed"));
}
};
return (
<div className="phone-detail-stack">
<form className="phone-inline-form" onSubmit={submit}>
<label>
<span className="phone-visually-hidden">{t("taskTitle")}</span>
<input
value={title}
maxLength={40}
disabled={busy || local.length >= 6}
placeholder={t("taskTitle")}
onChange={(event) => setTitle(event.target.value)}
/>
</label>
<button type="submit" disabled={busy || !title.trim()}>
<PhoneIcon name="plus" size={22} />
<span className="phone-visually-hidden">{t("addTask")}</span>
</button>
</form>
<section className="phone-panel phone-item-list">
{local.map((item, index) => (
<div className="phone-item-row" key={item.id}>
<button
type="button"
className="phone-check-button"
aria-pressed={item.completed}
disabled={busy}
onClick={() => void commit(toggleTodo(local, index))}
>
<PhoneIcon
name={item.completed ? "checkboxOn" : "checkbox"}
size={24}
/>
<span className="phone-visually-hidden">
{item.completed ? t("enabled") : t("disabled")}
</span>
</button>
{editing === item.id ? (
<input
autoFocus
defaultValue={item.title}
maxLength={40}
onBlur={(event) => {
try {
void commit(renameTodo(local, item.id, event.target.value));
} catch {
setError(t("validationFailed"));
}
setEditing(undefined);
}}
/>
) : (
<span className={item.completed ? "is-complete" : ""}>
{item.title}
</span>
)}
<button
type="button"
className="phone-icon-button"
aria-label={`${t("edit")} ${item.title}`}
disabled={busy}
onClick={() => setEditing(item.id)}
>
<PhoneIcon name="edit" size={20} />
</button>
<button
type="button"
className="phone-icon-button"
aria-label={`${t("delete")} ${item.title}`}
disabled={busy}
onClick={() => remove(item.id)}
>
<PhoneIcon name="trash" size={20} />
</button>
</div>
))}
</section>
{error && <p role="alert" className="phone-form-message">{error}</p>}
</div>
);
}