kitchen/web/src/components/recipe_selection.rs

75 lines
2.8 KiB
Rust
Raw Normal View History

// Copyright 2022 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::rc::Rc;
use sycamore::prelude::*;
use tracing::{debug, instrument};
2022-12-29 11:58:37 -06:00
use crate::app_state::{Message, StateHandler};
2023-01-11 15:38:32 -05:00
use crate::components::NumberField;
2022-11-01 20:38:14 -04:00
#[derive(Props)]
pub struct RecipeCheckBoxProps<'ctx> {
2022-08-15 19:37:08 -04:00
pub i: String,
pub title: &'ctx ReadSignal<String>,
2022-12-28 19:33:19 -06:00
pub sh: StateHandler<'ctx>,
}
#[instrument(skip(props, cx), fields(
2022-12-28 19:33:19 -06:00
id=%props.i,
title=%props.title.get()
))]
#[component]
2022-12-28 19:33:19 -06:00
pub fn RecipeSelection<'ctx, G: Html>(
cx: Scope<'ctx>,
props: RecipeCheckBoxProps<'ctx>,
) -> View<G> {
let RecipeCheckBoxProps { i, title, sh } = props;
let id = Rc::new(i);
2023-01-11 15:38:32 -05:00
let id_for_count = id.clone();
2023-01-12 17:32:18 -05:00
// NOTE(jwall): The below get's a little tricky. We need a separate signal to bind for the
// this recipes count. But we also want it to automatically update if the app_state
// recipe count updates. We need to avoid signal update cycles so we have to do this
// in two steps. We have a read signal that represents changes in the value of the
// app_states count. We have a Signal that represents the value of this components count.
// If the app_states count changes and is also different from the components count then we
// and only then do we set the components count to the app states count.
2023-01-11 15:38:32 -05:00
let current_count = sh.get_selector(cx, move |state| {
*state
.get()
.recipe_counts
.get(id_for_count.as_ref())
2023-01-18 19:29:44 -05:00
.unwrap_or(&0)
2023-01-11 15:38:32 -05:00
});
2023-02-06 16:02:14 -05:00
let count = create_signal(cx, *current_count.get_untracked() as f64);
2023-01-11 15:38:32 -05:00
create_effect(cx, || {
2023-02-06 16:02:14 -05:00
let updated_count = *current_count.get() as f64;
if updated_count != *count.get_untracked() {
2023-01-11 15:38:32 -05:00
count.set(updated_count);
}
});
2023-01-12 17:32:18 -05:00
2022-12-28 19:33:19 -06:00
let title = title.get().clone();
let href = format!("/ui/recipe/view/{}", id);
2022-08-15 19:37:08 -04:00
let name = format!("recipe_id:{}", id);
2023-01-11 15:38:32 -05:00
let for_id = name.clone();
view! {cx,
label(for=for_id, class="flex-item-grow") { a(href=href) { (*title) } }
NumberField(name=name, class="flex-item-shrink".to_string(), counter=count, min=0.0, on_change=Some(move |_| {
debug!(idx=%id, count=%(*count.get_untracked()), "setting recipe count");
sh.dispatch(cx, Message::UpdateRecipeCount(id.as_ref().clone(), *count.get_untracked() as usize));
}))
}
}