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