From 15173c48426e515fac6ab2e7d83b42bf3c00e841 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 10 Feb 2025 00:06:09 +0100 Subject: [PATCH] Do not display remove buttons for scheduled recipes when picking a date --- backend/scss/calendar.scss | 7 -- backend/templates/calendar.html | 3 +- frontend/src/calendar.rs | 117 +++++++++++++------------------- frontend/src/home.rs | 1 + frontend/src/recipe_view.rs | 1 + 5 files changed, 52 insertions(+), 77 deletions(-) diff --git a/backend/scss/calendar.scss b/backend/scss/calendar.scss index 822325e..601893a 100644 --- a/backend/scss/calendar.scss +++ b/backend/scss/calendar.scss @@ -57,13 +57,6 @@ } } -// Deactivate recipe links in dialog mode. -dialog .calendar .scheduled-recipe { - pointer-events: none; - cursor: text; - text-decoration: none; -} - #hidden-templates-calendar { display: none; } \ No newline at end of file diff --git a/backend/templates/calendar.html b/backend/templates/calendar.html index 25f5cf3..b186826 100644 --- a/backend/templates/calendar.html +++ b/backend/templates/calendar.html @@ -47,6 +47,7 @@
-
X
+ +
\ No newline at end of file diff --git a/frontend/src/calendar.rs b/frontend/src/calendar.rs index 62db389..3d5ef74 100644 --- a/frontend/src/calendar.rs +++ b/frontend/src/calendar.rs @@ -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(); - - // 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::().unwrap() * 7 - + day_grid_id[10..11].parse::().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(); - } + 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); + + 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::().unwrap() * 7 + + day_grid_id[10..11].parse::().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); } diff --git a/frontend/src/home.rs b/frontend/src/home.rs index f7ecfe8..942eb43 100644 --- a/frontend/src/home.rs +++ b/frontend/src/home.rs @@ -22,6 +22,7 @@ pub fn setup_page(is_user_logged: bool) -> Result<(), JsValue> { selector(".calendar"), calendar::CalendarOptions { can_select_date: false, + with_link_and_remove: true, }, recipe_scheduler, ); diff --git a/frontend/src/recipe_view.rs b/frontend/src/recipe_view.rs index 8b1062c..575be95 100644 --- a/frontend/src/recipe_view.rs +++ b/frontend/src/recipe_view.rs @@ -28,6 +28,7 @@ pub fn setup_page(recipe_id: i64, is_user_logged: bool) -> Result<(), JsValue> { element.selector(".calendar"), calendar::CalendarOptions { can_select_date: true, + with_link_and_remove: false, }, recipe_scheduler, ) -- 2.49.0