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

@ -222,11 +222,7 @@ fn display_month(
let scheduled_recipes_element: Element =
selector(&format!("#day-grid-{}{} .scheduled-recipes", i, j));
let recipe_element = recipe_template
.clone_node_with_deep(true)
.unwrap()
.dyn_into::<Element>()
.unwrap();
let recipe_element = recipe_template.deep_clone();
recipe_element.set_id(&id);
scheduled_recipes_element

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(())
}

View file

@ -15,6 +15,7 @@ mod recipe_edit;
mod recipe_scheduler;
mod recipe_view;
mod request;
mod shopping_list;
mod toast;
mod utils;

View file

@ -0,0 +1,35 @@
use chrono::{Datelike, Days, Months, NaiveDate};
use common::ron_api;
use gloo::storage::{LocalStorage, Storage};
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{calendar, request};
#[derive(Error, Debug)]
pub enum Error {
#[error("Request error: {0}")]
Request(#[from] request::Error),
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Copy)]
pub struct ShoppingList {
is_local: bool,
}
impl ShoppingList {
pub fn new(is_local: bool) -> Self {
Self { is_local }
}
pub async fn get_items(&self) -> Result<Vec<ron_api::ShoppingListItem>> {
if self.is_local {
Ok(vec![]) // TODO
} else {
Ok(request::get("shopping_list/get_list", ()).await?)
}
}
}

View file

@ -13,6 +13,8 @@ pub trait SelectorExt {
fn selector_all<T>(&self, selectors: &str) -> Vec<T>
where
T: JsCast;
fn deep_clone(&self) -> Self;
}
impl SelectorExt for Element {
@ -38,6 +40,13 @@ impl SelectorExt for Element {
.map(|e| e.unwrap().dyn_into::<T>().unwrap())
.collect()
}
fn deep_clone(&self) -> Self {
self.clone_node_with_deep(true)
.unwrap()
.dyn_into::<Element>()
.unwrap()
}
}
pub fn selector<T>(selectors: &str) -> T