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.
|
|
|
|
|
2022-12-30 16:43:56 -06:00
|
|
|
use crate::{
|
|
|
|
app_state::StateHandler,
|
2023-03-25 08:55:32 -04:00
|
|
|
components::{toast::Container, Footer, Header},
|
2022-12-30 16:43:56 -06:00
|
|
|
pages::*,
|
|
|
|
};
|
2022-11-01 20:38:14 -04:00
|
|
|
use sycamore::prelude::*;
|
|
|
|
use sycamore_router::{HistoryIntegration, Route, Router};
|
2022-12-30 16:43:56 -06:00
|
|
|
use tracing::{debug, instrument};
|
2022-11-01 20:38:14 -04:00
|
|
|
|
|
|
|
#[derive(Route, Debug)]
|
|
|
|
pub enum Routes {
|
2022-11-07 16:47:46 -05:00
|
|
|
#[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,
|
2023-01-06 00:32:57 -05:00
|
|
|
// TODO(jwall): This route is now deprecated. Remove when safe to do so.
|
2022-11-07 16:47:46 -05:00
|
|
|
#[to("/categories")]
|
|
|
|
Categories,
|
2023-01-06 00:32:57 -05:00
|
|
|
#[to("/ingredients")]
|
|
|
|
Ingredients,
|
2022-11-10 16:33:35 -05:00
|
|
|
#[to("/staples")]
|
|
|
|
Staples,
|
2022-11-07 16:47:46 -05:00
|
|
|
#[not_found]
|
|
|
|
NotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Route, Debug)]
|
|
|
|
pub enum PlanningRoutes {
|
2023-01-17 13:44:13 -05:00
|
|
|
#[to("/select")]
|
|
|
|
Select,
|
2022-11-07 16:47:46 -05:00
|
|
|
#[to("/plan")]
|
2022-11-01 20:38:14 -04:00
|
|
|
Plan,
|
2022-11-07 16:47:46 -05:00
|
|
|
#[to("/inventory")]
|
2022-11-01 20:38:14 -04:00
|
|
|
Inventory,
|
2022-11-07 16:47:46 -05:00
|
|
|
#[to("/cook")]
|
2022-11-01 20:38:14 -04:00
|
|
|
Cook,
|
|
|
|
#[not_found]
|
|
|
|
NotFound,
|
|
|
|
}
|
|
|
|
|
2022-12-26 21:29:09 -05:00
|
|
|
#[derive(Props)]
|
|
|
|
pub struct HandlerProps<'ctx> {
|
|
|
|
sh: StateHandler<'ctx>,
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:43:56 -06:00
|
|
|
#[instrument(skip_all, fields(?route))]
|
|
|
|
fn route_switch<'ctx, G: Html>(route: &Routes, cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View<G> {
|
|
|
|
debug!("Handling route change");
|
|
|
|
use ManageRoutes::*;
|
|
|
|
use PlanningRoutes::*;
|
|
|
|
match route {
|
2023-01-17 13:44:13 -05:00
|
|
|
Routes::Planning(Select) => view! {cx,
|
|
|
|
SelectPage(sh)
|
|
|
|
},
|
2022-12-30 16:43:56 -06:00
|
|
|
Routes::Planning(Plan) => view! {cx,
|
|
|
|
PlanPage(sh)
|
|
|
|
},
|
|
|
|
Routes::Planning(Inventory) => view! {cx,
|
|
|
|
InventoryPage(sh)
|
|
|
|
},
|
|
|
|
Routes::Planning(Cook) => view! {cx,
|
|
|
|
CookPage(sh)
|
|
|
|
},
|
|
|
|
Routes::Login => view! {cx,
|
|
|
|
LoginPage(sh)
|
|
|
|
},
|
|
|
|
Routes::Recipe(RecipeRoutes::View(id)) => view! {cx,
|
|
|
|
RecipeViewPage(recipe=id.clone(), sh=sh)
|
|
|
|
},
|
|
|
|
Routes::Recipe(RecipeRoutes::Edit(id)) => view! {cx,
|
|
|
|
RecipeEditPage(recipe=id.clone(), sh=sh)
|
|
|
|
},
|
|
|
|
Routes::Manage(Categories) => view! {cx,
|
2023-01-06 00:32:57 -05:00
|
|
|
IngredientsPage(sh)
|
|
|
|
},
|
|
|
|
Routes::Manage(Ingredients) => view! {cx,
|
|
|
|
IngredientsPage(sh)
|
2022-12-30 16:43:56 -06:00
|
|
|
},
|
|
|
|
Routes::Manage(NewRecipe) => view! {cx,
|
|
|
|
AddRecipePage(sh)
|
|
|
|
},
|
|
|
|
Routes::Manage(Staples) => view! {cx,
|
|
|
|
StaplesPage(sh)
|
|
|
|
},
|
|
|
|
Routes::NotFound
|
|
|
|
| Routes::Manage(ManageRoutes::NotFound)
|
|
|
|
| Routes::Planning(PlanningRoutes::NotFound)
|
|
|
|
| Routes::Recipe(RecipeRoutes::NotFound) => view! {cx,
|
|
|
|
// TODO(Create a real one)
|
|
|
|
PlanPage(sh)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 20:38:14 -04:00
|
|
|
#[component]
|
2022-12-26 21:29:09 -05:00
|
|
|
pub fn Handler<'ctx, G: Html>(cx: Scope<'ctx>, props: HandlerProps<'ctx>) -> View<G> {
|
|
|
|
let HandlerProps { sh } = props;
|
2022-11-01 20:38:14 -04:00
|
|
|
view! {cx,
|
|
|
|
Router(
|
|
|
|
integration=HistoryIntegration::new(),
|
2022-12-26 21:56:28 -05:00
|
|
|
view=move |cx: Scope, route: &ReadSignal<Routes>| {
|
2022-12-30 16:43:56 -06:00
|
|
|
view!{cx,
|
|
|
|
div(class="app") {
|
2023-03-25 08:55:32 -04:00
|
|
|
Container()
|
2022-12-30 16:43:56 -06:00
|
|
|
Header(sh)
|
|
|
|
(route_switch(route.get().as_ref(), cx, sh))
|
|
|
|
Footer { }
|
|
|
|
}
|
2022-12-26 21:56:28 -05:00
|
|
|
}
|
2022-12-26 21:29:09 -05:00
|
|
|
},
|
2022-11-01 20:38:14 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|