login_form.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //! This example demonstrates the following:
  2. //! Futures in a callback, Router, and Forms
  3. use dioxus::prelude::*;
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. fn app(cx: Scope) -> Element {
  8. let onsubmit = move |evt: FormEvent| {
  9. cx.spawn(async move {
  10. let resp = reqwest::Client::new()
  11. .post("http://localhost:8080/login")
  12. .form(&[
  13. ("username", &evt.values["username"]),
  14. ("password", &evt.values["password"]),
  15. ])
  16. .send()
  17. .await;
  18. match resp {
  19. // Parse data from here, such as storing a response token
  20. Ok(_data) => println!("Login successful!"),
  21. //Handle any errors from the fetch here
  22. Err(_err) => {
  23. println!("Login failed - you need a login server running on localhost:8080.")
  24. }
  25. }
  26. });
  27. };
  28. cx.render(rsx! {
  29. h1 { "Login" }
  30. form {
  31. onsubmit: onsubmit,
  32. prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
  33. input { r#type: "text", id: "username", name: "username" }
  34. label { "Username" }
  35. br {}
  36. input { r#type: "password", id: "password", name: "password" }
  37. label { "Password" }
  38. br {}
  39. button { "Login" }
  40. }
  41. })
  42. }