2022-03-15 18:01:25 -04:00
|
|
|
// 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;
|
|
|
|
|
2022-08-12 21:41:19 -04:00
|
|
|
use sycamore::prelude::*;
|
2022-07-18 18:50:11 -04:00
|
|
|
use tracing::{debug, instrument};
|
|
|
|
|
2022-12-29 11:58:37 -06:00
|
|
|
use crate::app_state::{Message, StateHandler};
|
2022-03-15 18:01:25 -04:00
|
|
|
|
2022-11-01 20:38:14 -04:00
|
|
|
#[derive(Props)]
|
2022-09-25 17:04:46 -04:00
|
|
|
pub struct RecipeCheckBoxProps<'ctx> {
|
2022-08-15 19:37:08 -04:00
|
|
|
pub i: String,
|
2022-09-25 17:04:46 -04:00
|
|
|
pub title: &'ctx ReadSignal<String>,
|
2022-12-28 19:33:19 -06:00
|
|
|
pub sh: StateHandler<'ctx>,
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[instrument(skip(props, cx), fields(
|
2022-12-28 19:33:19 -06:00
|
|
|
id=%props.i,
|
2022-07-18 18:50:11 -04:00
|
|
|
title=%props.title.get()
|
|
|
|
))]
|
2022-09-25 17:04:46 -04:00
|
|
|
#[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();
|
2022-09-25 17:04:46 -04:00
|
|
|
let count = create_signal(
|
|
|
|
cx,
|
2022-12-29 11:37:10 -06:00
|
|
|
sh.get_value(
|
|
|
|
|state| match state.get_untracked().recipe_counts.get(id_clone.as_ref()) {
|
|
|
|
Some(count) => format!("{}", count),
|
|
|
|
None => "0".to_owned(),
|
|
|
|
},
|
2022-09-25 17:04:46 -04:00
|
|
|
),
|
|
|
|
);
|
2022-12-28 19:33:19 -06:00
|
|
|
let title = title.get().clone();
|
2022-08-15 19:37:08 -04:00
|
|
|
let for_id = id.clone();
|
2022-11-07 16:47:46 -05:00
|
|
|
let href = format!("/ui/recipe/view/{}", id);
|
2022-08-15 19:37:08 -04:00
|
|
|
let name = format!("recipe_id:{}", id);
|
2022-09-25 17:04:46 -04:00
|
|
|
view! {cx,
|
2022-03-23 20:26:58 -04:00
|
|
|
div() {
|
2022-09-25 17:04:46 -04:00
|
|
|
label(for=for_id) { a(href=href) { (*title) } }
|
2022-12-29 12:42:05 -06:00
|
|
|
input(type="number", class="item-count-sel", min="0", bind:value=count, name=name, on:change=move |_| {
|
2022-08-15 19:37:08 -04:00
|
|
|
debug!(idx=%id, count=%(*count.get()), "setting recipe count");
|
2022-12-28 19:33:19 -06:00
|
|
|
sh.dispatch(cx, Message::UpdateRecipeCount(id.as_ref().clone(), count.get().parse().expect("Count is not a valid usize")));
|
2022-09-25 17:04:46 -04:00
|
|
|
})
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|