use crate::infra::{api::HostService, modal::Modal}; use anyhow::{Error, Result}; use graphql_client::GraphQLQuery; use yew::prelude::*; use yew::services::fetch::FetchTask; use yewtil::NeqAssign; #[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 { link: ComponentLink, props: DeleteUserProps, node_ref: NodeRef, modal: Option, _task: 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 Component for DeleteUser { type Message = Msg; type Properties = DeleteUserProps; fn create(props: Self::Properties, link: ComponentLink) -> Self { Self { link, props, node_ref: NodeRef::default(), modal: None, _task: 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 { 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) => { if let Err(e) = response { self.props.on_error.emit(e); } else { self.props.on_user_deleted.emit(self.props.username.clone()); } } } true } fn change(&mut self, props: Self::Properties) -> ShouldRender { self.props.neq_assign(props) } fn view(&self) -> Html { html! { <> {self.show_modal()} } } } impl DeleteUser { fn show_modal(&self) -> Html { html! { } } }