From b84b2722aa4922e4a7fffbbeec9543a1b4185908 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 12 Feb 2022 15:31:35 -0500 Subject: [PATCH] Handle 0 count menu items properly --- web/src/components/recipe.rs | 7 ++++--- web/src/components/shopping.rs | 3 ++- web/src/service.rs | 17 ++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/web/src/components/recipe.rs b/web/src/components/recipe.rs index 253a1e0..0b74a86 100644 --- a/web/src/components/recipe.rs +++ b/web/src/components/recipe.rs @@ -25,9 +25,7 @@ fn steps(steps: ReadSignal>) -> View { iterable: steps, template: |step: recipes::Step| { view! { div { - div(class="instructions") { - (step.instructions) - } + h3 { "Instructions" } ul(class="ingredients") { Indexed(IndexedProps{ iterable: Signal::new(step.ingredients).handle(), @@ -38,6 +36,9 @@ fn steps(steps: ReadSignal>) -> View { }} }) } + div(class="instructions") { + (step.instructions) + } }} } }) diff --git a/web/src/components/shopping.rs b/web/src/components/shopping.rs index 6fd66e9..830dd79 100644 --- a/web/src/components/shopping.rs +++ b/web/src/components/shopping.rs @@ -101,12 +101,13 @@ fn shopping_list() -> View { #[component(RecipeList)] fn recipe_list() -> View { let app_service = use_context::(); - let menu_list = app_service.get_menu_list(); + let menu_list = create_memo(move || app_service.get_menu_list()); view! { h1 { "Recipe List" } Indexed(IndexedProps{ iterable: menu_list, template: |(idx, _count)| { + console_log!("Rendering recipe index: {}", idx); let idx = Signal::new(idx); view ! { Recipe(idx.handle()) diff --git a/web/src/service.rs b/web/src/service.rs index 13b1f88..2e11989 100644 --- a/web/src/service.rs +++ b/web/src/service.rs @@ -93,15 +93,14 @@ impl AppService { self.recipes.clone() } - pub fn get_menu_list(&self) -> ReadSignal> { - let menu_list = self.menu_list.clone(); - create_memo(move || { - menu_list - .get() - .iter() - .map(|(idx, count)| (*idx, *count)) - .collect::>() - }) + pub fn get_menu_list(&self) -> Vec<(usize, usize)> { + self.menu_list + .get() + .iter() + // We exclude recipes in the menu_list with count 0 + .filter(|&(_, count)| *count != 0) + .map(|(idx, count)| (*idx, *count)) + .collect() } pub fn set_recipes(&mut self, mut recipes: Vec<(usize, Recipe)>) {