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,
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<Vec<recipes::Step>>) -> View<G> {
}}
})
}
div(class="instructions") {
(step.instructions)
}
}}
}
})

View File

@ -101,12 +101,13 @@ fn shopping_list() -> View<G> {
#[component(RecipeList<G>)]
fn recipe_list() -> View<G> {
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! {
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())

View File

@ -93,15 +93,14 @@ impl AppService {
self.recipes.clone()
}
pub fn get_menu_list(&self) -> ReadSignal<Vec<(usize, usize)>> {
let menu_list = self.menu_list.clone();
create_memo(move || {
menu_list
.get()
.iter()
.map(|(idx, count)| (*idx, *count))
.collect::<Vec<(usize, usize)>>()
})
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)>) {