use state handler in recipe selection

This commit is contained in:
Jeremy Wall 2022-12-29 11:37:10 -06:00
parent 0b7ff32d42
commit a1fa17da68

View File

@ -35,28 +35,18 @@ pub fn RecipeSelection<'ctx, G: Html>(
props: RecipeCheckBoxProps<'ctx>, props: RecipeCheckBoxProps<'ctx>,
) -> View<G> { ) -> View<G> {
let RecipeCheckBoxProps { i, title, sh } = props; 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 = Rc::new(i);
let id_clone = id.clone();
let count = create_signal( let count = create_signal(
cx, cx,
format!( sh.get_value(
"{}", |state| match state.get_untracked().recipe_counts.get(id_clone.as_ref()) {
state Some(count) => format!("{}", count),
.get_recipe_count_by_index(id.as_ref()) None => "0".to_owned(),
.unwrap_or_else(|| state.set_recipe_count_by_index(id.as_ref(), 0)) },
), ),
); );
create_effect(cx, { let id_clone = id.clone();
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 title = title.get().clone(); let title = title.get().clone();
let for_id = id.clone(); let for_id = id.clone();
let href = format!("/ui/recipe/view/{}", id); let href = format!("/ui/recipe/view/{}", id);
@ -64,7 +54,7 @@ pub fn RecipeSelection<'ctx, G: Html>(
view! {cx, view! {cx,
div() { div() {
label(for=for_id) { a(href=href) { (*title) } } 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"); 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"))); sh.dispatch(cx, Message::UpdateRecipeCount(id.as_ref().clone(), count.get().parse().expect("Count is not a valid usize")));
}) })