diff --git a/app/src/components/delete_user.rs b/app/src/components/delete_user.rs index ebe7165..f3f8c1a 100644 --- a/app/src/components/delete_user.rs +++ b/app/src/components/delete_user.rs @@ -1,9 +1,10 @@ -use crate::infra::{api::HostService, modal::Modal}; +use crate::infra::{ + common_component::{CommonComponent, CommonComponentParts}, + modal::Modal, +}; use anyhow::{Error, Result}; use graphql_client::GraphQLQuery; use yew::prelude::*; -use yew::services::fetch::FetchTask; -use yewtil::NeqAssign; #[derive(GraphQLQuery)] #[graphql( @@ -15,11 +16,9 @@ use yewtil::NeqAssign; pub struct DeleteUserQuery; pub struct DeleteUser { - link: ComponentLink, - props: DeleteUserProps, + common: CommonComponentParts, node_ref: NodeRef, modal: Option, - task: Option, } #[derive(yew::Properties, Clone, PartialEq, Debug)] @@ -36,17 +35,51 @@ pub enum Msg { DeleteUserResponse(Result), } +impl CommonComponent for DeleteUser { + fn handle_msg(&mut self, msg: ::Message) -> Result { + match msg { + Msg::ClickedDeleteUser => { + self.modal.as_ref().expect("modal not initialized").show(); + } + Msg::ConfirmDeleteUser => { + self.update(Msg::DismissModal); + self.common.call_graphql::( + delete_user_query::Variables { + user: self.common.props.username.clone(), + }, + Msg::DeleteUserResponse, + "Error trying to delete user", + ); + } + Msg::DismissModal => { + self.modal.as_ref().expect("modal not initialized").hide(); + } + Msg::DeleteUserResponse(response) => { + self.common.cancel_task(); + response?; + self.common + .props + .on_user_deleted + .emit(self.common.props.username.clone()); + } + } + Ok(true) + } + + fn mut_common(&mut self) -> &mut CommonComponentParts { + &mut self.common + } +} + impl Component for DeleteUser { type Message = Msg; type Properties = DeleteUserProps; fn create(props: Self::Properties, link: ComponentLink) -> Self { Self { - link, - props, + common: CommonComponentParts::::create(props, link), node_ref: NodeRef::default(), modal: None, - task: None, } } @@ -61,39 +94,15 @@ impl Component for DeleteUser { } fn update(&mut self, msg: Self::Message) -> ShouldRender { - match msg { - Msg::ClickedDeleteUser => { - self.modal.as_ref().expect("modal not initialized").show(); - } - Msg::ConfirmDeleteUser => { - self.update(Msg::DismissModal); - self.task = HostService::graphql_query::( - delete_user_query::Variables { - user: self.props.username.clone(), - }, - self.link.callback(Msg::DeleteUserResponse), - "Error trying to delete user", - ) - .map_err(|e| self.props.on_error.emit(e)) - .ok(); - } - Msg::DismissModal => { - self.modal.as_ref().expect("modal not initialized").hide(); - } - Msg::DeleteUserResponse(response) => { - self.task = None; - if let Err(e) = response { - self.props.on_error.emit(e); - } else { - self.props.on_user_deleted.emit(self.props.username.clone()); - } - } - } - true + CommonComponentParts::::update_and_report_error( + self, + msg, + self.common.props.on_error.clone(), + ) } fn change(&mut self, props: Self::Properties) -> ShouldRender { - self.props.neq_assign(props) + self.common.change(props) } fn view(&self) -> Html { @@ -101,8 +110,8 @@ impl Component for DeleteUser { <> {self.show_modal()} @@ -116,7 +125,7 @@ impl DeleteUser { html! {