Fix non dynamic routing :-(

This commit is contained in:
Jeremy Wall 2022-12-30 16:43:56 -06:00
parent a39ed5589f
commit 0167e6070d
4 changed files with 58 additions and 49 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ webdist/
nix/*/result nix/*/result
result result
.vscode/ .vscode/
.session_store/ .session_store/
.gitignore/

View File

@ -43,7 +43,6 @@ pub fn TabbedView<'a, G: Html>(cx: Scope<'a>, state: TabState<'a, G>) -> View<G>
view! {cx, view! {cx,
li(class=class) { a(href=href) { (show) } } li(class=class) { a(href=href) { (show) } }
} }
// TODO
}) })
.collect(), .collect(),
); );

View File

@ -12,11 +12,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crate::app_state::StateHandler; use crate::{
app_state::StateHandler,
components::{Footer, Header},
pages::*,
};
use sycamore::prelude::*; use sycamore::prelude::*;
use sycamore_router::{HistoryIntegration, Route, Router}; use sycamore_router::{HistoryIntegration, Route, Router};
use tracing::{debug, instrument};
use crate::pages::*;
#[derive(Route, Debug)] #[derive(Route, Debug)]
pub enum Routes { pub enum Routes {
@ -71,52 +74,63 @@ pub struct HandlerProps<'ctx> {
sh: StateHandler<'ctx>, sh: StateHandler<'ctx>,
} }
#[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 {
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,
CategoryPage(sh)
},
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)
},
}
}
#[component] #[component]
pub fn Handler<'ctx, G: Html>(cx: Scope<'ctx>, props: HandlerProps<'ctx>) -> View<G> { pub fn Handler<'ctx, G: Html>(cx: Scope<'ctx>, props: HandlerProps<'ctx>) -> View<G> {
let HandlerProps { sh } = props; let HandlerProps { sh } = props;
use ManageRoutes::*;
use PlanningRoutes::*;
view! {cx, view! {cx,
Router( Router(
integration=HistoryIntegration::new(), integration=HistoryIntegration::new(),
view=move |cx: Scope, route: &ReadSignal<Routes>| { view=move |cx: Scope, route: &ReadSignal<Routes>| {
match route.get().as_ref() { view!{cx,
Routes::Planning(Plan) => view! {cx, div(class="app") {
PlanPage(sh) Header(sh)
}, (route_switch(route.get().as_ref(), cx, sh))
Routes::Planning(Inventory) => view! {cx, Footer { }
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,
CategoryPage(sh)
},
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)
},
} }
}, },
) )
} }

View File

@ -15,7 +15,6 @@ use sycamore::{futures::spawn_local_scoped, prelude::*};
use tracing::{info, instrument}; use tracing::{info, instrument};
use crate::app_state::Message; use crate::app_state::Message;
use crate::components::{Footer, Header};
use crate::{api, routing::Handler as RouteHandler}; use crate::{api, routing::Handler as RouteHandler};
#[instrument] #[instrument]
@ -33,11 +32,7 @@ pub fn UI<G: Html>(cx: Scope) -> View<G> {
sh.dispatch(cx, Message::LoadState); sh.dispatch(cx, Message::LoadState);
// TODO(jwall): This needs to be moved into the RouteHandler // TODO(jwall): This needs to be moved into the RouteHandler
view.set(view! { cx, view.set(view! { cx,
div(class="app") { RouteHandler(sh=sh)
Header(sh)
RouteHandler(sh=sh)
Footer { }
}
}); });
} }
}); });