From 587d724c2c91b17ae7f8365f4b7c5208263bfe1c Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 31 Oct 2021 22:22:40 +0900 Subject: [PATCH] app: Migrate CreateUser to CommonComponent --- app/src/components/create_user.rs | 87 ++++++++++++++----------------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/app/src/components/create_user.rs b/app/src/components/create_user.rs index 3a534ab..1745b3b 100644 --- a/app/src/components/create_user.rs +++ b/app/src/components/create_user.rs @@ -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 graphql_client::GraphQLQuery; use lldap_auth::{opaque, registration}; use validator_derive::Validate; use yew::prelude::*; -use yew::services::{fetch::FetchTask, ConsoleService}; +use yew::services::ConsoleService; use yew_form_derive::Model; use yew_router::{ agent::{RouteAgentDispatcher, RouteRequest}, @@ -21,12 +27,9 @@ use yew_router::{ pub struct CreateUser; pub struct CreateUserForm { - link: ComponentLink, + common: CommonComponentParts, route_dispatcher: RouteAgentDispatcher, form: yew_form::Form, - error: Option, - // Used to keep the request alive long enough. - task: Option, } #[derive(Model, Validate, PartialEq, Clone, Default)] @@ -70,7 +73,7 @@ pub enum Msg { RegistrationFinishResponse(Result<()>), } -impl CreateUserForm { +impl CommonComponent for CreateUserForm { fn handle_msg(&mut self, msg: ::Message) -> Result { match msg { Msg::Update => Ok(true), @@ -89,11 +92,11 @@ impl CreateUserForm { lastName: to_option(model.last_name), }, }; - self.task = Some(HostService::graphql_query::( + self.common.call_graphql::( req, - self.link.callback(Msg::CreateUserResponse), + Msg::CreateUserResponse, "Error trying to create user", - )?); + ); Ok(true) } Msg::CreateUserResponse(r) => { @@ -118,14 +121,11 @@ impl CreateUserForm { username: user_id, registration_start_request: message, }; - self.task = Some( - HostService::register_start( - req, - self.link - .callback_once(move |r| Msg::RegistrationStartResponse((state, r))), - ) - .context("Error trying to create user")?, - ); + self.common + .call_backend(HostService::register_start, req, move |r| { + Msg::RegistrationStartResponse((state, r)) + }) + .context("Error trying to create user")?; } else { self.update(Msg::SuccessfulCreation); } @@ -143,13 +143,13 @@ impl CreateUserForm { server_data: response.server_data, registration_upload: registration_upload.message, }; - self.task = Some( - HostService::register_finish( + self.common + .call_backend( + HostService::register_finish, req, - self.link.callback(Msg::RegistrationFinishResponse), + Msg::RegistrationFinishResponse, ) - .context("Error trying to register user")?, - ); + .context("Error trying to register user")?; Ok(false) } Msg::RegistrationFinishResponse(response) => { @@ -163,33 +163,26 @@ impl CreateUserForm { } } } + + fn mut_common(&mut self) -> &mut CommonComponentParts { + &mut self.common + } } impl Component for CreateUserForm { type Message = Msg; type Properties = (); - fn create(_: Self::Properties, link: ComponentLink) -> Self { + fn create(props: Self::Properties, link: ComponentLink) -> Self { Self { - link, + common: CommonComponentParts::::create(props, link), route_dispatcher: RouteAgentDispatcher::new(), form: yew_form::Form::::new(CreateUserModel::default()), - error: None, - task: None, } } fn update(&mut self, msg: Self::Message) -> ShouldRender { - self.error = None; - match self.handle_msg(msg) { - Err(e) => { - ConsoleService::error(&e.to_string()); - self.error = Some(e); - self.task = None; - true - } - Ok(b) => b, - } + CommonComponentParts::::update(self, msg) } fn change(&mut self, _: Self::Properties) -> ShouldRender { @@ -217,7 +210,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="username" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("username")}
@@ -237,7 +230,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="email" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("email")}
@@ -256,7 +249,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" field_name="display_name" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("display_name")}
@@ -275,7 +268,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" field_name="first_name" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("first_name")}
@@ -294,7 +287,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" field_name="last_name" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("last_name")}
@@ -314,7 +307,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="new-password" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("password")}
@@ -334,7 +327,7 @@ impl Component for CreateUserForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="new-password" - oninput=self.link.callback(|_| Msg::Update) /> + oninput=self.common.callback(|_| Msg::Update) />
{&self.form.field_message("confirm_password")}
@@ -343,14 +336,14 @@ impl Component for CreateUserForm {
- { if let Some(e) = &self.error { + { if let Some(e) = &self.common.error { html! {
{e.to_string() }