mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
app: Migrate RemoveUserFromGroup to CommonComponent
This commit is contained in:
parent
640126f39a
commit
ec6e1b0c09
@ -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,37 +30,37 @@ 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> {
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
||||||
let group = self.props.group_id;
|
match msg {
|
||||||
self.task = HostService::graphql_query::<RemoveUserFromGroup>(
|
Msg::SubmitRemoveGroup => self.submit_remove_group(),
|
||||||
remove_user_from_group::Variables {
|
Msg::RemoveGroupResponse(response) => {
|
||||||
user: self.props.username.clone(),
|
response?;
|
||||||
group,
|
self.common.cancel_task();
|
||||||
},
|
self.common.props.on_user_removed_from_group.emit((
|
||||||
self.link.callback(Msg::RemoveGroupResponse),
|
self.common.props.username.clone(),
|
||||||
"Error trying to initiate removing the user from a group",
|
self.common.props.group_id,
|
||||||
)
|
));
|
||||||
.map_err(|e| {
|
}
|
||||||
ConsoleService::log(&e.to_string());
|
}
|
||||||
e
|
|
||||||
})
|
|
||||||
.ok();
|
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
||||||
match msg {
|
&mut self.common
|
||||||
Msg::SubmitRemoveGroup => return 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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(true)
|
|
||||||
|
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",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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>
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user