Handle Deletion in the shopping list

This commit is contained in:
Jeremy Wall 2022-02-14 18:46:43 -05:00
parent 256719a42d
commit 98c9a699bd
2 changed files with 12 additions and 6 deletions

View File

@ -167,7 +167,7 @@ impl Step {
/// Unique identifier for an Ingredient. Ingredients are identified by name, form, /// Unique identifier for an Ingredient. Ingredients are identified by name, form,
/// and measurement type. (Volume, Count, Weight) /// and measurement type. (Volume, Count, Weight)
#[derive(PartialEq, PartialOrd, Eq, Ord, Clone)] #[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
pub struct IngredientKey(String, Option<String>, String); pub struct IngredientKey(String, Option<String>, String);
/// Ingredient in a recipe. The `name` and `form` fields with the measurement type /// Ingredient in a recipe. The `name` and `form` fields with the measurement type

View File

@ -14,7 +14,7 @@
use crate::components::Recipe; use crate::components::Recipe;
use crate::console_log; use crate::console_log;
use crate::service::AppService; use crate::service::AppService;
use std::rc::Rc; use std::{collections::HashSet, rc::Rc};
use recipes::{Ingredient, IngredientKey}; use recipes::{Ingredient, IngredientKey};
use sycamore::{context::use_context, prelude::*}; use sycamore::{context::use_context, prelude::*};
@ -66,11 +66,14 @@ pub fn recipe_selector() -> View<G> {
#[component(ShoppingList<G>)] #[component(ShoppingList<G>)]
fn shopping_list() -> View<G> { fn shopping_list() -> View<G> {
let app_service = use_context::<AppService>(); let app_service = use_context::<AppService>();
let filtered_keys = Signal::new(HashSet::new());
let filter_signal = filtered_keys.clone();
let ingredients = create_memo(move || { let ingredients = create_memo(move || {
let ingredients = app_service.get_shopping_list(); let ingredients = app_service.get_shopping_list();
ingredients ingredients
.iter() .iter()
.map(|(k, v)| (k.clone(), v.clone())) .map(|(k, v)| (k.clone(), v.clone()))
.filter(|(k, _v)| !filter_signal.get().contains(k))
.collect::<Vec<(IngredientKey, Ingredient)>>() .collect::<Vec<(IngredientKey, Ingredient)>>()
}); });
@ -84,16 +87,19 @@ fn shopping_list() -> View<G> {
} }
Indexed(IndexedProps{ Indexed(IndexedProps{
iterable: ingredients, iterable: ingredients,
template: |(_k, i)| { template: move |(k, i)| {
let amt = Signal::new(format!("{}", i.amt.normalize())); let amt = Signal::new(format!("{}", i.amt.normalize()));
let name = i.name; let name = i.name;
let form = i.form.map(|form| format!("({})", form)).unwrap_or_default(); let form = i.form.map(|form| format!("({})", form)).unwrap_or_default();
let filtered_keys = filtered_keys.clone();
view! { view! {
tr { tr {
// TODO(jwall): What is the mechanism for deleting ingredients
// from the list?
td { input(bind:value=amt.clone(), type="text") } td { input(bind:value=amt.clone(), type="text") }
td { (name) " " (form) } td {input(type="button", value="X", on:click=move |_| {
let mut keyset = (*filtered_keys.get()).clone();
keyset.insert(k.clone());
filtered_keys.set(keyset);
}) " " (name) " " (form) }
} }
} }
}, },