2021-10-11 16:41:55 +00:00
|
|
|
use crate::{
|
|
|
|
components::group_table::Group,
|
2021-10-31 13:27:55 +00:00
|
|
|
infra::{
|
|
|
|
common_component::{CommonComponent, CommonComponentParts},
|
|
|
|
modal::Modal,
|
|
|
|
},
|
2021-10-11 16:41:55 +00:00
|
|
|
};
|
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use graphql_client::GraphQLQuery;
|
|
|
|
use yew::prelude::*;
|
|
|
|
|
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/delete_group.graphql",
|
|
|
|
response_derives = "Debug",
|
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
|
|
|
)]
|
|
|
|
pub struct DeleteGroupQuery;
|
|
|
|
|
|
|
|
pub struct DeleteGroup {
|
2021-10-31 13:27:55 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-10-11 16:41:55 +00:00
|
|
|
node_ref: NodeRef,
|
|
|
|
modal: Option<Modal>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(yew::Properties, Clone, PartialEq, Debug)]
|
|
|
|
pub struct DeleteGroupProps {
|
|
|
|
pub group: Group,
|
|
|
|
pub on_group_deleted: Callback<i64>,
|
|
|
|
pub on_error: Callback<Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
ClickedDeleteGroup,
|
|
|
|
ConfirmDeleteGroup,
|
|
|
|
DismissModal,
|
|
|
|
DeleteGroupResponse(Result<delete_group_query::ResponseData>),
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:27:55 +00:00
|
|
|
impl CommonComponent<DeleteGroup> for DeleteGroup {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
|
|
|
Msg::ClickedDeleteGroup => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").show();
|
|
|
|
}
|
|
|
|
Msg::ConfirmDeleteGroup => {
|
|
|
|
self.update(Msg::DismissModal);
|
|
|
|
self.common.call_graphql::<DeleteGroupQuery, _>(
|
|
|
|
delete_group_query::Variables {
|
|
|
|
group_id: self.common.props.group.id,
|
|
|
|
},
|
|
|
|
Msg::DeleteGroupResponse,
|
|
|
|
"Error trying to delete group",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Msg::DismissModal => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").hide();
|
|
|
|
}
|
|
|
|
Msg::DeleteGroupResponse(response) => {
|
|
|
|
self.common.cancel_task();
|
|
|
|
response?;
|
|
|
|
self.common
|
|
|
|
.props
|
|
|
|
.on_group_deleted
|
|
|
|
.emit(self.common.props.group.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:41:55 +00:00
|
|
|
impl Component for DeleteGroup {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = DeleteGroupProps;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
Self {
|
2021-10-31 13:27:55 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-10-11 16:41:55 +00:00
|
|
|
node_ref: NodeRef::default(),
|
|
|
|
modal: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rendered(&mut self, first_render: bool) {
|
|
|
|
if first_render {
|
|
|
|
self.modal = Some(Modal::new(
|
|
|
|
self.node_ref
|
|
|
|
.cast::<web_sys::Element>()
|
|
|
|
.expect("Modal node is not an element"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-31 13:27:55 +00:00
|
|
|
CommonComponentParts::<Self>::update_and_report_error(
|
|
|
|
self,
|
|
|
|
msg,
|
|
|
|
self.common.props.on_error.clone(),
|
|
|
|
)
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, props: Self::Properties) -> ShouldRender {
|
2021-10-31 13:27:55 +00:00
|
|
|
self.common.change(props)
|
2021-10-11 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
class="btn btn-danger"
|
2021-10-31 13:27:55 +00:00
|
|
|
disabled=self.common.is_task_running()
|
|
|
|
onclick=self.common.callback(|_| Msg::ClickedDeleteGroup)>
|
2021-10-11 16:41:55 +00:00
|
|
|
<i class="bi-x-circle-fill" aria-label="Delete group" />
|
|
|
|
</button>
|
|
|
|
{self.show_modal()}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeleteGroup {
|
|
|
|
fn show_modal(&self) -> Html {
|
|
|
|
html! {
|
|
|
|
<div
|
|
|
|
class="modal fade"
|
2021-10-31 13:27:55 +00:00
|
|
|
id="deleteGroupModal".to_string() + &self.common.props.group.id.to_string()
|
2021-10-11 16:41:55 +00:00
|
|
|
tabindex="-1"
|
2021-10-12 03:18:20 +00:00
|
|
|
aria-labelledby="deleteGroupModalLabel"
|
2021-10-11 16:41:55 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
ref=self.node_ref.clone()>
|
|
|
|
<div class="modal-dialog">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2021-10-12 03:18:20 +00:00
|
|
|
<h5 class="modal-title" id="deleteGroupModalLabel">{"Delete group?"}</h5>
|
2021-10-11 16:41:55 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn-close"
|
|
|
|
aria-label="Close"
|
2021-10-31 13:27:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::DismissModal) />
|
2021-10-11 16:41:55 +00:00
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
|
|
<span>
|
|
|
|
{"Are you sure you want to delete group "}
|
2021-10-31 13:27:55 +00:00
|
|
|
<b>{&self.common.props.group.display_name}</b>{"?"}
|
2021-10-11 16:41:55 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
2021-10-31 13:27:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::DismissModal)>
|
2021-10-11 16:41:55 +00:00
|
|
|
{"Cancel"}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2021-10-31 13:27:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::ConfirmDeleteGroup)
|
2021-10-11 16:41:55 +00:00
|
|
|
class="btn btn-danger">{"Yes, I'm sure"}</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|