diff --git a/recipes/src/lib.rs b/recipes/src/lib.rs index 4d5cdcf..f4f2337 100644 --- a/recipes/src/lib.rs +++ b/recipes/src/lib.rs @@ -135,12 +135,17 @@ impl IngredientAccumulator { } } - pub fn accumulate_from(&mut self, r: &Recipe) { - for i in r.steps.iter().map(|s| s.ingredients.iter()).flatten() { + pub fn accumulate_ingredients<'a, I, S>(&'a mut self, recipe_title: S, ingredients: I) + where + I: Iterator, + S: Into, + { + let recipe_title = recipe_title.into(); + for i in ingredients { let key = i.key(); if !self.inner.contains_key(&key) { let mut set = BTreeSet::new(); - set.insert(r.title.clone()); + set.insert(recipe_title.clone()); self.inner.insert(key, (i.clone(), set)); } else { let amt = match (self.inner[&key].0.amt, i.amt) { @@ -151,12 +156,19 @@ impl IngredientAccumulator { }; self.inner.get_mut(&key).map(|(i, set)| { i.amt = amt; - set.insert(r.title.clone()); + set.insert(recipe_title.clone()); }); } } } + pub fn accumulate_from(&mut self, r: &Recipe) { + self.accumulate_ingredients( + &r.title, + r.steps.iter().map(|s| s.ingredients.iter()).flatten(), + ); + } + pub fn ingredients(self) -> BTreeMap)> { self.inner } diff --git a/web/src/app_state.rs b/web/src/app_state.rs index 8a3a7dc..2162e6a 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -17,7 +17,7 @@ use std::{ }; use client_api::UserData; -use recipes::{parse, IngredientKey, Recipe, RecipeEntry}; +use recipes::{parse, Ingredient, IngredientKey, Recipe, RecipeEntry}; use sycamore::futures::spawn_local_scoped; use sycamore::prelude::*; use sycamore_state::{Handler, MessageMapper}; @@ -30,7 +30,7 @@ use crate::api::{HttpStore, LocalStore}; pub struct AppState { pub recipe_counts: BTreeMap, pub extras: Vec<(String, String)>, - pub staples: Option, + pub staples: BTreeSet, pub recipes: BTreeMap, pub category_map: BTreeMap, pub filtered_ingredients: BTreeSet, @@ -43,7 +43,7 @@ impl AppState { Self { recipe_counts: BTreeMap::new(), extras: Vec::new(), - staples: None, + staples: BTreeSet::new(), recipes: BTreeMap::new(), category_map: BTreeMap::new(), filtered_ingredients: BTreeSet::new(), @@ -160,9 +160,9 @@ impl StateMachine { let recipe_entries = &store.fetch_recipes().await?; let (staples, recipes) = filter_recipes(&recipe_entries)?; if let Some(recipes) = recipes { - state.staples = staples; state.recipes = recipes; }; + state.staples = BTreeSet::new(); if let Some(recipe_entries) = recipe_entries { local_store.set_all_recipes(recipe_entries); } diff --git a/web/src/components/categories.rs b/web/src/components/categories.rs index 1905e14..282f1db 100644 --- a/web/src/components/categories.rs +++ b/web/src/components/categories.rs @@ -103,14 +103,12 @@ pub fn Categories<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> Vie .insert(recipe_id.clone()); } } - if let Some(staples) = &state.staples { - for (_, i) in staples.get_ingredients().iter() { - let ingredient_name = i.name.clone(); - ingredients - .entry(ingredient_name) - .or_insert(BTreeSet::new()) - .insert("Staples".to_owned()); - } + for i in state.staples.iter() { + let ingredient_name = i.name.clone(); + ingredients + .entry(ingredient_name) + .or_insert(BTreeSet::new()) + .insert("Staples".to_owned()); } ingredients }); @@ -124,10 +122,8 @@ pub fn Categories<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> Vie ingredients.insert(i.name.clone()); } } - if let Some(staples) = &state.staples { - for (_, i) in staples.get_ingredients().iter() { - ingredients.insert(i.name.clone()); - } + for i in state.staples.iter() { + ingredients.insert(i.name.clone()); } let mut mapping_list = Vec::new(); for i in ingredients.iter() { diff --git a/web/src/components/shopping_list.rs b/web/src/components/shopping_list.rs index 5e01a45..4e8a2a8 100644 --- a/web/src/components/shopping_list.rs +++ b/web/src/components/shopping_list.rs @@ -42,9 +42,7 @@ fn make_ingredients_rows<'ctx, G: Html>( } } if *show_staples.get() { - if let Some(staples) = &state.staples { - acc.accumulate_from(staples); - } + acc.accumulate_ingredients("Staples", state.staples.iter()); } let mut ingredients = acc .ingredients()