app: Migrate CreateUser to CommonComponent

This commit is contained in:
Valentin Tolmer 2021-10-31 22:22:40 +09:00 committed by nitnelave
parent 29f3636064
commit 587d724c2c

View File

@ -1,10 +1,16 @@
use crate::{components::router::AppRoute, infra::api::HostService}; use crate::{
components::router::AppRoute,
infra::{
api::HostService,
common_component::{CommonComponent, CommonComponentParts},
},
};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use lldap_auth::{opaque, registration}; use lldap_auth::{opaque, registration};
use validator_derive::Validate; use validator_derive::Validate;
use yew::prelude::*; use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService}; use yew::services::ConsoleService;
use yew_form_derive::Model; use yew_form_derive::Model;
use yew_router::{ use yew_router::{
agent::{RouteAgentDispatcher, RouteRequest}, agent::{RouteAgentDispatcher, RouteRequest},
@ -21,12 +27,9 @@ use yew_router::{
pub struct CreateUser; pub struct CreateUser;
pub struct CreateUserForm { pub struct CreateUserForm {
link: ComponentLink<Self>, common: CommonComponentParts<Self>,
route_dispatcher: RouteAgentDispatcher, route_dispatcher: RouteAgentDispatcher,
form: yew_form::Form<CreateUserModel>, form: yew_form::Form<CreateUserModel>,
error: Option<anyhow::Error>,
// Used to keep the request alive long enough.
task: Option<FetchTask>,
} }
#[derive(Model, Validate, PartialEq, Clone, Default)] #[derive(Model, Validate, PartialEq, Clone, Default)]
@ -70,7 +73,7 @@ pub enum Msg {
RegistrationFinishResponse(Result<()>), RegistrationFinishResponse(Result<()>),
} }
impl CreateUserForm { impl CommonComponent<CreateUserForm> for CreateUserForm {
fn handle_msg(&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),
@ -89,11 +92,11 @@ impl CreateUserForm {
lastName: to_option(model.last_name), lastName: to_option(model.last_name),
}, },
}; };
self.task = Some(HostService::graphql_query::<CreateUser>( self.common.call_graphql::<CreateUser, _>(
req, req,
self.link.callback(Msg::CreateUserResponse), Msg::CreateUserResponse,
"Error trying to create user", "Error trying to create user",
)?); );
Ok(true) Ok(true)
} }
Msg::CreateUserResponse(r) => { Msg::CreateUserResponse(r) => {
@ -118,14 +121,11 @@ impl CreateUserForm {
username: user_id, username: user_id,
registration_start_request: message, registration_start_request: message,
}; };
self.task = Some( self.common
HostService::register_start( .call_backend(HostService::register_start, req, move |r| {
req, Msg::RegistrationStartResponse((state, r))
self.link })
.callback_once(move |r| Msg::RegistrationStartResponse((state, r))), .context("Error trying to create user")?;
)
.context("Error trying to create user")?,
);
} else { } else {
self.update(Msg::SuccessfulCreation); self.update(Msg::SuccessfulCreation);
} }
@ -143,13 +143,13 @@ impl CreateUserForm {
server_data: response.server_data, server_data: response.server_data,
registration_upload: registration_upload.message, registration_upload: registration_upload.message,
}; };
self.task = Some( self.common
HostService::register_finish( .call_backend(
HostService::register_finish,
req, req,
self.link.callback(Msg::RegistrationFinishResponse), Msg::RegistrationFinishResponse,
) )
.context("Error trying to register user")?, .context("Error trying to register user")?;
);
Ok(false) Ok(false)
} }
Msg::RegistrationFinishResponse(response) => { Msg::RegistrationFinishResponse(response) => {
@ -163,33 +163,26 @@ impl CreateUserForm {
} }
} }
} }
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
&mut self.common
}
} }
impl Component for CreateUserForm { impl Component for CreateUserForm {
type Message = Msg; type Message = Msg;
type Properties = (); type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { Self {
link, common: CommonComponentParts::<Self>::create(props, link),
route_dispatcher: RouteAgentDispatcher::new(), route_dispatcher: RouteAgentDispatcher::new(),
form: yew_form::Form::<CreateUserModel>::new(CreateUserModel::default()), form: yew_form::Form::<CreateUserModel>::new(CreateUserModel::default()),
error: None,
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_msg(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 {
@ -217,7 +210,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
autocomplete="username" autocomplete="username"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("username")} {&self.form.field_message("username")}
</div> </div>
@ -237,7 +230,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
autocomplete="email" autocomplete="email"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("email")} {&self.form.field_message("email")}
</div> </div>
@ -256,7 +249,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
field_name="display_name" field_name="display_name"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("display_name")} {&self.form.field_message("display_name")}
</div> </div>
@ -275,7 +268,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
field_name="first_name" field_name="first_name"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("first_name")} {&self.form.field_message("first_name")}
</div> </div>
@ -294,7 +287,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
field_name="last_name" field_name="last_name"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("last_name")} {&self.form.field_message("last_name")}
</div> </div>
@ -314,7 +307,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
autocomplete="new-password" autocomplete="new-password"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("password")} {&self.form.field_message("password")}
</div> </div>
@ -334,7 +327,7 @@ impl Component for CreateUserForm {
class_invalid="is-invalid has-error" class_invalid="is-invalid has-error"
class_valid="has-success" class_valid="has-success"
autocomplete="new-password" autocomplete="new-password"
oninput=self.link.callback(|_| Msg::Update) /> oninput=self.common.callback(|_| Msg::Update) />
<div class="invalid-feedback"> <div class="invalid-feedback">
{&self.form.field_message("confirm_password")} {&self.form.field_message("confirm_password")}
</div> </div>
@ -343,14 +336,14 @@ impl Component for CreateUserForm {
<div class="form-group row justify-content-center"> <div class="form-group row justify-content-center">
<button <button
class="btn btn-primary col-auto col-form-label mt-4" class="btn btn-primary col-auto col-form-label mt-4"
disabled=self.task.is_some() disabled=self.common.is_task_running()
type="submit" type="submit"
onclick=self.link.callback(|e: MouseEvent| {e.prevent_default(); Msg::SubmitForm})> onclick=self.common.callback(|e: MouseEvent| {e.prevent_default(); Msg::SubmitForm})>
{"Submit"} {"Submit"}
</button> </button>
</div> </div>
</form> </form>
{ if let Some(e) = &self.error { { if let Some(e) = &self.common.error {
html! { html! {
<div class="alert alert-danger"> <div class="alert alert-danger">
{e.to_string() } {e.to_string() }