-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
85 lines (77 loc) · 2.77 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
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
mod core;
mod http;
mod passkey;
use leptos::{
component, create_effect, create_node_ref, create_signal, ev::SubmitEvent, event_target_value,
html::Input, view, window, IntoView, NodeRef, SignalGet, SignalUpdate,
};
use shared::{Event, Status};
#[component]
fn RootComponent() -> impl IntoView {
let core = core::new();
let (view, render) = create_signal(core.view());
let (event, set_event) = create_signal(Event::ServerUrl(
window().location().origin().expect("origin to exist"),
));
create_effect(move |_| {
core::update(&core, event.get(), render);
});
let input_element: NodeRef<Input> = create_node_ref();
let on_submit = move |ev: SubmitEvent| {
ev.prevent_default();
let Some(submitter) = ev.submitter() else {
return;
};
let user_name = input_element.get().expect("<input> to exist").value();
match submitter.id().as_ref() {
"register" => {
set_event.update(|value| *value = Event::Register(user_name));
}
"login" => {
set_event.update(|value| *value = Event::Login(user_name));
}
_ => {}
}
};
let notification = move || match view.get().status {
Status::None => {
view! {<div />}
}
Status::Info(msg) => {
view! {<div class="notification is-info is-light">{msg}</div>}
}
Status::Error(msg) => {
view! {<div class="notification is-warning is-light">{msg}</div>}
}
};
view! {
<section class="box container has-text-centered m-5">
<div class="container" style="max-width:400px">
<form on:submit=on_submit>
<input class="input is-primary" type="text" placeholder="user name"
node_ref=input_element
on:input=move |ev| {
set_event.update(|value| *value = Event::Validate(event_target_value(&ev)));
}
/>
<div class="m-2">{notification}</div>
<div class="buttons section is-centered">
<button id="register" type="submit" class="button is-primary is-warning">
{"Register"}
</button>
<button id="login" type="submit" class="button is-primary is-success">
{"Login"}
</button>
</div>
</form>
</div>
</section>
}
}
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
leptos::mount_to_body(|| {
view! { <RootComponent /> }
});
}