2022-02-07 16:03:51 -05: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.
|
2022-02-12 12:11:36 -05:00
|
|
|
use crate::components::Recipe;
|
2022-02-07 16:03:51 -05:00
|
|
|
use crate::console_log;
|
|
|
|
use crate::service::AppService;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-02-11 20:32:54 -05:00
|
|
|
use recipes::{Ingredient, IngredientKey};
|
2022-02-07 16:03:51 -05:00
|
|
|
use sycamore::{context::use_context, prelude::*};
|
|
|
|
|
2022-02-10 21:31:25 -05:00
|
|
|
struct RecipeCheckBoxProps {
|
|
|
|
i: usize,
|
2022-02-12 12:11:36 -05:00
|
|
|
title: ReadSignal<String>,
|
2022-02-10 21:31:25 -05:00
|
|
|
}
|
|
|
|
|
2022-02-12 09:04:06 -05:00
|
|
|
#[component(RecipeSelection<G>)]
|
|
|
|
fn recipe_selection(props: RecipeCheckBoxProps) -> View<G> {
|
2022-02-10 21:31:25 -05:00
|
|
|
let app_service = use_context::<AppService>();
|
|
|
|
// This is total hack but it works around the borrow issues with
|
|
|
|
// the `view!` macro.
|
|
|
|
let i = props.i;
|
|
|
|
let id_as_str = Rc::new(format!("{}", i));
|
|
|
|
let id_cloned_2 = id_as_str.clone();
|
2022-02-11 20:30:38 -05:00
|
|
|
let count = Signal::new(format!("{}", app_service.get_recipe_count_by_index(i)));
|
2022-02-10 21:31:25 -05:00
|
|
|
view! {
|
2022-02-12 09:04:06 -05:00
|
|
|
input(type="number", min="0", bind:value=count.clone(), name=format!("recipe_id:{}", i), value=id_as_str.clone(), on:change=move |_| {
|
2022-02-10 21:31:25 -05:00
|
|
|
let mut app_service = app_service.clone();
|
2022-02-11 20:04:52 -05:00
|
|
|
console_log!("setting recipe id: {} to count: {}", i, *count.get());
|
|
|
|
app_service.set_recipe_count_by_index(i, count.get().parse().unwrap());
|
2022-02-10 21:31:25 -05:00
|
|
|
})
|
2022-02-12 12:11:36 -05:00
|
|
|
label(for=id_cloned_2) { (props.title.get()) }
|
2022-02-10 21:31:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:03:51 -05:00
|
|
|
#[component(RecipeSelector<G>)]
|
|
|
|
pub fn recipe_selector() -> View<G> {
|
|
|
|
let app_service = use_context::<AppService>();
|
|
|
|
let titles = create_memo(cloned!(app_service => move || {
|
2022-02-12 12:11:36 -05:00
|
|
|
app_service.get_recipes().get().iter().map(|(i, r)| (*i, r.clone())).collect::<Vec<(usize, Signal<recipes::Recipe>)>>()
|
2022-02-07 16:03:51 -05:00
|
|
|
}));
|
|
|
|
view! {
|
|
|
|
fieldset(class="recipe_selector") {
|
2022-02-12 12:11:36 -05:00
|
|
|
Indexed(IndexedProps{
|
2022-02-07 16:03:51 -05:00
|
|
|
iterable: titles,
|
2022-02-12 12:11:36 -05:00
|
|
|
template: |(i, recipe)| {
|
2022-02-07 16:03:51 -05:00
|
|
|
view! {
|
2022-02-12 12:11:36 -05:00
|
|
|
RecipeSelection(RecipeCheckBoxProps{i: i, title: create_memo(move || recipe.get().title.clone())})
|
2022-02-10 21:31:25 -05:00
|
|
|
}
|
2022-02-07 16:03:51 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:31:25 -05:00
|
|
|
#[component(ShoppingList<G>)]
|
|
|
|
fn shopping_list() -> View<G> {
|
|
|
|
let app_service = use_context::<AppService>();
|
|
|
|
let ingredients = create_memo(move || {
|
2022-02-12 12:11:36 -05:00
|
|
|
let ingredients = app_service.get_shopping_list();
|
2022-02-11 20:04:52 -05:00
|
|
|
ingredients
|
2022-02-10 21:31:25 -05:00
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.clone(), v.clone()))
|
|
|
|
.collect::<Vec<(IngredientKey, Ingredient)>>()
|
|
|
|
});
|
|
|
|
|
2022-02-12 09:04:06 -05:00
|
|
|
// TODO(jwall): Sort by categories and names.
|
2022-02-10 21:31:25 -05:00
|
|
|
view! {
|
2022-02-12 12:11:36 -05:00
|
|
|
h1 { "Shopping List" }
|
2022-02-10 21:31:25 -05:00
|
|
|
table(class="shopping_list") {
|
|
|
|
tr {
|
|
|
|
th { "Quantity" }
|
|
|
|
th { "Ingredient" }
|
|
|
|
}
|
|
|
|
Indexed(IndexedProps{
|
|
|
|
iterable: ingredients,
|
|
|
|
template: |(_k, i)| {
|
2022-02-12 09:04:06 -05:00
|
|
|
let amt = Signal::new(format!("{}", i.amt.normalize()));
|
2022-02-14 16:06:05 -05:00
|
|
|
let name = i.name;
|
|
|
|
let form = i.form.map(|form| format!("({})", form)).unwrap_or_default();
|
2022-02-10 21:31:25 -05:00
|
|
|
view! {
|
|
|
|
tr {
|
2022-02-14 15:54:26 -05:00
|
|
|
// TODO(jwall): What is the mechanism for deleting ingredients
|
|
|
|
// from the list?
|
2022-02-12 09:04:06 -05:00
|
|
|
td { input(bind:value=amt.clone(), type="text") }
|
2022-02-14 16:06:05 -05:00
|
|
|
td { (name) " " (form) }
|
2022-02-10 21:31:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 12:11:36 -05:00
|
|
|
#[component(RecipeList<G>)]
|
|
|
|
fn recipe_list() -> View<G> {
|
|
|
|
let app_service = use_context::<AppService>();
|
2022-02-12 15:31:35 -05:00
|
|
|
let menu_list = create_memo(move || app_service.get_menu_list());
|
2022-02-12 12:11:36 -05:00
|
|
|
view! {
|
|
|
|
h1 { "Recipe List" }
|
|
|
|
Indexed(IndexedProps{
|
|
|
|
iterable: menu_list,
|
|
|
|
template: |(idx, _count)| {
|
2022-02-12 15:31:35 -05:00
|
|
|
console_log!("Rendering recipe index: {}", idx);
|
2022-02-12 12:11:36 -05:00
|
|
|
let idx = Signal::new(idx);
|
|
|
|
view ! {
|
|
|
|
Recipe(idx.handle())
|
|
|
|
hr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component(MealPlan<G>)]
|
|
|
|
pub fn meal_plan() -> View<G> {
|
2022-02-07 16:03:51 -05:00
|
|
|
view! {
|
|
|
|
h1 {
|
|
|
|
"Select your recipes"
|
|
|
|
}
|
|
|
|
RecipeSelector()
|
2022-02-10 21:31:25 -05:00
|
|
|
ShoppingList()
|
2022-02-12 12:11:36 -05:00
|
|
|
RecipeList()
|
2022-02-07 16:03:51 -05:00
|
|
|
}
|
|
|
|
}
|