Recipe can now be scheduled

This commit is contained in:
Greg Burri 2025-02-04 22:29:56 +01:00
parent ae6da1a5ae
commit fbef990022
18 changed files with 233 additions and 51 deletions

View file

@ -1,8 +1,9 @@
use std::future::Future;
use std::{cell::RefCell, future::Future, rc::Rc, str::FromStr};
use common::ron_api;
use chrono::Locale;
use common::{ron_api, utils::substitute};
use gloo::{
console::console,
console::log,
events::EventListener,
net::http::Request,
utils::{document, window},
@ -24,13 +25,59 @@ pub fn setup_page(recipe_id: i64) -> Result<(), JsValue> {
let add_to_planner: Element = selector("#recipe-view .add-to-planner");
EventListener::new(&add_to_planner, "click", move |_event| {
spawn_local(async move {
modal_dialog::show_and_initialize(
if let Some((date, servings)) = modal_dialog::show_and_initialize_with_ok(
"#hidden-templates .date-and-servings",
async |element| {
calendar::setup(element.selector(".calendar"));
async |element| calendar::setup(element.selector(".calendar")),
|element, calendar_state| {
let servings_element: HtmlInputElement = element.selector("#input-servings");
(
calendar_state.get_selected_date().date_naive(),
servings_element.value_as_number() as u32,
)
},
)
.await;
.await
{
if request::post::<(), _>(
"calendar/schedule_recipe",
ron_api::ScheduleRecipe {
recipe_id,
date,
servings,
},
)
.await
.is_ok()
{
toast::show_element_and_initialize(
Level::Success,
"#hidden-templates .calendar-add-to-planner-success",
|element| {
let title =
selector::<Element>("#recipe-view .recipe-title").inner_html();
let date_format =
selector::<Element>("#hidden-templates .calendar-date-format")
.inner_html();
let locale = {
let lang_and_territory = selector::<Element>("html")
.get_attribute("lang")
.unwrap()
.replace("-", "_");
Locale::from_str(&lang_and_territory).unwrap_or_default()
};
element.set_inner_html(&substitute(
&element.inner_html(),
"{}",
&[
&title,
&date.format_localized(&date_format, locale).to_string(),
],
));
},
);
}
}
});
})
.forget();