Scheduled recipes can now be removed

This commit is contained in:
Greg Burri 2025-02-12 00:22:50 +01:00
parent 084be9fb00
commit a3f2b4a86a
8 changed files with 64 additions and 7 deletions

View file

@ -1,14 +1,18 @@
use std::{cell::RefCell, rc::Rc};
use chrono::{offset::Local, Datelike, Days, Months, NaiveDate, Weekday};
use common::{ron_api, utils::substitute_with_names};
use gloo::{console::log, events::EventListener, utils::document};
use scanf::sscanf;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use web_sys::Element;
use crate::{
modal_dialog,
recipe_scheduler::RecipeScheduler,
utils::{by_id, selector, selector_all, SelectorExt},
request,
utils::{by_id, get_locale, selector, selector_all, SelectorExt},
};
struct CalendarStateInternal {
@ -123,7 +127,46 @@ pub fn setup(
recipe_scheduler,
);
} else if target.class_name() == "remove-scheduled-recipe" {
log!("REMOVE"); // TODO.
spawn_local(async move {
let mut recipe_id: i64 = 0;
let mut date: NaiveDate = NaiveDate::default();
sscanf!(
&target.parent_element().unwrap().id(),
"scheduled-recipe-{}-{}",
recipe_id,
date
)
.unwrap();
let title = target.previous_element_sibling().unwrap().inner_html();
if modal_dialog::show_and_initialize(
"#hidden-templates-calendar .unschedule-confirmation",
async |element| {
let date_format =
selector::<Element>("#hidden-templates-calendar .calendar-date-format")
.inner_html();
element.set_inner_html(&substitute_with_names(
&element.inner_html(),
&["{title}", "{date}"],
&[
&title,
&date
.format_localized(&date_format, get_locale())
.to_string(),
],
));
},
)
.await
.is_some()
{
let body = ron_api::ScheduledRecipe { recipe_id, date };
let _ =
request::delete::<(), _>("calendar/remove_scheduled_recipe", body).await;
target.parent_element().unwrap().remove();
}
});
}
})
.forget();