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() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(app);
|
2022-01-08 02:20:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 19:11:31 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2023-10-23 20:26:10 +00:00
|
|
|
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"]),
|
2023-10-23 20:26:10 +00:00
|
|
|
])
|
|
|
|
.send()
|
|
|
|
.await;
|
2022-01-08 02:20:52 +00:00
|
|
|
|
2023-10-23 20:26:10 +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
|
|
|
|
2023-10-23 20:26:10 +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
|
|
|
}
|
2023-10-23 20:26:10 +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" }
|
2022-02-16 19:11:31 +00:00
|
|
|
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
|
|
|
}
|