Add router component render ability

This commit is contained in:
Zak Patterson 2019-02-04 14:31:59 -05:00
parent b791be68ba
commit ce39b200c2
2 changed files with 11 additions and 3 deletions

View File

@ -3,19 +3,20 @@ package outwatch.router
import cats.effect.LiftIO import cats.effect.LiftIO
import monix.execution.Scheduler import monix.execution.Scheduler
import org.scalajs.dom.window import org.scalajs.dom.window
import outwatch.dom._, dsl._
import outwatch.util.Store import outwatch.util.Store
sealed trait Action sealed trait Action
final case class Replace(path: Path) extends 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 { def routerReducer(state: RouterState, action: Action): RouterState = action match {
case Replace(path) => case Replace(path) =>
Path.unapplySeq(path).foreach(p => window.history.replaceState("", "", p.mkString("/"))) Path.unapplySeq(path).foreach(p => window.history.replaceState("", "", p.mkString("/")))
state.copy(currentPath = path) state.copy(path = path)
case _ => state case _ => state
} }
@ -24,4 +25,7 @@ case class AppRouter() {
RouterState(Root), RouterState(Root),
Store.Reducer.justState(routerReducer _) Store.Reducer.justState(routerReducer _)
).to[F] ).to[F]
def render(resolver: RouterResolve)(implicit store: RouterStore): VDomModifier =
div(store.map(state => resolver(state.path)))
} }

View File

@ -1,5 +1,9 @@
package outwatch package outwatch
import outwatch.dom.VDomModifier
package object router { package object router {
type RouterStore = ProHandler[Action, RouterState] type RouterStore = ProHandler[Action, RouterState]
type RouterResolve = PartialFunction[Path, VDomModifier]
} }