app: Migrate RemoveUserFromGroup to CommonComponent

This commit is contained in:
Valentin Tolmer 2021-10-31 22:57:30 +09:00 committed by nitnelave
parent 640126f39a
commit ec6e1b0c09

View File

@ -1,10 +1,7 @@
use crate::infra::api::HostService; use crate::infra::common_component::{CommonComponent, CommonComponentParts};
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use yew::{ use yew::prelude::*;
prelude::*,
services::{fetch::FetchTask, ConsoleService},
};
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
@ -17,10 +14,7 @@ use yew::{
pub struct RemoveUserFromGroup; pub struct RemoveUserFromGroup;
pub struct RemoveUserFromGroupComponent { pub struct RemoveUserFromGroupComponent {
link: ComponentLink<Self>, common: CommonComponentParts<Self>,
props: Props,
// Used to keep the request alive long enough.
task: Option<FetchTask>,
} }
#[derive(yew::Properties, Clone, PartialEq)] #[derive(yew::Properties, Clone, PartialEq)]
@ -36,38 +30,38 @@ pub enum Msg {
RemoveGroupResponse(Result<remove_user_from_group::ResponseData>), RemoveGroupResponse(Result<remove_user_from_group::ResponseData>),
} }
impl RemoveUserFromGroupComponent { impl CommonComponent<RemoveUserFromGroupComponent> for RemoveUserFromGroupComponent {
fn submit_remove_group(&mut self) -> Result<bool> {
let group = self.props.group_id;
self.task = HostService::graphql_query::<RemoveUserFromGroup>(
remove_user_from_group::Variables {
user: self.props.username.clone(),
group,
},
self.link.callback(Msg::RemoveGroupResponse),
"Error trying to initiate removing the user from a group",
)
.map_err(|e| {
ConsoleService::log(&e.to_string());
e
})
.ok();
Ok(true)
}
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> { fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
match msg { match msg {
Msg::SubmitRemoveGroup => return self.submit_remove_group(), Msg::SubmitRemoveGroup => self.submit_remove_group(),
Msg::RemoveGroupResponse(response) => { Msg::RemoveGroupResponse(response) => {
response?; response?;
self.task = None; self.common.cancel_task();
self.props self.common.props.on_user_removed_from_group.emit((
.on_user_removed_from_group self.common.props.username.clone(),
.emit((self.props.username.clone(), self.props.group_id)); self.common.props.group_id,
));
} }
} }
Ok(true) Ok(true)
} }
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
&mut self.common
}
}
impl RemoveUserFromGroupComponent {
fn submit_remove_group(&mut self) {
self.common.call_graphql::<RemoveUserFromGroup, _>(
remove_user_from_group::Variables {
user: self.common.props.username.clone(),
group: self.common.props.group_id,
},
Msg::RemoveGroupResponse,
"Error trying to initiate removing the user from a group",
);
}
} }
impl Component for RemoveUserFromGroupComponent { impl Component for RemoveUserFromGroupComponent {
@ -76,21 +70,16 @@ impl Component for RemoveUserFromGroupComponent {
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { Self {
link, common: CommonComponentParts::<Self>::create(props, link),
props,
task: None,
} }
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match self.handle_msg(msg) { CommonComponentParts::<Self>::update_and_report_error(
Err(e) => { self,
self.task = None; msg,
self.props.on_error.emit(e); self.common.props.on_error.clone(),
true )
}
Ok(b) => b,
}
} }
fn change(&mut self, _: Self::Properties) -> ShouldRender { fn change(&mut self, _: Self::Properties) -> ShouldRender {
@ -101,8 +90,8 @@ impl Component for RemoveUserFromGroupComponent {
html! { html! {
<button <button
class="btn btn-danger" class="btn btn-danger"
disabled=self.task.is_some() disabled=self.common.is_task_running()
onclick=self.link.callback(|_| Msg::SubmitRemoveGroup)> onclick=self.common.callback(|_| Msg::SubmitRemoveGroup)>
<i class="bi-x-circle-fill" aria-label="Remove user from group" /> <i class="bi-x-circle-fill" aria-label="Remove user from group" />
</button> </button>
} }