diff --git a/outwatch-router/src/main/scala/outwatch/router/Router.scala b/outwatch-router/src/main/scala/outwatch/router/Router.scala index a6d4c08..e6d91ed 100644 --- a/outwatch-router/src/main/scala/outwatch/router/Router.scala +++ b/outwatch-router/src/main/scala/outwatch/router/Router.scala @@ -3,19 +3,20 @@ package outwatch.router import cats.effect.LiftIO import monix.execution.Scheduler import org.scalajs.dom.window +import outwatch.dom._, dsl._ import outwatch.util.Store sealed trait Action final case class Replace(path: Path) extends Action -final case class RouterState(currentPath: Path) +final case class RouterState(path: Path) -case class AppRouter() { +object AppRouter { def routerReducer(state: RouterState, action: Action): RouterState = action match { case Replace(path) => Path.unapplySeq(path).foreach(p => window.history.replaceState("", "", p.mkString("/"))) - state.copy(currentPath = path) + state.copy(path = path) case _ => state } @@ -24,4 +25,7 @@ case class AppRouter() { RouterState(Root), Store.Reducer.justState(routerReducer _) ).to[F] + + def render(resolver: RouterResolve)(implicit store: RouterStore): VDomModifier = + div(store.map(state => resolver(state.path))) } diff --git a/outwatch-router/src/main/scala/outwatch/router/package.scala b/outwatch-router/src/main/scala/outwatch/router/package.scala index 6566cc3..362b185 100644 --- a/outwatch-router/src/main/scala/outwatch/router/package.scala +++ b/outwatch-router/src/main/scala/outwatch/router/package.scala @@ -1,5 +1,9 @@ package outwatch +import outwatch.dom.VDomModifier + package object router { type RouterStore = ProHandler[Action, RouterState] + + type RouterResolve = PartialFunction[Path, VDomModifier] }