kitchen/web/src/components/recipe_plan.rs

123 lines
4.0 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
// 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.
2022-08-15 19:37:08 -04:00
use recipes::Recipe;
2022-12-28 12:59:11 -06:00
use sycamore::prelude::*;
use tracing::{debug, instrument};
2022-12-28 11:54:24 -06:00
use crate::app_state::{Message, StateHandler};
use crate::components::recipe_selection::*;
#[derive(Props)]
pub struct CategoryGroupProps<'ctx> {
sh: StateHandler<'ctx>,
category: String,
recipes: Vec<(String, Recipe)>,
row_size: usize,
}
2022-10-10 20:49:18 -04:00
#[allow(non_snake_case)]
pub fn CategoryGroup<'ctx, G: Html>(
cx: Scope<'ctx>,
CategoryGroupProps {
sh,
category,
recipes,
row_size,
}: CategoryGroupProps<'ctx>,
) -> View<G> {
2023-02-04 12:06:16 -05:00
let rows = create_signal(cx, {
let mut rows = Vec::new();
for row in recipes
.iter()
.map(|(id, r)| create_signal(cx, (id.clone(), r.clone())))
.collect::<Vec<&Signal<(String, Recipe)>>>()
.chunks(row_size)
2022-10-12 08:45:31 -04:00
{
rows.push(create_signal(cx, Vec::from(row)));
}
rows
});
view! {cx,
h2 { (category) }
2022-08-24 18:32:43 -04:00
table(class="recipe_selector no-print") {
(View::new_fragment(
rows.get().iter().cloned().map(|r| {
view ! {cx,
tr { Keyed(
iterable=r,
2022-12-28 19:33:19 -06:00
view=move |cx, sig| {
let title = create_memo(cx, move || sig.get().1.title.clone());
view! {cx,
2022-12-28 19:33:19 -06:00
td { RecipeSelection(i=sig.get().0.to_owned(), title=title, sh=sh) }
}
},
key=|sig| sig.get().0.to_owned(),
)}
}
}).collect()
))
}
}
}
#[allow(non_snake_case)]
#[instrument(skip_all)]
pub fn RecipePlan<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View<G> {
let recipe_category_groups = sh.get_selector(cx, |state| {
state
.get()
.recipe_categories
.iter()
.fold(BTreeMap::new(), |mut map, (r, cat)| {
debug!(?cat, recipe_id=?r, "Accumulating recipe into category");
map.entry(cat.clone()).or_insert(Vec::new()).push((
r.clone(),
state
.get()
.recipes
.get(r)
.expect(&format!("Failed to find recipe {}", r))
.clone(),
));
map
})
.iter()
.map(|(cat, rs)| (cat.clone(), rs.clone()))
.collect::<Vec<(String, Vec<(String, Recipe)>)>>()
});
view! {cx,
Keyed(
iterable=recipe_category_groups,
view=move |cx, (cat, recipes)| {
view! {cx,
CategoryGroup(sh=sh, category=cat, recipes=recipes, row_size=4)
}
},
key=|(ref cat, _)| cat.clone(),
)
2023-01-07 16:21:06 -05:00
span(role="button", on:click=move |_| {
sh.dispatch(cx, Message::LoadState(None));
2023-01-07 16:21:06 -05:00
}) { "Reset" } " "
span(role="button", on:click=move |_| {
2022-12-28 19:33:19 -06:00
sh.dispatch(cx, Message::ResetRecipeCounts);
2023-01-07 16:21:06 -05:00
}) { "Clear All" } " "
span(role="button", on:click=move |_| {
2022-03-15 18:11:15 -04:00
// Poor man's click event signaling.
sh.dispatch(cx, Message::SaveState(None));
2023-01-07 16:21:06 -05:00
}) { "Save Plan" } " "
}
}