diff --git a/recipes/src/unit.rs b/recipes/src/unit.rs index 5cecf0a..5fa6066 100644 --- a/recipes/src/unit.rs +++ b/recipes/src/unit.rs @@ -137,33 +137,33 @@ impl VolumeMeasure { Ltr(self.get_ml() / LTR) } - pub fn normalize(self) -> Self { + pub fn normalize(&self) -> Self { let ml = self.get_ml(); if (ml / GAL) >= ONE { - return self.into_gal(); + return self.clone().into_gal(); } if (ml / LTR) >= ONE { - return self.into_ltr(); + return self.clone().into_ltr(); } if (ml / QRT) >= ONE { - return self.into_qrt(); + return self.clone().into_qrt(); } if (ml / PINT) >= ONE { - return self.into_pint(); + return self.clone().into_pint(); } if (ml / CUP) >= ONE { - return self.into_cup(); + return self.clone().into_cup(); } if (ml / FLOZ) >= ONE { - return self.into_floz(); + return self.clone().into_floz(); } if (ml / TBSP) >= ONE { - return self.into_tbsp(); + return self.clone().into_tbsp(); } if (ml / TSP) >= ONE { - return self.into_tsp(); + return self.clone().into_tsp(); } - return self.into_ml(); + return self.clone().into_ml(); } } @@ -249,18 +249,18 @@ impl WeightMeasure { Self::Oz(self.get_grams() / OZ) } - pub fn normalize(self) -> Self { + pub fn normalize(&self) -> Self { let grams = self.get_grams(); if (grams / KG) >= ONE { - return self.into_kilo(); + return self.clone().into_kilo(); } if (grams / LB) >= ONE { - return self.into_pound(); + return self.clone().into_pound(); } if (grams / OZ) >= ONE { - return self.into_oz(); + return self.clone().into_oz(); } - return self.into_gram(); + return self.clone().into_gram(); } } @@ -393,6 +393,14 @@ impl Measure { Weight(wm) => wm.plural(), } } + + pub fn normalize(&self) -> Self { + match self { + Volume(vm) => Volume(vm.normalize()), + Count(qty) => Count(qty.clone()), + Weight(wm) => Weight(wm.normalize()), + } + } } impl Display for Measure { diff --git a/web/src/components/shopping.rs b/web/src/components/shopping.rs index b47a3a4..4811aad 100644 --- a/web/src/components/shopping.rs +++ b/web/src/components/shopping.rs @@ -23,8 +23,8 @@ struct RecipeCheckBoxProps { title: String, } -#[component(RecipeCheckBox)] -fn recipe_check_box(props: RecipeCheckBoxProps) -> View { +#[component(RecipeSelection)] +fn recipe_selection(props: RecipeCheckBoxProps) -> View { let app_service = use_context::(); // This is total hack but it works around the borrow issues with // the `view!` macro. @@ -33,7 +33,7 @@ fn recipe_check_box(props: RecipeCheckBoxProps) -> View { let id_cloned_2 = id_as_str.clone(); let count = Signal::new(format!("{}", app_service.get_recipe_count_by_index(i))); view! { - input(type="number", min="0", bind:value=count.clone(), name="recipe_id", value=id_as_str.clone(), on:change=move |_| { + input(type="number", min="0", bind:value=count.clone(), name=format!("recipe_id:{}", i), value=id_as_str.clone(), on:change=move |_| { let mut app_service = app_service.clone(); console_log!("setting recipe id: {} to count: {}", i, *count.get()); app_service.set_recipe_count_by_index(i, count.get().parse().unwrap()); @@ -54,7 +54,7 @@ pub fn recipe_selector() -> View { iterable: titles, template: |(i, title)| { view! { - RecipeCheckBox(RecipeCheckBoxProps{i: i, title: title}) + RecipeSelection(RecipeCheckBoxProps{i: i, title: title}) } }, key: |(i, title)| (*i, title.clone()), @@ -74,6 +74,7 @@ fn shopping_list() -> View { .collect::>() }); + // TODO(jwall): Sort by categories and names. view! { table(class="shopping_list") { tr { @@ -83,9 +84,10 @@ fn shopping_list() -> View { Indexed(IndexedProps{ iterable: ingredients, template: |(_k, i)| { + let amt = Signal::new(format!("{}", i.amt.normalize())); view! { tr { - td { (i.amt) } + td { input(bind:value=amt.clone(), type="text") } td { (i.name) } } } diff --git a/web/src/service.rs b/web/src/service.rs index d5b71ea..0986e8f 100644 --- a/web/src/service.rs +++ b/web/src/service.rs @@ -54,7 +54,7 @@ impl AppService { Ok(r) => r, Err(e) => { console_error!("Error parsing recipe {}", e); - break; + continue; } }; console_debug!("We parsed a recipe {}", recipe.title);