Made unit deletion work

This commit is contained in:
Franklin 2023-04-25 07:44:20 -04:00
parent 6d577ddb9f
commit 04733dc806
6 changed files with 47 additions and 12 deletions

View File

@ -119,6 +119,7 @@ pub async fn get_all_page_visits_count() -> Result<Count, err::Error> {
) )
.await .await
} }
pub async fn get_all_contacts_count() -> Result<Count, err::Error> { pub async fn get_all_contacts_count() -> Result<Count, err::Error> {
perform_request_without_client::<String, Count>( perform_request_without_client::<String, Count>(
BASE_URL.into(), BASE_URL.into(),
@ -131,6 +132,7 @@ pub async fn get_all_contacts_count() -> Result<Count, err::Error> {
) )
.await .await
} }
pub async fn get_all_contacts() -> Result<Vec<Contact>, err::Error> { pub async fn get_all_contacts() -> Result<Vec<Contact>, err::Error> {
perform_request_without_client::<String, Vec<Contact>>( perform_request_without_client::<String, Vec<Contact>>(
BASE_URL.into(), BASE_URL.into(),
@ -143,6 +145,7 @@ pub async fn get_all_contacts() -> Result<Vec<Contact>, err::Error> {
) )
.await .await
} }
pub async fn create_new_contact_request(contact: ContactPayload) -> Result<(), err::Error> { pub async fn create_new_contact_request(contact: ContactPayload) -> Result<(), err::Error> {
perform_request_without_client( perform_request_without_client(
BASE_URL.into(), BASE_URL.into(),

View File

@ -16,16 +16,16 @@ pub fn admin_project(props: &AdminProjectProps) -> Html {
let delete_project = { let delete_project = {
let is_attempting_delete = is_attempting_delete.clone(); let is_attempting_delete = is_attempting_delete.clone();
let project_id = props.project.id.clone(); let project_id = props.project.id.clone();
let force_update_handle = props.forceupdate.clone(); let deletecb = props.deletecb.clone();
let index = props.index; let index = props.index;
Callback::from(move |event: MouseEvent| { Callback::from(move |event: MouseEvent| {
if *is_attempting_delete { if *is_attempting_delete {
let project_id = project_id.clone(); let project_id = project_id.clone();
let force_update_handle = force_update_handle.clone(); let deletecb = deletecb.clone();
let index = index.clone(); let index = index.clone();
wasm_bindgen_futures::spawn_local(async move { wasm_bindgen_futures::spawn_local(async move {
match delete_project(&project_id).await { match delete_project(&project_id).await {
Ok(_) => force_update_handle.emit(index), Ok(_) => deletecb.emit(index),
Err(error) => { Err(error) => {
log::error!("Error deleting project: {error}") log::error!("Error deleting project: {error}")
} }
@ -82,5 +82,5 @@ pub fn admin_project(props: &AdminProjectProps) -> Html {
pub struct AdminProjectProps { pub struct AdminProjectProps {
pub project: ProjectCardDto, pub project: ProjectCardDto,
pub index: usize, pub index: usize,
pub forceupdate: Callback<usize>, pub deletecb: Callback<usize>,
} }

View File

@ -5,12 +5,13 @@ 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_unit,
}; };
#[function_component(AdminUnit)] #[function_component(AdminUnit)]
pub fn admin_project(props: &AdminUnitProps) -> Html { pub fn admin_unit(props: &AdminUnitProps) -> Html {
let navigator = use_navigator().unwrap(); let navigator = use_navigator().unwrap();
let is_attempting_delete = use_state(|| false);
let price_usd = format!("${}", { let price_usd = format!("${}", {
let price_separated = props.unit.price_usd.separate_with_commas(); let price_separated = props.unit.price_usd.separate_with_commas();
if price_separated.contains(".") { if price_separated.contains(".") {
@ -19,12 +20,27 @@ pub fn admin_project(props: &AdminUnitProps) -> Html {
format!("{price_separated}.00") format!("{price_separated}.00")
} }
}); });
let is_attempting_delete = use_state(|| false);
let delete_unit = { let delete_unit = {
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 unit_id = props.unit.id.clone();
Callback::from(move |event: MouseEvent| { Callback::from(move |event: MouseEvent| {
if *is_attempting_delete { if *is_attempting_delete {
// Call delete // Call delete
let is_attempting_delete = is_attempting_delete.clone();
let deletecb = deletecb.clone();
let index = index.clone();
let unit_id = unit_id.clone();
wasm_bindgen_futures::spawn_local(async move {
match delete_unit(&unit_id).await {
Ok(_) => deletecb.emit(index),
Err(error) => {
log::error!("Error deleting unit: {error}")
}
};
});
is_attempting_delete.set(false); is_attempting_delete.set(false);
} else { } else {
is_attempting_delete.set(true); is_attempting_delete.set(true);
@ -73,4 +89,5 @@ pub fn admin_project(props: &AdminUnitProps) -> Html {
pub struct AdminUnitProps { pub struct AdminUnitProps {
pub unit: Unit, pub unit: Unit,
pub index: usize, pub index: usize,
pub deletecb: Callback<usize>
} }

View File

@ -483,7 +483,7 @@ pub fn generate_fields_for_project(props: &ProjectFieldsProps) -> Html {
<div class={"admin-edit-submit-button"} onclick={update_button_onclick}> <div class={"admin-edit-submit-button"} onclick={update_button_onclick}>
{"Actualizar"} {"Actualizar"}
</div> </div>
<AdminUnits units={(*units).clone()}/> <AdminUnits units={units} onchange={ontype_cb.clone()}/>
</> </>
} }
} }

View File

@ -36,7 +36,7 @@ pub fn admin_projects() -> Html {
<div class={"admin-navbar-divider"}></div> <div class={"admin-navbar-divider"}></div>
{(*projects).clone().into_iter().enumerate().map(|(key, project)| html! { {(*projects).clone().into_iter().enumerate().map(|(key, project)| html! {
<> <>
<AdminProject project={project} index={key} forceupdate={ <AdminProject project={project} index={key} deletecb={
let projects_handle = projects.clone(); let projects_handle = projects.clone();
Callback::from(move |index| { Callback::from(move |index| {
let mut projects = (*projects_handle).clone(); let mut projects = (*projects_handle).clone();

View File

@ -5,14 +5,28 @@ use crate::components::admin_unit::AdminUnit;
#[function_component(AdminUnits)] #[function_component(AdminUnits)]
pub fn admin_units(props: &AdminUnitProps) -> Html { pub fn admin_units(props: &AdminUnitProps) -> Html {
let units_handle = props.units.clone();
html! { html! {
<div class={"admin-start-container"} style={"min-height: 10vh; margin-top: 10vh;"}> <div class={"admin-start-container"} style={"min-height: 10vh; margin-top: 10vh;"}>
<div class={"admin-panel-page-title"}>{"Unidades"}</div> <div class={"admin-panel-page-title"}>{"Unidades"}</div>
<div class={"admin-projects-table"}> <div class={"admin-projects-table"}>
<div class={"admin-navbar-divider"}></div> <div class={"admin-navbar-divider"}></div>
{props.units.clone().into_iter().enumerate().map(|(key, unit)| html! { {(*units_handle).clone().into_iter().enumerate().map(|(key, unit)| html! {
<> <>
<AdminUnit unit={unit} index={key}/> <AdminUnit unit={unit} index={key} deletecb={
let units_handle = units_handle.clone();
let onchange_cb = props.onchange.clone();
Callback::from(move |index| {
match onchange_cb.clone() {
Some(cb) => cb.emit(String::new()),
None => {}
};
let mut units = (*units_handle).clone();
units.remove(index);
units_handle.set(units);
})
}/>
<div class={"admin-navbar-divider"}></div> <div class={"admin-navbar-divider"}></div>
</> </>
}).collect::<Html>()} }).collect::<Html>()}
@ -23,5 +37,6 @@ pub fn admin_units(props: &AdminUnitProps) -> Html {
#[derive(PartialEq, Properties, Clone)] #[derive(PartialEq, Properties, Clone)]
pub struct AdminUnitProps { pub struct AdminUnitProps {
pub units: Vec<Unit>, pub units: UseStateHandle<Vec<Unit>>,
pub onchange: Option<Callback<String>>,
} }