fix: login form

This commit is contained in:
Jonathan Kelley 2022-02-16 14:08:57 -05:00
parent aa60971c5c
commit c6bdb5db76
1 changed files with 24 additions and 44 deletions

View File

@ -3,14 +3,14 @@
use dioxus::events::*;
use dioxus::prelude::*;
use dioxus::router::{Link, Router, Route, RouterService};
use dioxus::router::{Link, Route, Router, RouterService};
fn main() {
dioxus::desktop::launch(APP);
}
static APP: Component = |cx| {
cx.render(rsx!{
cx.render(rsx! {
Router {
Route { to: "/", home() }
Route { to: "/login", login() }
@ -25,65 +25,45 @@ fn home(cx: Scope) -> Element {
})
}
fn login(cx: Scope) -> Element {
let username = use_state(&cx, String::new);
let password = use_state(&cx, String::new);
let service = cx.consume_context::<RouterService>()?;
let onsubmit = move |_| {
cx.push_future({
let (username, password) = (username.get().clone(), password.get().clone());
let service = service.clone();
let onsubmit = move |evt: FormEvent| {
to_owned![service];
let username = evt.values["username"].clone();
let password = evt.values["password"].clone();
async move {
let params = [
("username", username.to_string()),
("password", password.to_string())
];
cx.spawn(async move {
let resp = reqwest::Client::new()
.post("http://localhost/login")
.form(&[("username", username), ("password", password)])
.send()
.await;
let resp = reqwest::Client::new()
.post("http://localhost/login")
.form(&params)
.send()
.await;
match resp {
// Parse data from here, such as storing a response token
Ok(data) => service.push_route("/"),
match resp {
Ok(data) => {
// Parse data from here, such as storing a response token
service.push_route("/");
}
Err(err) => {} //Handle any errors from the fetch here
}
//Handle any errors from the fetch here
Err(err) => {}
}
});
};
cx.render(rsx!{
cx.render(rsx! {
h1 { "Login" }
form {
onsubmit: onsubmit,
// Prevent the default behavior of <form> to post
prevent_default: "onsubmit",
input {
oninput: move |evt| username.set(evt.value.clone())
}
label {
"Username"
}
prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
input { r#type: "text" }
label { "Username" }
br {}
input {
oninput: move |evt| password.set(evt.value.clone()),
r#type: "password"
}
input { r#type: "password" }
label {
"Password"
}
br {}
button {
"Login"
}
button { "Login" }
}
})
}
}