kitchen/web/src/components/recipe_selection.rs

70 lines
2.3 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);
2022-12-29 11:37:10 -06:00
let id_clone = id.clone();
2023-01-11 15:38:32 -05:00
let id_for_count = id.clone();
let current_count = sh.get_selector(cx, move |state| {
*state
.get()
.recipe_counts
.get(id_for_count.as_ref())
.unwrap()
});
let count = create_signal(cx, format!("{}", *current_count.get_untracked()));
create_effect(cx, || {
let updated_count = format!("{}", current_count.get());
if updated_count != count.get_untracked().as_ref() {
count.set(updated_count);
}
});
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,
div() {
label(for=for_id) { a(href=href) { (*title) } }
2023-01-11 15:38:32 -05:00
NumberField(name=name, counter=count, min=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().parse().expect("Count is not a valid usize")));
}))
}
}
}