From b9e5ebd705e4c027754815b15f44428cade4e413 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Fri, 6 May 2022 21:24:00 -0400 Subject: [PATCH] Fixed bug in shopping list when missing categories --- recipes/src/lib.rs | 2 +- web/src/service.rs | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/recipes/src/lib.rs b/recipes/src/lib.rs index c9bb3c3..ce36e96 100644 --- a/recipes/src/lib.rs +++ b/recipes/src/lib.rs @@ -176,7 +176,7 @@ impl Step { /// Unique identifier for an Ingredient. Ingredients are identified by name, form, /// and measurement type. (Volume, Count, Weight) -#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Hash)] +#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Hash, Debug)] pub struct IngredientKey(String, Option, String); /// Ingredient in a recipe. The `name` and `form` fields with the measurement type diff --git a/web/src/service.rs b/web/src/service.rs index 072ced5..66918c1 100644 --- a/web/src/service.rs +++ b/web/src/service.rs @@ -25,17 +25,16 @@ use recipes::{parse, Ingredient, IngredientAccumulator, Recipe}; pub struct AppService { recipes: Signal)>>, staples: Signal>, - category_map: Signal>>, + category_map: Signal>, menu_list: Signal>, } impl AppService { pub fn new() -> Self { - let mut category_map = BTreeMap::new(); Self { recipes: Signal::new(Vec::new()), staples: Signal::new(None), - category_map: Signal::new(None), + category_map: Signal::new(BTreeMap::new()), menu_list: Signal::new(BTreeMap::new()), } } @@ -193,20 +192,19 @@ impl AppService { } let mut ingredients = acc.ingredients(); let mut groups = BTreeMap::new(); - self.category_map.get().as_ref().as_ref().map(|cm| { - for (_, (i, recipes)) in ingredients.iter_mut() { - let category = if let Some(cat) = cm.get(&i.name) { - cat.clone() - } else { - "other".to_owned() - }; - i.category = category.clone(); - groups - .entry(category) - .or_insert(vec![]) - .push((i.clone(), recipes.clone())); - } - }); + let cat_map = self.category_map.get().clone(); + for (_, (i, recipes)) in ingredients.iter_mut() { + let category = if let Some(cat) = cat_map.get(&i.name) { + cat.clone() + } else { + "other".to_owned() + }; + i.category = category.clone(); + groups + .entry(category) + .or_insert(vec![]) + .push((i.clone(), recipes.clone())); + } console_debug!("Category map {:?}", self.category_map); // FIXM(jwall): Sort by categories and names. groups @@ -246,6 +244,6 @@ impl AppService { } pub fn set_categories(&mut self, categories: BTreeMap) { - self.category_map.set(Some(categories)); + self.category_map.set(categories); } }