diff --git a/web/src/components/shopping_list.rs b/web/src/components/shopping_list.rs index c5d343e..0a7e211 100644 --- a/web/src/components/shopping_list.rs +++ b/web/src/components/shopping_list.rs @@ -13,9 +13,8 @@ // limitations under the License. use crate::service::AppService; use std::collections::HashMap; -use std::collections::{BTreeMap, BTreeSet, HashSet}; +use std::collections::{BTreeMap, HashSet}; -use recipes::{Ingredient, IngredientKey}; use sycamore::{context::use_context, prelude::*}; #[component(ShoppingList)] @@ -28,12 +27,16 @@ pub fn shopping_list() -> View { ingredients_map.set(app_service.get_shopping_list()); })); let ingredients = create_memo(cloned!((ingredients_map, filtered_keys) => move || { - ingredients_map - .get() - .iter() - .map(|(k, v)| (k.clone(), v.clone())) - .filter(|(k, _v)| !filtered_keys.get().contains(k)) - .collect::))>>() + let mut ingredients = Vec::new(); + // This has the effect of sorting the ingredients by category + for (_, ingredients_list) in ingredients_map.get().iter() { + for (i, recipes) in ingredients_list.iter() { + if !filtered_keys.get().contains(&i.key()) { + ingredients.push((i.key(), (i.clone(), recipes.clone()))); + } + } + } + ingredients })); let table_view = Signal::new(View::empty()); create_effect( diff --git a/web/src/service.rs b/web/src/service.rs index 9b90be7..dc5cf8b 100644 --- a/web/src/service.rs +++ b/web/src/service.rs @@ -19,7 +19,7 @@ use reqwasm::http; use sycamore::prelude::*; use web_sys::{window, Storage}; -use recipes::{parse, Ingredient, IngredientAccumulator, IngredientKey, Recipe}; +use recipes::{parse, Ingredient, IngredientAccumulator, Recipe}; #[derive(Clone)] pub struct AppService { @@ -179,7 +179,7 @@ impl AppService { self.recipes.get().get(idx).map(|(_, r)| r.clone()) } - pub fn get_shopping_list(&self) -> BTreeMap)> { + pub fn get_shopping_list(&self) -> BTreeMap)>> { let mut acc = IngredientAccumulator::new(); let recipe_counts = self.menu_list.get(); for (idx, count) in recipe_counts.iter() { @@ -191,16 +191,24 @@ impl AppService { acc.accumulate_from(staples); } let mut ingredients = acc.ingredients(); + let mut groups = BTreeMap::new(); self.category_map.get().as_ref().as_ref().map(|cm| { - for (_, (i, _)) in ingredients.iter_mut() { - if let Some(cat) = cm.get(&i.name) { - i.category = cat.clone(); - } + 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())); } }); console_debug!("{:?}", self.category_map); - // TODO(jwall): Sort by categories and names. - ingredients + // FIXM(jwall): Sort by categories and names. + groups } pub fn set_recipe_count_by_index(&mut self, i: usize, count: usize) {