dioxus/examples/login_form.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

2022-01-08 02:20:52 +00:00
//! This example demonstrates the following:
//! Futures in a callback, Router, and Forms
use dioxus::prelude::*;
fn main() {
dioxus_desktop::launch(app);
2022-01-08 02:20:52 +00:00
}
fn app(cx: Scope) -> Element {
2022-02-16 19:08:57 +00:00
let onsubmit = move |evt: FormEvent| {
cx.spawn(async move {
let resp = reqwest::Client::new()
2022-02-23 13:44:16 +00:00
.post("http://localhost:8080/login")
2022-02-16 19:16:03 +00:00
.form(&[
("username", &evt.values["username"]),
("password", &evt.values["password"]),
])
2022-02-16 19:08:57 +00:00
.send()
.await;
2022-01-08 02:20:52 +00:00
2022-02-16 19:08:57 +00:00
match resp {
// Parse data from here, such as storing a response token
2022-02-23 13:44:16 +00:00
Ok(_data) => println!("Login successful!"),
2022-01-08 02:20:52 +00:00
2022-02-16 19:08:57 +00:00
//Handle any errors from the fetch here
2022-02-23 13:44:16 +00:00
Err(_err) => {
println!("Login failed - you need a login server running on localhost:8080.")
}
2022-01-08 02:20:52 +00:00
}
});
};
2022-02-16 19:08:57 +00:00
cx.render(rsx! {
2022-01-08 02:20:52 +00:00
h1 { "Login" }
form {
onsubmit: onsubmit,
2022-02-16 19:08:57 +00:00
prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
2022-11-16 00:37:23 +00:00
input { r#type: "text", id: "username", name: "username" }
2022-02-16 19:08:57 +00:00
label { "Username" }
2022-01-08 02:20:52 +00:00
br {}
2022-11-16 00:37:23 +00:00
input { r#type: "password", id: "password", name: "password" }
label { "Password" }
2022-01-08 02:20:52 +00:00
br {}
2022-02-16 19:08:57 +00:00
button { "Login" }
2022-01-08 02:20:52 +00:00
}
})
2022-02-16 19:08:57 +00:00
}