2022-01-13 18:07:01 -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-07-15 19:30:06 -04:00
|
|
|
use crate::pages::*;
|
|
|
|
use crate::{app_state::*, components::*, router_integration::*, service::AppService};
|
|
|
|
use crate::{console_error, console_log};
|
2022-01-24 19:59:16 -05:00
|
|
|
|
2022-01-27 21:29:17 -05:00
|
|
|
use sycamore::{
|
|
|
|
context::{ContextProvider, ContextProviderProps},
|
|
|
|
futures::spawn_local_in_scope,
|
|
|
|
prelude::*,
|
|
|
|
};
|
2022-01-13 18:07:01 -05:00
|
|
|
|
2022-07-15 19:30:06 -04:00
|
|
|
fn route_switch<G: Html>(route: ReadSignal<AppRoutes>) -> View<G> {
|
|
|
|
// NOTE(jwall): This needs to not be a dynamic node. The rules around
|
|
|
|
// this are somewhat unclear and underdocumented for Sycamore. But basically
|
|
|
|
// avoid conditionals in the `view!` macro calls here.
|
|
|
|
cloned!((route) => match route.get().as_ref() {
|
|
|
|
AppRoutes::Plan => view! {
|
|
|
|
PlanPage()
|
|
|
|
},
|
|
|
|
AppRoutes::Inventory => view! {
|
|
|
|
InventoryPage()
|
|
|
|
},
|
|
|
|
AppRoutes::Cook => view! {
|
|
|
|
CookPage()
|
|
|
|
},
|
|
|
|
AppRoutes::Recipe(idx) => view! {
|
|
|
|
RecipePage(RecipePageProps { recipe: Signal::new(*idx) })
|
|
|
|
},
|
|
|
|
AppRoutes::NotFound => view! {
|
|
|
|
// TODO(Create a real one)
|
|
|
|
PlanPage()
|
|
|
|
},
|
|
|
|
AppRoutes::Error(ref e) => {
|
|
|
|
let e = e.clone();
|
|
|
|
view! {
|
|
|
|
"Error: " (e)
|
2022-06-28 20:29:09 -04:00
|
|
|
}
|
2022-07-15 19:30:06 -04:00
|
|
|
}
|
2022-03-02 21:03:28 -05:00
|
|
|
})
|
2022-02-10 17:11:27 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 20:32:17 -05:00
|
|
|
#[component(UI<G>)]
|
|
|
|
pub fn ui() -> View<G> {
|
2022-01-25 21:14:30 -05:00
|
|
|
let app_service = AppService::new();
|
2022-01-25 21:03:36 -05:00
|
|
|
console_log!("Starting UI");
|
2022-01-25 20:32:17 -05:00
|
|
|
view! {
|
2022-01-27 21:29:17 -05:00
|
|
|
// 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 {
|
2022-03-02 21:03:28 -05:00
|
|
|
value: app_service.clone(),
|
|
|
|
children: || {
|
2022-07-15 19:30:06 -04:00
|
|
|
create_effect(move || {
|
|
|
|
spawn_local_in_scope({
|
2022-03-02 21:03:28 -05:00
|
|
|
let mut app_service = app_service.clone();
|
|
|
|
async move {
|
2022-04-26 23:17:17 -04:00
|
|
|
match AppService::fetch_recipes_from_storage() {
|
2022-03-09 21:02:36 -05:00
|
|
|
Ok((_, Some(recipes))) => {
|
2022-03-02 21:03:28 -05:00
|
|
|
app_service.set_recipes(recipes);
|
|
|
|
}
|
2022-03-09 21:02:36 -05:00
|
|
|
Ok((_, None)) => {
|
2022-03-02 21:03:28 -05:00
|
|
|
console_error!("No recipes to find");
|
2022-01-27 21:29:17 -05:00
|
|
|
}
|
2022-03-02 21:03:28 -05:00
|
|
|
Err(msg) => console_error!("Failed to get recipes {}", msg),
|
2022-01-27 21:29:17 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 19:30:06 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-02 21:03:28 -05:00
|
|
|
view! {
|
|
|
|
div(class="app") {
|
|
|
|
Header()
|
2022-07-15 19:30:06 -04:00
|
|
|
Router(RouterProps {
|
|
|
|
route: AppRoutes::Plan,
|
|
|
|
route_select: route_switch,
|
|
|
|
browser_integration: BrowserIntegration::new(),
|
|
|
|
})
|
2022-03-02 21:03:28 -05:00
|
|
|
}
|
2022-01-26 20:40:21 -05:00
|
|
|
}
|
2022-03-02 21:03:28 -05:00
|
|
|
}
|
2022-01-27 21:29:17 -05:00
|
|
|
})
|
2022-01-25 20:32:17 -05:00
|
|
|
}
|
2022-01-23 14:28:01 -05:00
|
|
|
}
|