kitchen/web/src/web.rs

94 lines
3.7 KiB
Rust
Raw Normal View History

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.
use crate::{components::*, service::AppService};
2022-01-25 21:03:36 -05:00
use crate::{console_debug, console_error, console_log};
2022-01-24 19:59:16 -05:00
use sycamore::{
context::{ContextProvider, ContextProviderProps},
futures::spawn_local_in_scope,
prelude::*,
};
use sycamore_router::{HistoryIntegration, Route, Router, RouterProps};
2022-01-13 18:07:01 -05:00
2022-01-26 20:40:21 -05:00
#[derive(Route, Debug)]
enum AppRoutes {
2022-02-15 22:30:18 -05:00
#[to("/ui/")]
Plan,
2022-01-26 20:40:21 -05:00
#[to("/ui/recipe/<index>")]
Recipe { index: usize },
#[not_found]
NotFound,
}
2022-02-10 17:11:27 -05:00
fn route_switch<G: Html>(route: ReadSignal<AppRoutes>) -> View<G> {
view! {
(match route.get().as_ref() {
2022-02-15 22:30:18 -05:00
AppRoutes::Plan => view! {
MealPlan()
2022-02-10 17:11:27 -05:00
},
AppRoutes::Recipe { index: idx } => view! {
RecipeView(*idx)
},
AppRoutes::NotFound => view! {
"NotFound"
},
})
}
}
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! {
// 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<AppRoutes>| {
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 {
2022-02-16 21:30:31 -05:00
Ok(Some(recipes)) => {
app_service.set_recipes(recipes);
}
2022-02-16 21:30:31 -05:00
Ok(None) => {
console_error!("No recipes to find");
}
Err(msg) => console_error!("Failed to get recipes {}", msg),
}
console_debug!("Determining route.");
2022-02-10 17:11:27 -05:00
view.set(route_switch(routes));
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") {
2022-02-07 16:03:51 -05:00
Header()
(view.get().as_ref().clone())
}
}
}))
2022-01-26 20:40:21 -05:00
}
})
2022-01-25 20:32:17 -05:00
}
2022-01-23 14:28:01 -05:00
}