Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
262 lines (240 loc) · 7.71 KB
/
Copy pathlib.rs
File metadata and controls
262 lines (240 loc) · 7.71 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
261
262
use chrono::{Local, NaiveDate};
use leptos::{logging::warn, prelude::*};
use reactive_stores::{Field, Patch, Store};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
// ID starts higher than 0 because we have a few starting todos by default
static NEXT_ID: AtomicUsize = AtomicUsize::new(3);
#[derive(Debug, Store, Patch, Serialize, Deserialize)]
struct Todos {
/// Current user.
user: User,
/// Vector storage of a collection of todo's.
#[store(key: usize = |todo| todo.id)]
todos: Vec<Todo>,
/// User names to todo IDs.
#[store(key: Arc<String> = |(name, _)| name.clone())]
completed: BTreeMap<Arc<String>, usize>,
}
impl Todos {
fn data() -> Self {
Self {
user: User {
name: "Bob".to_string(),
email: "lawblog@bobloblaw.com".into(),
},
todos: vec![
Todo {
id: 0,
label: "Create reactive store".to_string(),
status: Status::Pending,
},
Todo {
id: 1,
label: "???".to_string(),
status: Status::Pending,
},
Todo {
id: 2,
label: "Profit".to_string(),
status: Status::Pending,
},
],
completed: Default::default(),
}
}
}
#[derive(Debug, Store, Patch, Serialize, Deserialize)]
struct User {
name: String,
email: String,
}
#[derive(Debug, Store, Patch, Serialize, Deserialize)]
struct Todo {
id: usize,
label: String,
/// the `#[patch] attribute allows you to indicate how a particular field should be patch
/// this expands to `if this != new { *this = new; notify(path); }`
#[patch(|this, new| *this = new)]
status: Status,
}
impl Todo {
pub fn new(label: impl ToString) -> Self {
Self {
id: NEXT_ID.fetch_add(1, Ordering::Relaxed),
label: label.to_string(),
status: Status::Pending,
}
}
}
#[derive(Debug, Default, Clone, Store, Serialize, Deserialize, PartialEq)]
enum Status {
#[default]
Pending,
Scheduled,
ScheduledFor {
date: NaiveDate,
},
Done,
}
impl Status {
pub fn next_step(&mut self) {
*self = match self {
Status::Pending => Status::ScheduledFor {
date: Local::now().naive_local().into(),
},
Status::Scheduled | Status::ScheduledFor { .. } => Status::Done,
Status::Done => Status::Done,
};
}
}
#[component]
pub fn App() -> impl IntoView {
let store = Store::new(Todos::data());
let input_ref = NodeRef::new();
view! {
<p>"Hello, " {move || store.user().name().get()}</p>
<UserForm user=store.user() />
<UserAchievements store />
<hr />
<form on:submit=move |ev| {
ev.prevent_default();
store.todos().write().push(Todo::new(input_ref.get().unwrap().value()));
}>
<label>"Add a Todo" <input type="text" node_ref=input_ref /></label>
<input type="submit" />
</form>
<ol>
// because `todos` is a keyed field, `store.todos()` returns a struct that
// directly implements IntoIterator, so we can use it in <For/> and
// it will manage reactivity for the store fields correctly
<For each=move || store.todos() key=|row| row.id().get() let:todo>
<TodoRow store todo />
</For>
</ol>
<pre>{move || serde_json::to_string_pretty(&*store.read())}</pre>
}
}
#[component]
fn UserForm(#[prop(into)] user: Field<User>) -> impl IntoView {
let error = RwSignal::new(None);
view! {
{move || error.get().map(|n| view! { <p>{n}</p> })}
<form on:submit:target=move |ev| {
ev.prevent_default();
match User::from_event(&ev) {
Ok(new_user) => {
error.set(None);
user.patch(new_user);
}
Err(e) => error.set(Some(e.to_string())),
}
}>
<label>
"Name" <input type="text" name="name" prop:value=move || user.name().get() />
</label>
<label>
"Email" <input type="email" name="email" prop:value=move || user.email().get() />
</label>
<input type="submit" />
</form>
}
}
#[component]
fn UserAchievements(store: Store<Todos>) -> impl IntoView {
let completed = Memo::new(move |_| {
store
.completed()
.at_key(store.user().name().get().into())
.try_get()
.unwrap_or_default()
.to_string()
});
view! {
<div>"You completed: " {completed} " tasks"</div>
<ul>
<For each=move || store.completed() key=|row| row.key() let:achievement>
<div>{achievement.key().to_uppercase()}: {move || achievement.get()}</div>
</For>
</ul>
}
}
#[component]
fn TodoRow(
store: Store<Todos>,
#[prop(into)] todo: Field<Todo>,
) -> impl IntoView {
let status = todo.status();
let title = todo.label();
let editing = RwSignal::new(true);
view! {
<li style:text-decoration=move || {
if status.done() { "line-through" } else { Default::default() }
}>
<p
class:hidden=move || editing.get()
on:click=move |_| {
editing.update(|n| *n = !*n);
}
>
{move || title.get()}
</p>
<input
class:hidden=move || !(editing.get())
type="text"
prop:value=move || title.get()
on:change=move |ev| {
title.set(event_target_value(&ev));
}
/>
<button on:click=move |_| {
let was_undone = !status.done();
status.write().next_step();
if was_undone && status.done() {
let name = store.user().name().get();
store
.completed()
.update(|completed| *completed.entry(name.into()).or_default() += 1)
}
}>
{move || {
if todo.status().done() {
"Done"
} else if status.scheduled() || status.scheduled_for() {
"Scheduled"
} else {
"Pending"
}
}}
</button>
<button on:click=move |_| {
let id = todo.id().get();
store.todos().write().retain(|todo| todo.id != id);
}>"X"</button>
<input
type="date"
prop:value=move || {
todo.status().scheduled_for_date().map(|n| n.get().to_string())
}
class:hidden=move || !todo.status().scheduled_for()
on:change:target=move |ev| {
if let Some(date) = todo.status().scheduled_for_date() {
let value = ev.target().value();
match NaiveDate::parse_from_str(&value, "%Y-%m-%d") {
Ok(new_date) => {
date.set(new_date);
}
Err(e) => warn!("{e}"),
}
}
}
/>
</li>
}
}