dioxus/examples/login_form.rs

45 lines
1.2 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 {
let onsubmit = move |evt: FormEvent| async move {
let resp = reqwest::Client::new()
.post("http://localhost:8080/login")
.form(&[
2024-01-05 01:02:00 +00:00
("username", &evt.values()["username"]),
("password", &evt.values()["password"]),
])
.send()
.await;
2022-01-08 02:20:52 +00:00
match resp {
// Parse data from here, such as storing a response token
Ok(_data) => println!("Login successful!"),
2022-01-08 02:20:52 +00:00
//Handle any errors from the fetch here
Err(_err) => {
println!("Login failed - you need a login server running on localhost:8080.")
2022-01-08 02:20:52 +00:00
}
}
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" }
2024-01-05 01:02:00 +00:00
form { onsubmit: onsubmit,
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
}