-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
52 lines (48 loc) · 1.69 KB
/
main.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
use prest::*;
#[derive(Debug, Table, Default, serde::Serialize, serde::Deserialize)]
struct Todo {
#[serde(default = "Uuid::new_v4")]
pub id: Uuid,
pub task: String,
#[serde(default)]
pub done: bool,
}
impl Render for Todo {
fn render(&self) -> Markup {
html! {
."flex items-center" hx-target="this" hx-swap="outerHTML" hx-vals=(json!(self)) {
input."toggle toggle-primary" type="checkbox" hx-patch="/" checked[self.done] {}
label."ml-4 text-lg" {(self.task)}
button."btn btn-ghost ml-auto" hx-delete="/" {"Delete"}
}
}
}
}
async fn into_page(content: Markup) -> Markup {
html! {(DOCTYPE) html data-theme="dark" {(Head::with_title("Todo app"))
body."max-w-screen-sm mx-auto mt-12 flex flex-col items-center" {
form hx-put="/" hx-target="div" hx-swap="beforeend" hx-on--after-request="this.reset()" {
input."input input-bordered input-primary" type="text" name="task" {}
button."btn btn-outline btn-primary ml-4" type="submit" {"Add"}
}
."w-full" {(content)}
(Scripts::default())
}
}}
}
fn main() {
init!(tables Todo);
route(
"/",
get(|| async { html!(@for todo in Todo::find_all() {(todo)}) })
.put(|Form(todo): Form<Todo>| async move { todo.save().unwrap().render() })
.patch(|Form(mut todo): Form<Todo>| async move {
todo.update_done(!todo.done).unwrap().render()
})
.delete(|Form(todo): Form<Todo>| async move {
todo.remove().unwrap();
}),
)
.wrap_non_htmx(into_page)
.run();
}