use new state handler in RouteHandler

This commit is contained in:
Jeremy Wall 2022-12-26 21:29:09 -05:00
parent 02536d63d8
commit e21353eeba
4 changed files with 26 additions and 17 deletions

View File

@ -131,10 +131,9 @@ impl MessageMapper<Message, AppState> for StateMachine {
} }
} }
pub fn get_state_handler<'ctx>( pub type StateHandler<'ctx> = &'ctx Handler<'ctx, StateMachine, AppState, Message>;
cx: Scope<'ctx>,
initial: AppState, pub fn get_state_handler<'ctx>(cx: Scope<'ctx>, initial: AppState) -> StateHandler<'ctx> {
) -> &'ctx Handler<'ctx, StateMachine, AppState, Message> {
Handler::new(cx, initial, StateMachine()) Handler::new(cx, initial, StateMachine())
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -14,14 +14,10 @@
use sycamore::prelude::*; use sycamore::prelude::*;
use crate::app_state::{AppState, Message, StateMachine}; use crate::app_state::StateHandler;
use sycamore_state::Handler;
#[component] #[component]
pub fn Header<'ctx, G: Html>( pub fn Header<'ctx, G: Html>(cx: Scope<'ctx>, h: StateHandler<'ctx>) -> View<G> {
cx: Scope<'ctx>,
h: &'ctx Handler<'ctx, StateMachine, AppState, Message>,
) -> View<G> {
let login = h.get_selector(cx, |sig| match &sig.get().auth { let login = h.get_selector(cx, |sig| match &sig.get().auth {
Some(id) => id.user_id.clone(), Some(id) => id.user_id.clone(),
None => "Login".to_owned(), None => "Login".to_owned(),

View File

@ -12,19 +12,25 @@
// 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 sycamore::prelude::*; use sycamore::prelude::*;
use sycamore_router::{HistoryIntegration, Route, Router}; use sycamore_router::{HistoryIntegration, Route, Router};
use tracing::{debug, instrument}; use tracing::{debug, instrument};
use crate::pages::*; use crate::pages::*;
#[instrument] #[instrument(skip_all, fields(?route))]
fn route_switch<'a, G: Html>(cx: Scope<'a>, route: &'a ReadSignal<Routes>) -> View<G> { fn route_switch<'ctx, G: Html>(
cx: Scope<'ctx>,
sh: StateHandler<'ctx>,
route: &'ctx ReadSignal<Routes>,
) -> View<G> {
// NOTE(jwall): This needs to not be a dynamic node. The rules around // NOTE(jwall): This needs to not be a dynamic node. The rules around
// this are somewhat unclear and underdocumented for Sycamore. But basically // this are somewhat unclear and underdocumented for Sycamore. But basically
// avoid conditionals in the `view!` macro calls here. // avoid conditionals in the `view!` macro calls here.
let switcher = |cx: Scope, route: &Routes| { let switcher = |cx: Scope, sh: StateHandler, route: &Routes| {
debug!(?route, "Dispatching for route"); debug!(?route, "Dispatching for route");
match route { match route {
Routes::Planning(Plan) => view! {cx, Routes::Planning(Plan) => view! {cx,
@ -65,7 +71,7 @@ fn route_switch<'a, G: Html>(cx: Scope<'a>, route: &'a ReadSignal<Routes>) -> Vi
}; };
use PlanningRoutes::*; use PlanningRoutes::*;
view! {cx, view! {cx,
(switcher(cx, route.get().as_ref())) (switcher(cx, sh, route.get().as_ref()))
} }
} }
@ -117,12 +123,20 @@ pub enum PlanningRoutes {
NotFound, NotFound,
} }
#[derive(Props)]
pub struct HandlerProps<'ctx> {
sh: StateHandler<'ctx>,
}
#[component] #[component]
pub fn Handler<G: Html>(cx: Scope) -> View<G> { pub fn Handler<'ctx, G: Html>(cx: Scope<'ctx>, props: HandlerProps<'ctx>) -> View<G> {
let HandlerProps { sh } = props;
view! {cx, view! {cx,
Router( Router(
integration=HistoryIntegration::new(), integration=HistoryIntegration::new(),
view=route_switch, view=|cx, route| {
route_switch(cx, sh, route)
},
) )
} }
} }

View File

@ -34,7 +34,7 @@ pub fn UI<G: Html>(cx: Scope) -> View<G> {
view.set(view! { cx, view.set(view! { cx,
div(class="app") { div(class="app") {
Header(handler) Header(handler)
RouteHandler() RouteHandler(sh=handler)
Footer { } Footer { }
} }
}); });