Split out shopping into module per component.

This commit is contained in:
Jeremy Wall 2022-03-15 18:01:25 -04:00
parent 652f1c976e
commit d95201f076
8 changed files with 159 additions and 105 deletions

View File

@ -13,12 +13,18 @@
// limitations under the License.
pub mod header;
pub mod recipe;
pub mod recipe_list;
pub mod recipe_selection;
pub mod recipe_selector;
pub mod root;
pub mod shopping;
pub mod shopping_list;
pub mod tabs;
pub use header::*;
pub use recipe::*;
pub use recipe_list::*;
pub use recipe_selection::*;
pub use recipe_selector::*;
pub use root::*;
pub use shopping::*;
pub use shopping_list::*;
pub use tabs::*;

View File

@ -0,0 +1,37 @@
// 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 crate::console_log;
use crate::{components::Recipe, service::AppService};
use sycamore::{context::use_context, prelude::*};
#[component(RecipeList<G>)]
pub fn recipe_list() -> View<G> {
let app_service = use_context::<AppService>();
let menu_list = create_memo(move || app_service.get_menu_list());
view! {
h1 { "Recipe List" }
Indexed(IndexedProps{
iterable: menu_list,
template: |(idx, _count)| {
console_log!("Rendering recipe index: {}", idx);
let idx = Signal::new(idx);
view ! {
Recipe(idx.handle())
hr()
}
}
})
}
}

View File

@ -0,0 +1,44 @@
// 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 crate::console_log;
use crate::service::AppService;
use std::rc::Rc;
use sycamore::{context::use_context, prelude::*};
pub struct RecipeCheckBoxProps {
pub i: usize,
pub title: ReadSignal<String>,
}
#[component(RecipeSelection<G>)]
pub fn recipe_selection(props: RecipeCheckBoxProps) -> View<G> {
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();
let count = Signal::new(format!("{}", app_service.get_recipe_count_by_index(i)));
view! {
div(class="form-group col-md-1") {
input(type="number", class="item-count-sel", min="0", bind:value=count.clone(), name=format!("recipe_id:{}", i), value=id_as_str.clone(), on:change=move |_| {
let mut app_service = app_service.clone();
console_log!("setting recipe id: {} to count: {}", i, *count.get());
app_service.set_recipe_count_by_index(i, count.get().parse().unwrap());
})
label(for=id_cloned_2) { (props.title.get()) }
}
}
}

View File

@ -0,0 +1,64 @@
// 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 crate::console_error;
use crate::{components::recipe_selection::*, service::AppService};
use sycamore::{context::use_context, futures::spawn_local_in_scope, prelude::*};
#[component(RecipeSelector<G>)]
pub fn recipe_selector() -> View<G> {
let app_service = use_context::<AppService>();
let rows = create_memo(cloned!(app_service => move || {
let mut rows = Vec::new();
for row in app_service.get_recipes().get().as_slice().chunks(4) {
rows.push(Signal::new(Vec::from(row)));
}
rows
}));
let clicked = Signal::new(false);
create_effect(cloned!((clicked, app_service) => move || {
clicked.get();
spawn_local_in_scope(cloned!((app_service) => {
let mut app_service = app_service.clone();
async move {
if let Err(e) = app_service.refresh_recipes().await {
console_error!("{}", e);
};
}
}));
}));
view! {
input(type="button", value="Refresh Recipes", on:click=move |_| {
// Poor man's click event signaling.
let toggle = !*clicked.get();
clicked.set(toggle);
})
fieldset(class="recipe_selector no-print container no-left-mgn pad-top") {
(View::new_fragment(
rows.get().iter().cloned().map(|r| {
view ! {
div(class="row") {Indexed(IndexedProps{
iterable: r.handle(),
template: |(i, recipe)| {
view! {
RecipeSelection(RecipeCheckBoxProps{i: i, title: create_memo(move || recipe.get().title.clone())})
}
},
})}
}
}).collect()
))
}
}
}

View File

@ -11,89 +11,12 @@
// 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 crate::{components::Recipe, service::AppService};
use crate::{console_error, console_log};
use crate::service::AppService;
use std::collections::HashMap;
use std::{
collections::{BTreeMap, HashSet},
rc::Rc,
};
use std::collections::{BTreeMap, HashSet};
use recipes::{Ingredient, IngredientKey};
use sycamore::{context::use_context, futures::spawn_local_in_scope, prelude::*};
struct RecipeCheckBoxProps {
i: usize,
title: ReadSignal<String>,
}
#[component(RecipeSelection<G>)]
fn recipe_selection(props: RecipeCheckBoxProps) -> View<G> {
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();
let count = Signal::new(format!("{}", app_service.get_recipe_count_by_index(i)));
view! {
div(class="form-group col-md-1") {
input(type="number", class="item-count-sel", min="0", bind:value=count.clone(), name=format!("recipe_id:{}", i), value=id_as_str.clone(), on:change=move |_| {
let mut app_service = app_service.clone();
console_log!("setting recipe id: {} to count: {}", i, *count.get());
app_service.set_recipe_count_by_index(i, count.get().parse().unwrap());
})
label(for=id_cloned_2) { (props.title.get()) }
}
}
}
#[component(RecipeSelector<G>)]
pub fn recipe_selector() -> View<G> {
let app_service = use_context::<AppService>();
let rows = create_memo(cloned!(app_service => move || {
let mut rows = Vec::new();
for row in app_service.get_recipes().get().as_slice().chunks(4) {
rows.push(Signal::new(Vec::from(row)));
}
rows
}));
let clicked = Signal::new(false);
create_effect(cloned!((clicked, app_service) => move || {
clicked.get();
spawn_local_in_scope(cloned!((app_service) => {
let mut app_service = app_service.clone();
async move {
if let Err(e) = app_service.refresh_recipes().await {
console_error!("{}", e);
};
}
}));
}));
view! {
input(type="button", value="Refresh Recipes", on:click=move |_| {
// Poor man's click event signaling.
let toggle = !*clicked.get();
clicked.set(toggle);
})
fieldset(class="recipe_selector no-print container no-left-mgn pad-top") {
(View::new_fragment(
rows.get().iter().cloned().map(|r| {
view ! {
div(class="row") {Indexed(IndexedProps{
iterable: r.handle(),
template: |(i, recipe)| {
view! {
RecipeSelection(RecipeCheckBoxProps{i: i, title: create_memo(move || recipe.get().title.clone())})
}
},
})}
}
}).collect()
))
}
}
}
use sycamore::{context::use_context, prelude::*};
#[component(ShoppingList<G>)]
pub fn shopping_list() -> View<G> {
@ -162,23 +85,3 @@ pub fn shopping_list() -> View<G> {
(table_view.get().as_ref().clone())
}
}
#[component(RecipeList<G>)]
pub fn recipe_list() -> View<G> {
let app_service = use_context::<AppService>();
let menu_list = create_memo(move || app_service.get_menu_list());
view! {
h1 { "Recipe List" }
Indexed(IndexedProps{
iterable: menu_list,
template: |(idx, _count)| {
console_log!("Rendering recipe index: {}", idx);
let idx = Signal::new(idx);
view ! {
Recipe(idx.handle())
hr()
}
}
})
}
}

View File

@ -11,7 +11,7 @@
// 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 crate::components::{shopping::RecipeList, tabs::*};
use crate::components::{recipe_list::*, tabs::*};
use crate::pages::PageState;
use sycamore::prelude::*;

View File

@ -11,7 +11,7 @@
// 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 crate::components::{shopping::ShoppingList, tabs::*};
use crate::components::{shopping_list::*, tabs::*};
use crate::pages::PageState;
use sycamore::prelude::*;

View File

@ -11,7 +11,7 @@
// 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 crate::components::{shopping::RecipeSelector, tabs::*};
use crate::components::{recipe_selector::*, tabs::*};
use crate::pages::PageState;
use sycamore::prelude::*;