Turn the login form into a regular form

This commit is contained in:
Valentin Tolmer 2021-05-18 17:44:46 +02:00
parent 29b7f4b836
commit d57cd1230c

View File

@ -3,7 +3,8 @@ use anyhow::{anyhow, Result};
use lldap_model::*; use lldap_model::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use yew::prelude::*; use yew::prelude::*;
use yew::services::fetch::FetchTask; use yew::services::{fetch::FetchTask, ConsoleService};
use yew::FocusEvent;
pub struct LoginForm { pub struct LoginForm {
link: ComponentLink<Self>, link: ComponentLink<Self>,
@ -24,6 +25,13 @@ pub enum Msg {
AuthenticationResponse(Result<String>), AuthenticationResponse(Result<String>),
} }
impl LoginForm {
fn set_error(&mut self, error: anyhow::Error) {
ConsoleService::error(&error.to_string());
self.error = Some(error);
}
}
impl Component for LoginForm { impl Component for LoginForm {
type Message = Msg; type Message = Msg;
type Properties = Props; type Properties = Props;
@ -63,14 +71,14 @@ impl Component for LoginForm {
self.link.callback(Msg::AuthenticationResponse), self.link.callback(Msg::AuthenticationResponse),
) { ) {
Ok(task) => self._task = Some(task), Ok(task) => self._task = Some(task),
Err(e) => self.error = Some(e), Err(e) => self.set_error(e),
} }
} }
Msg::AuthenticationResponse(Ok(user_id)) => { Msg::AuthenticationResponse(Ok(user_id)) => {
self.on_logged_in.emit(user_id); self.on_logged_in.emit(user_id);
} }
Msg::AuthenticationResponse(Err(e)) => { Msg::AuthenticationResponse(Err(e)) => {
self.error = Some(anyhow!("Could not log in: {}", e)); self.set_error(anyhow!("Could not log in: {}", e));
} }
}; };
true true
@ -82,7 +90,7 @@ impl Component for LoginForm {
fn view(&self) -> Html { fn view(&self) -> Html {
html! { html! {
<form ref=self.node_ref.clone()> <form ref=self.node_ref.clone() onsubmit=self.link.callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit })>
<div> <div>
<label for="username">{"User name:"}</label> <label for="username">{"User name:"}</label>
<input type="text" id="username" /> <input type="text" id="username" />
@ -91,7 +99,7 @@ impl Component for LoginForm {
<label for="password">{"Password:"}</label> <label for="password">{"Password:"}</label>
<input type="password" id="password" /> <input type="password" id="password" />
</div> </div>
<button type="button" onclick=self.link.callback(|_| { Msg::Submit })>{"Login"}</button> <button type="submit">{"Login"}</button>
<div> <div>
{ if let Some(e) = &self.error { { if let Some(e) = &self.error {
html! { e.to_string() } html! { e.to_string() }