Handle 0 count menu items properly

This commit is contained in:
Jeremy Wall 2022-02-12 15:31:35 -05:00
parent 5296ed10c1
commit b84b2722aa
3 changed files with 14 additions and 13 deletions

View File

@ -25,9 +25,7 @@ fn steps(steps: ReadSignal<Vec<recipes::Step>>) -> View<G> {
iterable: steps, iterable: steps,
template: |step: recipes::Step| { view! { template: |step: recipes::Step| { view! {
div { div {
div(class="instructions") { h3 { "Instructions" }
(step.instructions)
}
ul(class="ingredients") { ul(class="ingredients") {
Indexed(IndexedProps{ Indexed(IndexedProps{
iterable: Signal::new(step.ingredients).handle(), iterable: Signal::new(step.ingredients).handle(),
@ -38,6 +36,9 @@ fn steps(steps: ReadSignal<Vec<recipes::Step>>) -> View<G> {
}} }}
}) })
} }
div(class="instructions") {
(step.instructions)
}
}} }}
} }
}) })

View File

@ -101,12 +101,13 @@ fn shopping_list() -> View<G> {
#[component(RecipeList<G>)] #[component(RecipeList<G>)]
fn recipe_list() -> View<G> { fn recipe_list() -> View<G> {
let app_service = use_context::<AppService>(); let app_service = use_context::<AppService>();
let menu_list = app_service.get_menu_list(); let menu_list = create_memo(move || app_service.get_menu_list());
view! { view! {
h1 { "Recipe List" } h1 { "Recipe List" }
Indexed(IndexedProps{ Indexed(IndexedProps{
iterable: menu_list, iterable: menu_list,
template: |(idx, _count)| { template: |(idx, _count)| {
console_log!("Rendering recipe index: {}", idx);
let idx = Signal::new(idx); let idx = Signal::new(idx);
view ! { view ! {
Recipe(idx.handle()) Recipe(idx.handle())

View File

@ -93,15 +93,14 @@ impl AppService {
self.recipes.clone() self.recipes.clone()
} }
pub fn get_menu_list(&self) -> ReadSignal<Vec<(usize, usize)>> { pub fn get_menu_list(&self) -> Vec<(usize, usize)> {
let menu_list = self.menu_list.clone(); self.menu_list
create_memo(move || { .get()
menu_list .iter()
.get() // We exclude recipes in the menu_list with count 0
.iter() .filter(|&(_, count)| *count != 0)
.map(|(idx, count)| (*idx, *count)) .map(|(idx, count)| (*idx, *count))
.collect::<Vec<(usize, usize)>>() .collect()
})
} }
pub fn set_recipes(&mut self, mut recipes: Vec<(usize, Recipe)>) { pub fn set_recipes(&mut self, mut recipes: Vec<(usize, Recipe)>) {