From 65dd1d1fd33e3c5683c5271bc8492dd16e740435 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 31 Oct 2021 21:19:54 +0900 Subject: [PATCH] app,infra: Move more functionality in CommonComponent --- app/src/components/change_password.rs | 12 +++---- app/src/components/group_details.rs | 29 +++++------------ app/src/infra/common_component.rs | 47 ++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/app/src/components/change_password.rs b/app/src/components/change_password.rs index 5e53219..39f2a4a 100644 --- a/app/src/components/change_password.rs +++ b/app/src/components/change_password.rs @@ -176,7 +176,7 @@ impl CommonComponent for ChangePasswordForm { Ok(false) } Msg::RegistrationFinishResponse(response) => { - self.common.task = None; + self.common.cancel_task(); if response.is_ok() { self.route_dispatcher .send(RouteRequest::ChangeRoute(Route::from( @@ -236,7 +236,7 @@ impl Component for ChangePasswordForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="current-password" - oninput=self.common.link.callback(|_| Msg::FormUpdate) /> + oninput=self.common.callback(|_| Msg::FormUpdate) />
{&self.form.field_message("old_password")}
@@ -256,7 +256,7 @@ impl Component for ChangePasswordForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="new-password" - oninput=self.common.link.callback(|_| Msg::FormUpdate) /> + oninput=self.common.callback(|_| Msg::FormUpdate) />
{&self.form.field_message("password")}
@@ -275,7 +275,7 @@ impl Component for ChangePasswordForm { class_invalid="is-invalid has-error" class_valid="has-success" autocomplete="new-password" - oninput=self.common.link.callback(|_| Msg::FormUpdate) /> + oninput=self.common.callback(|_| Msg::FormUpdate) />
{&self.form.field_message("confirm_password")}
@@ -285,8 +285,8 @@ impl Component for ChangePasswordForm { diff --git a/app/src/components/group_details.rs b/app/src/components/group_details.rs index 9fd85ee..7913328 100644 --- a/app/src/components/group_details.rs +++ b/app/src/components/group_details.rs @@ -4,17 +4,11 @@ use crate::{ remove_user_from_group::RemoveUserFromGroupComponent, router::{AppRoute, Link}, }, - infra::{ - common_component::{CommonComponent, CommonComponentParts}, - api::HostService, - } + infra::common_component::{CommonComponent, CommonComponentParts}, }; use anyhow::{bail, Error, Result}; use graphql_client::GraphQLQuery; -use yew::{ - prelude::*, - services::ConsoleService, -}; +use yew::prelude::*; #[derive(GraphQLQuery)] #[graphql( @@ -53,18 +47,13 @@ pub struct Props { impl GroupDetails { fn get_group_details(&mut self) { - self.common.task = HostService::graphql_query::( + self.common.call_graphql::( get_group_details::Variables { id: self.common.group_id, }, - self.common.link.callback(Msg::GroupDetailsResponse), + Msg::GroupDetailsResponse, "Error trying to fetch group details", - ) - .map_err(|e| { - ConsoleService::log(&e.to_string()); - e - }) - .ok(); + ); } fn view_messages(&self, error: &Option) -> Html { @@ -95,8 +84,8 @@ impl GroupDetails { + on_user_removed_from_group=self.common.callback(Msg::OnUserRemovedFromGroup) + on_error=self.common.callback(Msg::OnError)/> } @@ -145,8 +134,8 @@ impl GroupDetails { + on_error=self.common.callback(Msg::OnError) + on_user_added_to_group=self.common.callback(Msg::OnUserAddedToGroup)/> } } } diff --git a/app/src/infra/common_component.rs b/app/src/infra/common_component.rs index 36f82ba..048fb4f 100644 --- a/app/src/infra/common_component.rs +++ b/app/src/infra/common_component.rs @@ -1,4 +1,6 @@ +use crate::infra::api::HostService; use anyhow::{Error, Result}; +use graphql_client::GraphQLQuery; use yew::{ prelude::*, services::{fetch::FetchTask, ConsoleService}, @@ -10,13 +12,21 @@ pub trait CommonComponent>: Component { } pub struct CommonComponentParts> { - pub link: ComponentLink, + link: ComponentLink, pub props: ::Properties, pub error: Option, - pub task: Option, + task: Option, } impl> CommonComponentParts { + pub fn is_task_running(&self) -> bool { + self.task.is_some() + } + + pub fn cancel_task(&mut self) { + self.task = None; + } + pub fn create(props: ::Properties, link: ComponentLink) -> Self { Self { link, @@ -38,6 +48,14 @@ impl> CommonComponentParts { } } + pub fn callback(&self, function: F) -> Callback + where + M: Into, + F: Fn(IN) -> M + 'static, + { + self.link.callback(function) + } + pub fn call_backend( &mut self, method: M, @@ -46,11 +64,32 @@ impl> CommonComponentParts { ) -> Result<()> where M: Fn(Req, Callback) -> Result, - Cb: Fn(Resp) -> ::Message + 'static, + Cb: FnOnce(Resp) -> ::Message + 'static, { - self.task = Some(method(req, self.link.callback(callback))?); + self.task = Some(method(req, self.link.callback_once(callback))?); Ok(()) } + + pub fn call_graphql( + &mut self, + variables: QueryType::Variables, + enum_callback: EnumCallback, + error_message: &'static str, + ) where + QueryType: GraphQLQuery + 'static, + EnumCallback: Fn(Result) -> ::Message + 'static, + { + self.task = HostService::graphql_query::( + variables, + self.link.callback(enum_callback), + error_message, + ) + .map_err::<(), _>(|e| { + ConsoleService::log(&e.to_string()); + self.error = Some(e); + }) + .ok(); + } } impl> std::ops::Deref for CommonComponentParts {