use crate::{ components::group_table::Group, 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_group.graphql", response_derives = "Debug", custom_scalars_module = "crate::infra::graphql" )] pub struct DeleteGroupQuery; pub struct DeleteGroup { common: CommonComponentParts, node_ref: NodeRef, modal: Option, } #[derive(yew::Properties, Clone, PartialEq, Debug)] pub struct DeleteGroupProps { pub group: Group, pub on_group_deleted: Callback, pub on_error: Callback, } pub enum Msg { ClickedDeleteGroup, ConfirmDeleteGroup, DismissModal, DeleteGroupResponse(Result), } impl CommonComponent for DeleteGroup { fn handle_msg(&mut self, msg: ::Message) -> Result { match msg { Msg::ClickedDeleteGroup => { self.modal.as_ref().expect("modal not initialized").show(); } Msg::ConfirmDeleteGroup => { self.update(Msg::DismissModal); self.common.call_graphql::( delete_group_query::Variables { group_id: self.common.props.group.id, }, Msg::DeleteGroupResponse, "Error trying to delete group", ); } Msg::DismissModal => { self.modal.as_ref().expect("modal not initialized").hide(); } Msg::DeleteGroupResponse(response) => { self.common.cancel_task(); response?; self.common .props .on_group_deleted .emit(self.common.props.group.id); } } Ok(true) } fn mut_common(&mut self) -> &mut CommonComponentParts { &mut self.common } } impl Component for DeleteGroup { type Message = Msg; type Properties = DeleteGroupProps; 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.props.on_error.clone(), ) } fn change(&mut self, props: Self::Properties) -> ShouldRender { self.common.change(props) } fn view(&self) -> Html { html! { <> {self.show_modal()} } } } impl DeleteGroup { fn show_modal(&self) -> Html { html! { } } }