2021-10-31 13:30:55 +00:00
|
|
|
use crate::infra::{
|
|
|
|
common_component::{CommonComponent, CommonComponentParts},
|
|
|
|
modal::Modal,
|
|
|
|
};
|
2021-09-24 09:12:50 +00:00
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use graphql_client::GraphQLQuery;
|
|
|
|
use yew::prelude::*;
|
|
|
|
|
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/delete_user.graphql",
|
|
|
|
response_derives = "Debug",
|
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
|
|
|
)]
|
|
|
|
pub struct DeleteUserQuery;
|
|
|
|
|
|
|
|
pub struct DeleteUser {
|
2021-10-31 13:30:55 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-09-24 09:12:50 +00:00
|
|
|
node_ref: NodeRef,
|
|
|
|
modal: Option<Modal>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(yew::Properties, Clone, PartialEq, Debug)]
|
|
|
|
pub struct DeleteUserProps {
|
|
|
|
pub username: String,
|
|
|
|
pub on_user_deleted: Callback<String>,
|
|
|
|
pub on_error: Callback<Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
|
|
|
ClickedDeleteUser,
|
|
|
|
ConfirmDeleteUser,
|
|
|
|
DismissModal,
|
|
|
|
DeleteUserResponse(Result<delete_user_query::ResponseData>),
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:30:55 +00:00
|
|
|
impl CommonComponent<DeleteUser> for DeleteUser {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
|
|
|
Msg::ClickedDeleteUser => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").show();
|
|
|
|
}
|
|
|
|
Msg::ConfirmDeleteUser => {
|
|
|
|
self.update(Msg::DismissModal);
|
|
|
|
self.common.call_graphql::<DeleteUserQuery, _>(
|
|
|
|
delete_user_query::Variables {
|
|
|
|
user: self.common.props.username.clone(),
|
|
|
|
},
|
|
|
|
Msg::DeleteUserResponse,
|
|
|
|
"Error trying to delete user",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Msg::DismissModal => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").hide();
|
|
|
|
}
|
|
|
|
Msg::DeleteUserResponse(response) => {
|
|
|
|
self.common.cancel_task();
|
|
|
|
response?;
|
|
|
|
self.common
|
|
|
|
.props
|
|
|
|
.on_user_deleted
|
|
|
|
.emit(self.common.props.username.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 09:12:50 +00:00
|
|
|
impl Component for DeleteUser {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = DeleteUserProps;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
Self {
|
2021-10-31 13:30:55 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-09-24 09:12:50 +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:30:55 +00:00
|
|
|
CommonComponentParts::<Self>::update_and_report_error(
|
|
|
|
self,
|
|
|
|
msg,
|
|
|
|
self.common.props.on_error.clone(),
|
|
|
|
)
|
2021-09-24 09:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, props: Self::Properties) -> ShouldRender {
|
2021-10-31 13:30:55 +00:00
|
|
|
self.common.change(props)
|
2021-09-24 09:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
class="btn btn-danger"
|
2021-10-31 13:30:55 +00:00
|
|
|
disabled=self.common.is_task_running()
|
|
|
|
onclick=self.common.callback(|_| Msg::ClickedDeleteUser)>
|
2021-09-24 09:12:50 +00:00
|
|
|
<i class="bi-x-circle-fill" aria-label="Delete user" />
|
|
|
|
</button>
|
|
|
|
{self.show_modal()}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeleteUser {
|
|
|
|
fn show_modal(&self) -> Html {
|
|
|
|
html! {
|
|
|
|
<div
|
|
|
|
class="modal fade"
|
2021-10-31 13:30:55 +00:00
|
|
|
id="deleteUserModal".to_string() + &self.common.props.username
|
2021-09-24 09:12:50 +00:00
|
|
|
tabindex="-1"
|
|
|
|
//role="dialog"
|
2021-10-12 03:18:20 +00:00
|
|
|
aria-labelledby="deleteUserModalLabel"
|
2021-09-24 09:12:50 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
ref=self.node_ref.clone()>
|
|
|
|
<div class="modal-dialog" /*role="document"*/>
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2021-10-12 03:18:20 +00:00
|
|
|
<h5 class="modal-title" id="deleteUserModalLabel">{"Delete user?"}</h5>
|
2021-09-24 09:12:50 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn-close"
|
|
|
|
aria-label="Close"
|
2021-10-31 13:30:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::DismissModal) />
|
2021-09-24 09:12:50 +00:00
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
|
|
<span>
|
|
|
|
{"Are you sure you want to delete user "}
|
2021-10-31 13:30:55 +00:00
|
|
|
<b>{&self.common.props.username}</b>{"?"}
|
2021-09-24 09:12:50 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
2021-10-31 13:30:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::DismissModal)>
|
2021-09-24 09:12:50 +00:00
|
|
|
{"Cancel"}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2021-10-31 13:30:55 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::ConfirmDeleteUser)
|
2021-09-24 09:12:50 +00:00
|
|
|
class="btn btn-danger">{"Yes, I'm sure"}</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|