124 lines
3.3 KiB
Rust
Raw Normal View History

2022-11-01 20:38:14 -04:00
// Copyright 2022 zaphar
//
// 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 sycamore_router::{HistoryIntegration, Route, Router};
use tracing::{debug, instrument};
2022-11-01 20:38:14 -04:00
use crate::pages::*;
#[instrument]
fn route_switch<'a, G: Html>(cx: Scope<'a>, route: &'a ReadSignal<Routes>) -> 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.
let switcher = |cx: Scope, route: &Routes| {
debug!(?route, "Dispatching for route");
match route {
Routes::Planning(Plan) => view! {cx,
2022-11-01 20:38:14 -04:00
PlanPage()
},
Routes::Planning(Inventory) => view! {cx,
2022-11-01 20:38:14 -04:00
InventoryPage()
},
Routes::Planning(Cook) => view! {cx,
CookPage()
},
2022-11-01 20:38:14 -04:00
Routes::Login => view! {cx,
LoginPage()
},
Routes::Recipe(RecipeRoutes::View(id)) => view! {cx,
RecipeViewPage(recipe=id.clone())
2022-11-01 20:38:14 -04:00
},
Routes::Recipe(RecipeRoutes::Edit(id)) => view! {cx,
RecipeEditPage(recipe=id.clone())
2022-11-01 20:38:14 -04:00
},
Routes::Manage(ManageRoutes::Categories) => view! {cx,
2022-11-01 20:38:14 -04:00
CategoryPage()
},
Routes::Manage(ManageRoutes::NewRecipe) => view! {cx,
2022-11-01 20:38:14 -04:00
AddRecipePage()
},
Routes::NotFound
| Routes::Manage(ManageRoutes::NotFound)
| Routes::Planning(PlanningRoutes::NotFound)
| Routes::Recipe(RecipeRoutes::NotFound) => view! {cx,
2022-11-01 20:38:14 -04:00
// TODO(Create a real one)
PlanPage()
},
}
};
use PlanningRoutes::*;
view! {cx,
(switcher(cx, route.get().as_ref()))
2022-11-01 20:38:14 -04:00
}
}
#[derive(Route, Debug)]
pub enum Routes {
#[to("/ui/planning/<_..>")]
Planning(PlanningRoutes),
#[to("/ui/recipe/<_..>")]
Recipe(RecipeRoutes),
#[to("/ui/manage/<_..>")]
Manage(ManageRoutes),
#[to("/ui/login")]
Login,
#[not_found]
NotFound,
}
#[derive(Route, Debug)]
pub enum RecipeRoutes {
#[to("/edit/<id>")]
Edit(String),
#[to("/view/<id>")]
View(String),
#[not_found]
NotFound,
}
#[derive(Route, Debug)]
pub enum ManageRoutes {
#[to("/new_recipe")]
NewRecipe,
#[to("/categories")]
Categories,
#[not_found]
NotFound,
}
#[derive(Route, Debug)]
pub enum PlanningRoutes {
#[to("/plan")]
2022-11-01 20:38:14 -04:00
Plan,
#[to("/inventory")]
2022-11-01 20:38:14 -04:00
Inventory,
#[to("/cook")]
2022-11-01 20:38:14 -04:00
Cook,
#[not_found]
NotFound,
}
#[component]
pub fn Handler<G: Html>(cx: Scope) -> View<G> {
view! {cx,
Router(
integration=HistoryIntegration::new(),
view=route_switch,
)
}
}