use crate::infra::{ common_component::{CommonComponent, CommonComponentParts}, modal::Modal, }; use anyhow::{Error, Result}; use graphql_client::GraphQLQuery; use yew::prelude::*; #[derive(GraphQLQuery)] #[graphql( schema_path = "../schema.graphql", query_path = "queries/delete_user.graphql", response_derives = "Debug", custom_scalars_module = "crate::infra::graphql" )] pub struct DeleteUserQuery; pub struct DeleteUser { common: CommonComponentParts, node_ref: NodeRef, modal: Option, } #[derive(yew::Properties, Clone, PartialEq, Debug)] pub struct DeleteUserProps { pub username: String, pub on_user_deleted: Callback, pub on_error: Callback, } pub enum Msg { ClickedDeleteUser, ConfirmDeleteUser, DismissModal, 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.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.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 { common: CommonComponentParts::::create(props, link), node_ref: NodeRef::default(), modal: None, } } fn rendered(&mut self, first_render: bool) { if first_render { self.modal = Some(Modal::new( self.node_ref .cast::() .expect("Modal node is not an element"), )); } } fn update(&mut self, msg: Self::Message) -> ShouldRender { CommonComponentParts::::update_and_report_error( self, msg, self.common.on_error.clone(), ) } fn change(&mut self, props: Self::Properties) -> ShouldRender { self.common.change(props) } fn view(&self) -> Html { let link = &self.common; html! { <> {self.show_modal()} } } } impl DeleteUser { fn show_modal(&self) -> Html { let link = &self.common; html! { } } }