app: Migrate Login to CommonComponent

This commit is contained in:
Valentin Tolmer 2021-10-31 21:20:30 +09:00 committed by nitnelave
parent 29962881cf
commit 540ac5d241

View File

@ -1,21 +1,17 @@
use crate::infra::api::HostService; use crate::infra::{
api::HostService,
common_component::{CommonComponent, CommonComponentParts},
};
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use lldap_auth::*; use lldap_auth::*;
use validator_derive::Validate; use validator_derive::Validate;
use yew::{ use yew::{prelude::*, services::ConsoleService};
prelude::*,
services::{fetch::FetchTask, ConsoleService},
};
use yew_form::Form; use yew_form::Form;
use yew_form_derive::Model; use yew_form_derive::Model;
pub struct LoginForm { pub struct LoginForm {
link: ComponentLink<Self>, common: CommonComponentParts<Self>,
on_logged_in: Callback<(String, bool)>,
error: Option<anyhow::Error>,
form: Form<FormModel>, form: Form<FormModel>,
// Used to keep the request alive long enough.
task: Option<FetchTask>,
} }
/// The fields of the form, with the constraints. /// The fields of the form, with the constraints.
@ -44,8 +40,8 @@ pub enum Msg {
AuthenticationFinishResponse(Result<(String, bool)>), AuthenticationFinishResponse(Result<(String, bool)>),
} }
impl LoginForm { impl CommonComponent<LoginForm> for LoginForm {
fn handle_message(&mut self, msg: <Self as Component>::Message) -> Result<bool> { fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
match msg { match msg {
Msg::Update => Ok(true), Msg::Update => Ok(true),
Msg::Submit => { Msg::Submit => {
@ -61,11 +57,10 @@ impl LoginForm {
username, username,
login_start_request: message, login_start_request: message,
}; };
self.task = Some(HostService::login_start( self.common
req, .call_backend(HostService::login_start, req, move |r| {
self.link Msg::AuthenticationStartResponse((state, r))
.callback_once(move |r| Msg::AuthenticationStartResponse((state, r))), })?;
)?);
Ok(true) Ok(true)
} }
Msg::AuthenticationStartResponse((login_start, res)) => { Msg::AuthenticationStartResponse((login_start, res)) => {
@ -77,8 +72,8 @@ impl LoginForm {
// Common error, we want to print a full error to the console but only a // Common error, we want to print a full error to the console but only a
// simple one to the user. // simple one to the user.
ConsoleService::error(&format!("Invalid username or password: {}", e)); ConsoleService::error(&format!("Invalid username or password: {}", e));
self.error = Some(anyhow!("Invalid username or password")); self.common.error = Some(anyhow!("Invalid username or password"));
self.task = None; self.common.cancel_task();
return Ok(true); return Ok(true);
} }
Ok(l) => l, Ok(l) => l,
@ -87,20 +82,26 @@ impl LoginForm {
server_data: res.server_data, server_data: res.server_data,
credential_finalization: login_finish.message, credential_finalization: login_finish.message,
}; };
self.task = Some(HostService::login_finish( self.common.call_backend(
HostService::login_finish,
req, req,
self.link.callback_once(Msg::AuthenticationFinishResponse), Msg::AuthenticationFinishResponse,
)?); )?;
Ok(false) Ok(false)
} }
Msg::AuthenticationFinishResponse(user_info) => { Msg::AuthenticationFinishResponse(user_info) => {
self.task = None; self.common.cancel_task();
self.on_logged_in self.common
.on_logged_in
.emit(user_info.context("Could not log in")?); .emit(user_info.context("Could not log in")?);
Ok(true) Ok(true)
} }
} }
} }
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
&mut self.common
}
} }
impl Component for LoginForm { impl Component for LoginForm {
@ -109,25 +110,13 @@ impl Component for LoginForm {
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
LoginForm { LoginForm {
link, common: CommonComponentParts::<Self>::create(props, link),
on_logged_in: props.on_logged_in,
error: None,
form: Form::<FormModel>::new(FormModel::default()), form: Form::<FormModel>::new(FormModel::default()),
task: None,
} }
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
self.error = None; CommonComponentParts::<Self>::update(self, msg)
match self.handle_message(msg) {
Err(e) => {
ConsoleService::error(&e.to_string());
self.error = Some(e);
self.task = None;
true
}
Ok(b) => b,
}
} }
fn change(&mut self, _: Self::Properties) -> ShouldRender { fn change(&mut self, _: Self::Properties) -> ShouldRender {
@ -153,7 +142,7 @@ impl Component for LoginForm {
field_name="username" field_name="username"
placeholder="Username" placeholder="Username"
autocomplete="username" autocomplete="username"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
</div> </div>
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">
@ -175,13 +164,13 @@ impl Component for LoginForm {
<button <button
type="submit" type="submit"
class="btn btn-primary" class="btn btn-primary"
disabled=self.task.is_some() disabled=self.common.is_task_running()
onclick=self.link.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})> onclick=self.common.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})>
{"Login"} {"Login"}
</button> </button>
</div> </div>
<div class="form-group"> <div class="form-group">
{ if let Some(e) = &self.error { { if let Some(e) = &self.common.error {
html! { e.to_string() } html! { e.to_string() }
} else { html! {} } } else { html! {} }
} }