Finished agent delete

This commit is contained in:
Franklin 2023-04-25 18:47:38 -04:00
parent f31dd3c4d2
commit b6754102c6
3 changed files with 24 additions and 3 deletions

View File

@ -4,7 +4,7 @@ use yew_router::prelude::use_navigator;
use crate::{ use crate::{
pages::admin::edit::{EditItem, EditType}, pages::admin::edit::{EditItem, EditType},
routes::main_router::Route, routes::main_router::Route, api::backend::delete_agent,
}; };
//TODO: Add admin tag //TODO: Add admin tag
@ -15,8 +15,23 @@ pub fn admin_agent(props: &AdminAgentProps) -> Html {
let is_attempting_delete = use_state(|| false); let is_attempting_delete = use_state(|| false);
let delete_agent = { let delete_agent = {
let is_attempting_delete = is_attempting_delete.clone(); let is_attempting_delete = is_attempting_delete.clone();
let deletecb = props.deletecb.clone();
let index = props.index.clone();
let agent_id = props.agent.id.clone();
Callback::from(move |event: MouseEvent| { Callback::from(move |event: MouseEvent| {
if *is_attempting_delete { if *is_attempting_delete {
let is_attempting_delete = is_attempting_delete.clone();
let deletecb = deletecb.clone();
let index = index.clone();
let agent_id = agent_id.clone();
wasm_bindgen_futures::spawn_local(async move {
match delete_agent(&agent_id).await {
Ok(_) => deletecb.emit(index),
Err(error) => {
log::error!("Error deleting agent: {error}")
}
};
});
// Call delete // Call delete
is_attempting_delete.set(false); is_attempting_delete.set(false);
} else { } else {
@ -59,5 +74,6 @@ pub fn admin_agent(props: &AdminAgentProps) -> Html {
#[derive(PartialEq, Properties, Clone)] #[derive(PartialEq, Properties, Clone)]
pub struct AdminAgentProps { pub struct AdminAgentProps {
pub agent: Agent, pub agent: Agent,
pub deletecb: Callback<usize>,
pub index: usize, pub index: usize,
} }

View File

@ -35,7 +35,13 @@ pub fn admin_agents() -> Html {
<div class={"admin-navbar-divider"}></div> <div class={"admin-navbar-divider"}></div>
{(*agents).clone().into_iter().enumerate().map(|(key, agent)| html! { {(*agents).clone().into_iter().enumerate().map(|(key, agent)| html! {
<> <>
<AdminAgent agent={agent} index={key}/> <AdminAgent agent={agent} index={key} deletecb={
let agents_handle = agents.clone();
Callback::from(move |index| {
let mut agents = (*agents_handle).clone();
agents.remove(index);
agents_handle.set(agents);
})}/>
<div class={"admin-navbar-divider"}></div> <div class={"admin-navbar-divider"}></div>
</> </>
}).collect::<Html>()} }).collect::<Html>()}

View File

@ -67,7 +67,6 @@ pub fn agent_fields(props: &AgentFieldsProps) -> Html {
// Fields to be sent to the backend // Fields to be sent to the backend
let agent_name_handle = agent_name_handle.clone(); let agent_name_handle = agent_name_handle.clone();
let agent_shortcode_handle = agent_shortcode_handle.clone();
let profile_picture_url_handle = profile_picture_url_handle.clone(); let profile_picture_url_handle = profile_picture_url_handle.clone();
let credential_handle = credential_handle.clone(); let credential_handle = credential_handle.clone();
let credential_type = credential_type.clone(); let credential_type = credential_type.clone();