From a1fa17da68feebd257b4ecca4681f65a76d2e67a Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 29 Dec 2022 11:37:10 -0600 Subject: [PATCH] use state handler in recipe selection --- web/src/components/recipe_selection.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/web/src/components/recipe_selection.rs b/web/src/components/recipe_selection.rs index 8375027..e8ff8a6 100644 --- a/web/src/components/recipe_selection.rs +++ b/web/src/components/recipe_selection.rs @@ -35,28 +35,18 @@ pub fn RecipeSelection<'ctx, G: Html>( props: RecipeCheckBoxProps<'ctx>, ) -> View { let RecipeCheckBoxProps { i, title, sh } = props; - let state = app_state::State::get_from_context(cx); - // This is total hack but it works around the borrow issues with - // the `view!` macro. let id = Rc::new(i); + let id_clone = id.clone(); let count = create_signal( cx, - format!( - "{}", - state - .get_recipe_count_by_index(id.as_ref()) - .unwrap_or_else(|| state.set_recipe_count_by_index(id.as_ref(), 0)) + sh.get_value( + |state| match state.get_untracked().recipe_counts.get(id_clone.as_ref()) { + Some(count) => format!("{}", count), + None => "0".to_owned(), + }, ), ); - create_effect(cx, { - let id = id.clone(); - let state = app_state::State::get_from_context(cx); - move || { - if let Some(usize_count) = state.get_recipe_count_by_index(id.as_ref()) { - count.set(format!("{}", *usize_count.get())); - } - } - }); + let id_clone = id.clone(); let title = title.get().clone(); let for_id = id.clone(); let href = format!("/ui/recipe/view/{}", id); @@ -64,7 +54,7 @@ pub fn RecipeSelection<'ctx, G: Html>( view! {cx, div() { label(for=for_id) { a(href=href) { (*title) } } - input(type="number", class="item-count-sel", min="0", bind:value=count, name=name, on:change=move |_| { + input(type="number", class="item-count-sel", min="0", value=count, name=name, on:change=move |_| { debug!(idx=%id, count=%(*count.get()), "setting recipe count"); sh.dispatch(cx, Message::UpdateRecipeCount(id.as_ref().clone(), count.get().parse().expect("Count is not a valid usize"))); })