use crate::infra::common_component::{CommonComponent, CommonComponentParts}; use anyhow::{Error, Result}; use graphql_client::GraphQLQuery; use yew::prelude::*; #[derive(GraphQLQuery)] #[graphql( schema_path = "../schema.graphql", query_path = "queries/remove_user_from_group.graphql", response_derives = "Debug", variables_derives = "Clone", custom_scalars_module = "crate::infra::graphql" )] pub struct RemoveUserFromGroup; pub struct RemoveUserFromGroupComponent { common: CommonComponentParts, } #[derive(yew::Properties, Clone, PartialEq)] pub struct Props { pub username: String, pub group_id: i64, pub on_user_removed_from_group: Callback<(String, i64)>, pub on_error: Callback, } pub enum Msg { SubmitRemoveGroup, RemoveGroupResponse(Result), } impl CommonComponent for RemoveUserFromGroupComponent { fn handle_msg( &mut self, ctx: &Context, msg: ::Message, ) -> Result { match msg { Msg::SubmitRemoveGroup => self.submit_remove_group(ctx), Msg::RemoveGroupResponse(response) => { response?; ctx.props() .on_user_removed_from_group .emit((ctx.props().username.clone(), ctx.props().group_id)); } } Ok(true) } fn mut_common(&mut self) -> &mut CommonComponentParts { &mut self.common } } impl RemoveUserFromGroupComponent { fn submit_remove_group(&mut self, ctx: &Context) { self.common.call_graphql::( ctx, remove_user_from_group::Variables { user: ctx.props().username.clone(), group: ctx.props().group_id, }, Msg::RemoveGroupResponse, "Error trying to initiate removing the user from a group", ); } } impl Component for RemoveUserFromGroupComponent { type Message = Msg; type Properties = Props; fn create(_: &Context) -> Self { Self { common: CommonComponentParts::::create(), } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { CommonComponentParts::::update_and_report_error( self, ctx, msg, ctx.props().on_error.clone(), ) } fn view(&self, ctx: &Context) -> Html { let link = &ctx.link(); html! { } } }