/
lib.rs
362 lines (331 loc) · 12.4 KB
/
lib.rs
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use leptos::{html::Input, leptos_dom::helpers::location_hash, *};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Todos(pub Vec<Todo>);
const STORAGE_KEY: &str = "todos-leptos";
impl Default for Todos {
fn default() -> Self {
let starting_todos =
window()
.local_storage()
.ok()
.flatten()
.and_then(|storage| {
storage.get_item(STORAGE_KEY).ok().flatten().and_then(
|value| serde_json::from_str::<Vec<Todo>>(&value).ok(),
)
})
.unwrap_or_default();
Self(starting_todos)
}
}
// Basic operations to manipulate the todo list: nothing really interesting here
impl Todos {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn add(&mut self, todo: Todo) {
self.0.push(todo);
}
pub fn remove(&mut self, id: Uuid) {
self.retain(|todo| todo.id != id);
}
pub fn remaining(&self) -> usize {
// `todo.completed` is a signal, so we call .get() to access its value
self.0.iter().filter(|todo| !todo.completed.get()).count()
}
pub fn completed(&self) -> usize {
// `todo.completed` is a signal, so we call .get() to access its value
self.0.iter().filter(|todo| todo.completed.get()).count()
}
pub fn toggle_all(&self) {
// if all are complete, mark them all active
if self.remaining() == 0 {
for todo in &self.0 {
todo.completed.update(|completed| {
if *completed {
*completed = false
}
});
}
}
// otherwise, mark them all complete
else {
for todo in &self.0 {
todo.completed.set(true);
}
}
}
fn clear_completed(&mut self) {
self.retain(|todo| !todo.completed.get());
}
fn retain(&mut self, mut f: impl FnMut(&Todo) -> bool) {
self.0.retain(|todo| {
let retain = f(todo);
// because these signals are created at the top level,
// they are owned by the <TodoMVC/> component and not
// by the individual <Todo/> components. This means
// that if they are not manually disposed when removed, they
// will be held onto until the <TodoMVC/> is unmounted.
if !retain {
todo.title.dispose();
todo.completed.dispose();
}
retain
})
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Todo {
pub id: Uuid,
pub title: RwSignal<String>,
pub completed: RwSignal<bool>,
}
impl Todo {
pub fn new(id: Uuid, title: String) -> Self {
Self::new_with_completed(id, title, false)
}
pub fn new_with_completed(
id: Uuid,
title: String,
completed: bool,
) -> Self {
// RwSignal combines the getter and setter in one struct, rather than separating
// the getter from the setter. This makes it more convenient in some cases, such
// as when we're putting the signals into a struct and passing it around. There's
// no real difference: you could use `create_signal` here, or use `create_rw_signal`
// everywhere.
let title = create_rw_signal(title);
let completed = create_rw_signal(completed);
Self {
id,
title,
completed,
}
}
pub fn toggle(&self) {
// A signal's `update()` function gives you a mutable reference to the current value
// You can use that to modify the value in place, which will notify any subscribers.
self.completed.update(|completed| *completed = !*completed);
}
}
const ESCAPE_KEY: u32 = 27;
const ENTER_KEY: u32 = 13;
#[component]
pub fn TodoMVC() -> impl IntoView {
// The `todos` are a signal, since we need to reactively update the list
let (todos, set_todos) = create_signal(Todos::default());
// We provide a context that each <Todo/> component can use to update the list
// Here, I'm just passing the `WriteSignal`; a <Todo/> doesn't need to read the whole list
// (and shouldn't try to, as that would cause each individual <Todo/> to re-render when
// a new todo is added! This kind of hygiene is why `create_signal` defaults to read-write
// segregation.)
provide_context(set_todos);
// Handle the three filter modes: All, Active, and Completed
let (mode, set_mode) = create_signal(Mode::All);
window_event_listener(ev::hashchange, move |_| {
let new_mode =
location_hash().map(|hash| route(&hash)).unwrap_or_default();
set_mode.set(new_mode);
});
// Callback to add a todo on pressing the `Enter` key, if the field isn't empty
let input_ref = create_node_ref::<Input>();
let add_todo = move |ev: web_sys::KeyboardEvent| {
let input = input_ref.get().unwrap();
ev.stop_propagation();
let key_code = ev.key_code();
if key_code == ENTER_KEY {
let title = input.value();
let title = title.trim();
if !title.is_empty() {
let new = Todo::new(Uuid::new_v4(), title.to_string());
set_todos.update(|t| t.add(new));
input.set_value("");
}
}
};
// A derived signal that filters the list of the todos depending on the filter mode
// This doesn't need to be a `Memo`, because we're only reading it in one place
let filtered_todos = move || {
todos.with(|todos| match mode.get() {
Mode::All => todos.0.to_vec(),
Mode::Active => todos
.0
.iter()
.filter(|todo| !todo.completed.get())
.cloned()
.collect(),
Mode::Completed => todos
.0
.iter()
.filter(|todo| todo.completed.get())
.cloned()
.collect(),
})
};
// Serialization
//
// the effect reads the `todos` signal, and each `Todo`'s title and completed
// status, so it will automatically re-run on any change to the list of tasks
//
// this is the main point of `create_effect`: to synchronize reactive state
// with something outside the reactive system (like localStorage)
create_effect(move |_| {
if let Ok(Some(storage)) = window().local_storage() {
let json = serde_json::to_string(&todos)
.expect("couldn't serialize Todos");
if storage.set_item(STORAGE_KEY, &json).is_err() {
log::error!("error while trying to set item in localStorage");
}
}
});
// focus the main input on load
create_effect(move |_| {
if let Some(input) = input_ref.get() {
let _ = input.focus();
}
});
view! {
<main>
<section class="todoapp">
<header class="header">
<h1>"todos"</h1>
<input
class="new-todo"
placeholder="What needs to be done?"
autofocus
on:keydown=add_todo
node_ref=input_ref
/>
</header>
<section
class="main"
class:hidden={move || todos.with(|t| t.is_empty())}
>
<input id="toggle-all" class="toggle-all" type="checkbox"
prop:checked={move || todos.with(|t| t.remaining() > 0)}
on:input=move |_| todos.with(|t| t.toggle_all())
/>
<label for="toggle-all">"Mark all as complete"</label>
<ul class="todo-list">
<For
each=filtered_todos
key=|todo| todo.id
let:todo
>
<Todo todo/>
</For>
</ul>
</section>
<footer
class="footer"
class:hidden={move || todos.with(|t| t.is_empty())}
>
<span class="todo-count">
<strong>{move || todos.with(|t| t.remaining().to_string())}</strong>
{move || if todos.with(|t| t.remaining()) == 1 {
" item"
} else {
" items"
}}
" left"
</span>
<ul class="filters">
<li><a href="#/" class="selected" class:selected={move || mode.get() == Mode::All}>"All"</a></li>
<li><a href="#/active" class:selected={move || mode.get() == Mode::Active}>"Active"</a></li>
<li><a href="#/completed" class:selected={move || mode.get() == Mode::Completed}>"Completed"</a></li>
</ul>
<button
class="clear-completed hidden"
class:hidden={move || todos.with(|t| t.completed() == 0)}
on:click=move |_| set_todos.update(|t| t.clear_completed())
>
"Clear completed"
</button>
</footer>
</section>
<footer class="info">
<p>"Double-click to edit a todo"</p>
<p>"Created by "<a href="http://todomvc.com">"Greg Johnston"</a></p>
<p>"Part of "<a href="http://todomvc.com">"TodoMVC"</a></p>
</footer>
</main>
}
}
#[component]
pub fn Todo(todo: Todo) -> impl IntoView {
let (editing, set_editing) = create_signal(false);
let set_todos = use_context::<WriteSignal<Todos>>().unwrap();
// this will be filled by node_ref=input below
let todo_input = create_node_ref::<Input>();
let save = move |value: &str| {
let value = value.trim();
if value.is_empty() {
set_todos.update(|t| t.remove(todo.id));
} else {
todo.title.set(value.to_string());
}
set_editing.set(false);
};
view! {
<li
class="todo"
class:editing={editing}
class:completed={todo.completed}
>
<div class="view">
<input
node_ref=todo_input
class="toggle"
type="checkbox"
prop:checked={todo.completed}
on:input={move |ev| {
let checked = event_target_checked(&ev);
todo.completed.set(checked);
}}
/>
<label on:dblclick=move |_| {
set_editing.set(true);
if let Some(input) = todo_input.get() {
_ = input.focus();
}
}>
{todo.title}
</label>
<button class="destroy" on:click=move |_| set_todos.update(|t| t.remove(todo.id))/>
</div>
{move || editing.get().then(|| view! {
<input
class="edit"
class:hidden={move || !editing.get()}
prop:value=todo.title
on:focusout=move |ev: web_sys::FocusEvent| save(&event_target_value(&ev))
on:keyup={move |ev: web_sys::KeyboardEvent| {
let key_code = ev.key_code();
if key_code == ENTER_KEY {
save(&event_target_value(&ev));
} else if key_code == ESCAPE_KEY {
set_editing.set(false);
}
}}
/>
})
}
</li>
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Active,
Completed,
#[default]
All,
}
pub fn route(hash: &str) -> Mode {
match hash {
"/active" => Mode::Active,
"/completed" => Mode::Completed,
_ => Mode::All,
}
}