Do not display remove buttons for scheduled recipes when picking a date

This commit is contained in:
Greg Burri 2025-02-10 00:06:09 +01:00
parent 37721ac3ea
commit 15173c4842
5 changed files with 50 additions and 75 deletions

View file

@ -1,15 +1,13 @@
use std::{cell::RefCell, default, rc::Rc};
use std::{cell::RefCell, rc::Rc};
use chrono::{offset::Local, DateTime, Datelike, Days, Months, NaiveDate, Weekday};
use common::ron_api;
use chrono::{offset::Local, Datelike, Days, Months, NaiveDate, Weekday};
use gloo::{console::log, events::EventListener, utils::document};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use web_sys::Element;
use crate::{
recipe_scheduler::{self, RecipeScheduler},
request,
recipe_scheduler::RecipeScheduler,
utils::{by_id, selector, selector_all, SelectorExt},
};
@ -60,7 +58,7 @@ impl CalendarState {
#[derive(Clone, Copy)]
pub struct CalendarOptions {
pub can_select_date: bool,
// pub show_scheduled_recipes: bool,
pub with_link_and_remove: bool,
}
pub fn setup(
@ -104,33 +102,31 @@ pub fn setup(
.forget();
// Click on a day of the current month.
if options.can_select_date {
let days: Element = calendar.selector(".days");
let calendar_clone = calendar.clone();
let state_clone = state.clone();
EventListener::new(&days, "click", move |event| {
let target: Element = event.target().unwrap().dyn_into().unwrap();
let days: Element = calendar.selector(".days");
let calendar_clone = calendar.clone();
let state_clone = state.clone();
EventListener::new(&days, "click", move |event| {
let target: Element = event.target().unwrap().dyn_into().unwrap();
// log!(event);
log!(event);
if target.class_name() == "number" {
let first_day = first_grid_day(state_clone.get_displayed_date());
let day_grid_id = target.parent_element().unwrap().id();
let day_offset = day_grid_id[9..10].parse::<u64>().unwrap() * 7
+ day_grid_id[10..11].parse::<u64>().unwrap();
state_clone.set_selected_date(first_day + Days::new(day_offset));
display_month(
&calendar_clone,
state_clone.clone(),
options,
recipe_scheduler,
);
} else if target.class_name() == "remove-scheduled-recipe" {
log!("REMOVE"); // TODO.
}
})
.forget();
}
if target.class_name() == "number" && options.can_select_date {
let first_day = first_grid_day(state_clone.get_displayed_date());
let day_grid_id = target.parent_element().unwrap().id();
let day_offset = day_grid_id[9..10].parse::<u64>().unwrap() * 7
+ day_grid_id[10..11].parse::<u64>().unwrap();
state_clone.set_selected_date(first_day + Days::new(day_offset));
display_month(
&calendar_clone,
state_clone.clone(),
options,
recipe_scheduler,
);
} else if target.class_name() == "remove-scheduled-recipe" {
log!("REMOVE"); // TODO.
}
})
.forget();
state
}
@ -198,23 +194,7 @@ fn display_month(
}
// Load and display scheduled recipes.
// if options.show_scheduled_recipes {
spawn_local(async move {
// let scheduled_recipes: ron_api::ScheduledRecipes = request::get(
// "calendar/get_scheduled_recipes",
// [
// ("start_date", first_day.date_naive().to_string()),
// (
// "end_date",
// (first_day + Days::new(NB_CALENDAR_ROW * 7))
// .date_naive()
// .to_string(),
// ),
// ],
// )
// .await
// .unwrap();
let scheduled_recipes = recipe_scheduler
.get_scheduled_recipes(first_day, first_day + Days::new(NB_CALENDAR_ROW * 7))
.await
@ -225,7 +205,11 @@ fn display_month(
}
if !scheduled_recipes.is_empty() {
let recipe_template: Element = selector("#hidden-templates-calendar .scheduled-recipe");
let recipe_template: Element = if options.with_link_and_remove {
selector("#hidden-templates-calendar .scheduled-recipe-with-link-and-remove")
} else {
selector("#hidden-templates-calendar .scheduled-recipe")
};
for (date, title, recipe_id) in scheduled_recipes {
let id = format!("scheduled-recipe-{}-{}", recipe_id, date);
if document().get_element_by_id(&id).is_some() {
@ -245,34 +229,29 @@ fn display_month(
.unwrap();
recipe_element.set_id(&id);
let recipe_link_element: Element = recipe_element.selector("a");
// let recipe_remove_element: Element =
// recipe_element.selector(".remove-scheduled-recipe");
//
// EventListener::new(&recipe_remove_element, "click", move |_event| {
// log!("CLICK REMOVE");
// })
// .forget();
recipe_link_element
.set_attribute("href", &format!("/recipe/view/{}", recipe_id))
.unwrap();
recipe_link_element.set_inner_html(&title);
scheduled_recipes_element
.append_child(&recipe_element)
.unwrap();
// log!(&title);
// TODO
let recipe_link_element: Element = if options.with_link_and_remove {
recipe_element.selector("a")
} else {
recipe_element
};
if options.with_link_and_remove {
recipe_link_element
.set_attribute("href", &format!("/recipe/view/{}", recipe_id))
.unwrap();
}
recipe_link_element.set_inner_html(&title);
}
}
});
// }
}
pub fn first_grid_day(mut date: NaiveDate) -> NaiveDate {
fn first_grid_day(mut date: NaiveDate) -> NaiveDate {
while (date - Days::new(1)).month() == date.month() {
date = date - Days::new(1);
}