Browse Source

Add router component render ability

master
Zak Patterson 5 years ago
parent
commit
ce39b200c2
  1. 10
      outwatch-router/src/main/scala/outwatch/router/Router.scala
  2. 4
      outwatch-router/src/main/scala/outwatch/router/package.scala

10
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)))
}

4
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]
}
Loading…
Cancel
Save