Shopping list (WIP)

This commit is contained in:
Greg Burri 2025-02-11 19:39:13 +01:00
parent ce3821b94e
commit 084be9fb00
16 changed files with 296 additions and 90 deletions

View file

@ -11,8 +11,9 @@ use crate::{
calendar, modal_dialog,
recipe_scheduler::RecipeScheduler,
request,
shopping_list::ShoppingList,
toast::{self, Level},
utils::{get_locale, selector, SelectorExt},
utils::{by_id, get_locale, selector, SelectorExt},
};
pub fn setup_page(is_user_logged: bool) -> Result<(), JsValue> {
@ -26,5 +27,44 @@ pub fn setup_page(is_user_logged: bool) -> Result<(), JsValue> {
},
recipe_scheduler,
);
let shopping_list = ShoppingList::new(!is_user_logged);
spawn_local(async move {
let item_template: Element = selector("#hidden-templates .shopping-item");
let container: Element = by_id("shopping-list");
let date_format =
selector::<Element>("#hidden-templates .calendar-date-format").inner_html();
for item in shopping_list.get_items().await.unwrap() {
let item_element = item_template.deep_clone();
item_element
.selector::<Element>(".item-name")
.set_inner_html(&item.name);
if let Some(quantity_value) = item.quantity_value {
item_element
.selector::<Element>(".item-quantity")
.set_inner_html(&format!("{} {}", quantity_value, item.quantity_unit));
}
// Display associated sheduled recipe information if it exists.
if let (Some(recipe_id), Some(recipe_title), Some(date)) =
(item.recipe_id, item.recipe_title, item.date)
{
let recipe_element = item_element.selector::<Element>(".item-scheduled-recipe a");
recipe_element.set_inner_html(&format!(
"{} @ {}",
recipe_title,
date.format_localized(&date_format, get_locale()),
));
recipe_element
.set_attribute("href", &format!("/recipe/view/{}", recipe_id))
.unwrap();
}
container.append_child(&item_element).unwrap();
}
});
Ok(())
}