diff --git a/web/src/app_state.rs b/web/src/app_state.rs new file mode 100644 index 0000000..d8ff5a9 --- /dev/null +++ b/web/src/app_state.rs @@ -0,0 +1,23 @@ +// 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. + + +#[derive(Debug)] +pub enum AppRoutes { + Plan, + Recipe { index: usize }, + NotFound, + Inventory, + Cook, +} \ No newline at end of file diff --git a/web/src/components/shopping.rs b/web/src/components/shopping.rs index d29887d..f43c2bc 100644 --- a/web/src/components/shopping.rs +++ b/web/src/components/shopping.rs @@ -11,8 +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::Recipe; -use crate::service::AppService; +use crate::{app_state::*, components::Recipe, service::AppService}; use crate::{console_error, console_log}; use std::collections::HashMap; use std::{ @@ -50,7 +49,7 @@ fn recipe_selection(props: RecipeCheckBoxProps) -> View { } #[component(RecipeSelector)] -pub fn recipe_selector() -> View { +pub fn recipe_selector(page_state: crate::pages::PageState) -> View { let app_service = use_context::(); let rows = create_memo(cloned!(app_service => move || { let mut rows = Vec::new(); @@ -72,6 +71,11 @@ pub fn recipe_selector() -> View { })); })); 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| { @@ -88,16 +92,17 @@ pub fn recipe_selector() -> View { }).collect() )) } - input(type="button", value="Refresh Recipes", on:click=move |_| { - // Poor man's click event signaling. - let toggle = !*clicked.get(); - clicked.set(toggle); - }) + input(type="button", value="Inventory", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Inventory); + })) + input(type="button", value="Cook", class="no-print", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Cook); + })) } } #[component(ShoppingList)] -fn shopping_list() -> View { +pub fn shopping_list(page_state: crate::pages::PageState) -> View { let app_service = use_context::(); let filtered_keys = Signal::new(HashSet::new()); let ingredients_map = Signal::new(BTreeMap::new()); @@ -161,11 +166,17 @@ fn shopping_list() -> View { modified_amts.set(HashMap::new()); })) (table_view.get().as_ref().clone()) + input(type="button", value="Plan", class="no-print", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Plan); + })) + input(type="button", value="Cook", class="no-print", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Cook); + })) } } #[component(RecipeList)] -fn recipe_list() -> View { +pub fn recipe_list(page_state: crate::pages::PageState) -> View { let app_service = use_context::(); let menu_list = create_memo(move || app_service.get_menu_list()); view! { @@ -181,6 +192,12 @@ fn recipe_list() -> View { } } }) + input(type="button", value="Inventory", class="no-print", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Inventory); + })) + input(type="button", value="Cook", class="no-print", on:click=cloned!((page_state) => move |_| { + page_state.route.set(AppRoutes::Cook); + })) } } @@ -190,8 +207,8 @@ pub fn meal_plan() -> View { h1 { "Select your recipes" } - RecipeSelector() - ShoppingList() - RecipeList() + //RecipeSelector() + //ShoppingList() + //RecipeList() } } diff --git a/web/src/main.rs b/web/src/main.rs index 91f4b36..3ec7dec 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -12,9 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. mod components; +mod pages; mod service; mod typings; mod web; +mod app_state; use sycamore::prelude::*; diff --git a/web/src/pages/cook.rs b/web/src/pages/cook.rs new file mode 100644 index 0000000..ef177f0 --- /dev/null +++ b/web/src/pages/cook.rs @@ -0,0 +1,28 @@ +// 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::components::shopping::RecipeList; +use crate::pages::PageState; + +use sycamore::prelude::*; + +pub struct CookPageProps { + pub page_state: PageState, +} + +#[component(CookPage)] +pub fn cook_page(props: CookPageProps) -> View { + view! { + RecipeList(props.page_state) + } +} diff --git a/web/src/pages/inventory.rs b/web/src/pages/inventory.rs new file mode 100644 index 0000000..60bc857 --- /dev/null +++ b/web/src/pages/inventory.rs @@ -0,0 +1,28 @@ +// 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::components::shopping::ShoppingList; +use crate::pages::PageState; + +use sycamore::prelude::*; + +pub struct InventoryPageProps { + pub page_state: PageState, +} + +#[component(InventoryPage)] +pub fn inventory_page(props: InventoryPageProps) -> View { + view! { + ShoppingList(props.page_state) + } +} diff --git a/web/src/pages/mod.rs b/web/src/pages/mod.rs new file mode 100644 index 0000000..f196f34 --- /dev/null +++ b/web/src/pages/mod.rs @@ -0,0 +1,29 @@ +// 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 sycamore::prelude::*; + +use crate::app_state::AppRoutes; + +mod plan; +mod inventory; +mod cook; + +pub use plan::*; +pub use inventory::*; +pub use cook::*; + +#[derive(Clone)] +pub struct PageState { + pub route: Signal +} \ No newline at end of file diff --git a/web/src/pages/plan.rs b/web/src/pages/plan.rs new file mode 100644 index 0000000..0274799 --- /dev/null +++ b/web/src/pages/plan.rs @@ -0,0 +1,28 @@ +// 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::pages::PageState; +use crate::components::RecipeSelector; + +use sycamore::prelude::*; + +pub struct PlanPageProps { + pub page_state: PageState +} + +#[component(PlanPage)] +pub fn plan_page(props: PlanPageProps) -> View { + view! { + RecipeSelector(props.page_state) + } +} \ No newline at end of file diff --git a/web/src/web.rs b/web/src/web.rs index 9e18e12..42f5872 100644 --- a/web/src/web.rs +++ b/web/src/web.rs @@ -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::*, service::AppService}; +use crate::{app_state::*, components::*, service::AppService}; use crate::{console_debug, console_error, console_log}; use sycamore::{ @@ -19,23 +19,21 @@ use sycamore::{ futures::spawn_local_in_scope, prelude::*, }; -use sycamore_router::{HistoryIntegration, Route, Router, RouterProps}; -#[derive(Route, Debug)] -enum AppRoutes { - #[to("/ui/")] - Plan, - #[to("/ui/recipe/")] - Recipe { index: usize }, - #[not_found] - NotFound, -} +use crate::pages::*; -fn route_switch(route: ReadSignal) -> View { - view! { +fn route_switch(page_state: PageState) -> View { + let route = page_state.route.clone(); + cloned!((page_state, route) => view! { (match route.get().as_ref() { AppRoutes::Plan => view! { - MealPlan() + PlanPage(PlanPageProps { page_state: page_state.clone() }) + }, + AppRoutes::Inventory => view! { + InventoryPage(InventoryPageProps { page_state: page_state.clone() }) + }, + AppRoutes::Cook => view! { + CookPage(CookPageProps { page_state: page_state.clone() }) }, AppRoutes::Recipe { index: idx } => view! { RecipeView(*idx) @@ -44,7 +42,7 @@ fn route_switch(route: ReadSignal) -> View { "NotFound" }, }) - } + }) } #[component(UI)] @@ -55,39 +53,39 @@ pub fn ui() -> View { // NOTE(jwall): Set the app_service in our toplevel scope. Children will be able // to find the service as long as they are a child of this scope. ContextProvider(ContextProviderProps { - value: app_service.clone(), - children: || view! { - Router(RouterProps::new(HistoryIntegration::new(), move |routes: ReadSignal| { - let view = Signal::new(View::empty()); - create_effect(cloned!((view) => move || { - spawn_local_in_scope(cloned!((routes, view) => { - let mut app_service = app_service.clone(); - async move { - match AppService::fetch_recipes().await { - Ok(Some(recipes)) => { - app_service.set_recipes(recipes); - } - Ok(None) => { - console_error!("No recipes to find"); - } - Err(msg) => console_error!("Failed to get recipes {}", msg), - } - console_debug!("Determining route."); - view.set(route_switch(routes)); - console_debug!("Created our route view effect."); + value: app_service.clone(), + children: || { + let view = Signal::new(View::empty()); + let route = Signal::new(AppRoutes::Plan); + let page_state = PageState { route: route.clone() }; + create_effect(cloned!((page_state, view) => move || { + spawn_local_in_scope(cloned!((page_state, view) => { + let mut app_service = app_service.clone(); + async move { + match AppService::fetch_recipes().await { + Ok(Some(recipes)) => { + app_service.set_recipes(recipes); } - })); - })); - view! { - // NOTE(jwall): The Router component *requires* there to be exactly one node as the root of this view. - // No fragments or missing nodes allowed or it will panic at runtime. - div(class="app") { - Header() - (view.get().as_ref().clone()) + Ok(None) => { + console_error!("No recipes to find"); + } + Err(msg) => console_error!("Failed to get recipes {}", msg), } + console_debug!("Determining route."); + view.set(route_switch(page_state.clone())); + console_debug!("Created our route view effect."); } - })) + })); + })); + view! { + // NOTE(jwall): The Router component *requires* there to be exactly one node as the root of this view. + // No fragments or missing nodes allowed or it will panic at runtime. + div(class="app") { + Header() + (view.get().as_ref().clone()) + } } + } }) } }