From d31ca426f715eaa06c8294fbf0dbba43eaf21756 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 31 Oct 2021 22:34:27 +0900 Subject: [PATCH] app: Migrate GroupTable to CommonComponent --- app/src/components/group_table.rs | 81 ++++++++++++------------------- 1 file changed, 31 insertions(+), 50 deletions(-) diff --git a/app/src/components/group_table.rs b/app/src/components/group_table.rs index 4c20129..3933946 100644 --- a/app/src/components/group_table.rs +++ b/app/src/components/group_table.rs @@ -3,12 +3,11 @@ use crate::{ delete_group::DeleteGroup, router::{AppRoute, Link}, }, - infra::api::HostService, + infra::common_component::{CommonComponent, CommonComponentParts}, }; use anyhow::{Error, Result}; use graphql_client::GraphQLQuery; use yew::prelude::*; -use yew::services::{fetch::FetchTask, ConsoleService}; #[derive(GraphQLQuery)] #[graphql( @@ -24,11 +23,8 @@ use get_group_list::ResponseData; pub type Group = get_group_list::GetGroupListGroups; pub struct GroupTable { - link: ComponentLink, + common: CommonComponentParts, groups: Option>, - error: Option, - // Used to keep the request alive long enough. - _task: Option, } pub enum Msg { @@ -37,18 +33,24 @@ pub enum Msg { OnError(Error), } -impl GroupTable { - fn get_groups(&mut self) { - self._task = HostService::graphql_query::( - get_group_list::Variables {}, - self.link.callback(Msg::ListGroupsResponse), - "Error trying to fetch groups", - ) - .map_err(|e| { - ConsoleService::log(&e.to_string()); - e - }) - .ok(); +impl CommonComponent for GroupTable { + fn handle_msg(&mut self, msg: ::Message) -> Result { + match msg { + Msg::ListGroupsResponse(groups) => { + self.groups = Some(groups?.groups.into_iter().collect()); + Ok(true) + } + Msg::OnError(e) => Err(e), + Msg::OnGroupDeleted(group_id) => { + debug_assert!(self.groups.is_some()); + self.groups.as_mut().unwrap().retain(|u| u.id != group_id); + Ok(true) + } + } + } + + fn mut_common(&mut self) -> &mut CommonComponentParts { + &mut self.common } } @@ -56,27 +58,21 @@ impl Component for GroupTable { type Message = Msg; type Properties = (); - fn create(_: Self::Properties, link: ComponentLink) -> Self { + fn create(props: Self::Properties, link: ComponentLink) -> Self { let mut table = GroupTable { - link, - _task: None, + common: CommonComponentParts::::create(props, link), groups: None, - error: None, }; - table.get_groups(); + table.common.call_graphql::( + get_group_list::Variables {}, + Msg::ListGroupsResponse, + "Error trying to fetch groups", + ); table } 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); - true - } - Ok(b) => b, - } + CommonComponentParts::::update(self, msg) } fn change(&mut self, _: Self::Properties) -> ShouldRender { @@ -94,21 +90,6 @@ impl Component for GroupTable { } impl GroupTable { - fn handle_msg(&mut self, msg: ::Message) -> Result { - match msg { - Msg::ListGroupsResponse(groups) => { - self.groups = Some(groups?.groups.into_iter().collect()); - Ok(true) - } - Msg::OnError(e) => Err(e), - Msg::OnGroupDeleted(group_id) => { - debug_assert!(self.groups.is_some()); - self.groups.as_mut().unwrap().retain(|u| u.id != group_id); - Ok(true) - } - } - } - fn view_groups(&self) -> Html { let make_table = |groups: &Vec| { html! { @@ -144,15 +125,15 @@ impl GroupTable { + on_group_deleted=self.common.callback(Msg::OnGroupDeleted) + on_error=self.common.callback(Msg::OnError)/> } } fn view_errors(&self) -> Html { - match &self.error { + match &self.common.error { None => html! {}, Some(e) => html! {
{"Error: "}{e.to_string()}
}, }