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 {
|
2023-03-08 17:05:08 +00:00
|
|
|
fn handle_msg(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Context<Self>,
|
|
|
|
msg: <Self as Component>::Message,
|
|
|
|
) -> Result<bool> {
|
2021-10-31 13:30:55 +00:00
|
|
|
match msg {
|
|
|
|
Msg::ClickedDeleteUser => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").show();
|
|
|
|
}
|
|
|
|
Msg::ConfirmDeleteUser => {
|
2023-03-08 17:05:08 +00:00
|
|
|
self.update(ctx, Msg::DismissModal);
|
2021-10-31 13:30:55 +00:00
|
|
|
self.common.call_graphql::<DeleteUserQuery, _>(
|
2023-03-08 17:05:08 +00:00
|
|
|
ctx,
|
2021-10-31 13:30:55 +00:00
|
|
|
delete_user_query::Variables {
|
2023-03-08 17:05:08 +00:00
|
|
|
user: ctx.props().username.clone(),
|
2021-10-31 13:30:55 +00:00
|
|
|
},
|
|
|
|
Msg::DeleteUserResponse,
|
|
|
|
"Error trying to delete user",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Msg::DismissModal => {
|
|
|
|
self.modal.as_ref().expect("modal not initialized").hide();
|
|
|
|
}
|
|
|
|
Msg::DeleteUserResponse(response) => {
|
|
|
|
response?;
|
2023-03-08 17:05:08 +00:00
|
|
|
ctx.props()
|
2021-10-31 13:30:55 +00:00
|
|
|
.on_user_deleted
|
2023-03-08 17:05:08 +00:00
|
|
|
.emit(ctx.props().username.clone());
|
2021-10-31 13:30:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2023-03-08 17:05:08 +00:00
|
|
|
fn create(_: &Context<Self>) -> Self {
|
2021-09-24 09:12:50 +00:00
|
|
|
Self {
|
2023-03-08 17:05:08 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(),
|
2021-09-24 09:12:50 +00:00
|
|
|
node_ref: NodeRef::default(),
|
|
|
|
modal: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:05:08 +00:00
|
|
|
fn rendered(&mut self, _: &Context<Self>, first_render: bool) {
|
2021-09-24 09:12:50 +00:00
|
|
|
if first_render {
|
|
|
|
self.modal = Some(Modal::new(
|
|
|
|
self.node_ref
|
|
|
|
.cast::<web_sys::Element>()
|
|
|
|
.expect("Modal node is not an element"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:05:08 +00:00
|
|
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
2021-10-31 13:30:55 +00:00
|
|
|
CommonComponentParts::<Self>::update_and_report_error(
|
|
|
|
self,
|
2023-03-08 17:05:08 +00:00
|
|
|
ctx,
|
2021-10-31 13:30:55 +00:00
|
|
|
msg,
|
2023-03-08 17:05:08 +00:00
|
|
|
ctx.props().on_error.clone(),
|
2021-10-31 13:30:55 +00:00
|
|
|
)
|
2021-09-24 09:12:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 17:05:08 +00:00
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
|
|
let link = &ctx.link();
|
2021-09-24 09:12:50 +00:00
|
|
|
html! {
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
class="btn btn-danger"
|
2023-03-08 13:27:47 +00:00
|
|
|
disabled={self.common.is_task_running()}
|
|
|
|
onclick={link.callback(|_| Msg::ClickedDeleteUser)}>
|
2021-09-24 09:12:50 +00:00
|
|
|
<i class="bi-x-circle-fill" aria-label="Delete user" />
|
|
|
|
</button>
|
2023-03-08 17:05:08 +00:00
|
|
|
{self.show_modal(ctx)}
|
2021-09-24 09:12:50 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeleteUser {
|
2023-03-08 17:05:08 +00:00
|
|
|
fn show_modal(&self, ctx: &Context<Self>) -> Html {
|
|
|
|
let link = &ctx.link();
|
2021-09-24 09:12:50 +00:00
|
|
|
html! {
|
|
|
|
<div
|
|
|
|
class="modal fade"
|
2023-03-08 17:05:08 +00:00
|
|
|
id={"deleteUserModal".to_string() + &ctx.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"
|
2023-03-08 13:27:47 +00:00
|
|
|
ref={self.node_ref.clone()}>
|
2021-09-24 09:12:50 +00:00
|
|
|
<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"
|
2023-03-08 13:27:47 +00:00
|
|
|
onclick={link.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 "}
|
2023-03-08 17:05:08 +00:00
|
|
|
<b>{&ctx.props().username}</b>{"?"}
|
2021-09-24 09:12:50 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
2023-03-08 13:27:47 +00:00
|
|
|
onclick={link.callback(|_| Msg::DismissModal)}>
|
2022-11-03 14:40:02 +00:00
|
|
|
<i class="bi-x-circle me-2"></i>
|
|
|
|
{"Cancel"}
|
2021-09-24 09:12:50 +00:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-03-08 13:27:47 +00:00
|
|
|
onclick={link.callback(|_| Msg::ConfirmDeleteUser)}
|
2022-11-03 14:40:02 +00:00
|
|
|
class="btn btn-danger">
|
|
|
|
<i class="bi-check-circle me-2"></i>
|
|
|
|
{"Yes, I'm sure"}
|
|
|
|
</button>
|
2021-09-24 09:12:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|