Browse Source

Add router store that updates window location on push

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

27
outwatch-router/src/main/scala/outwatch/router/Router.scala

@ -0,0 +1,27 @@
package outwatch.router
import cats.effect.LiftIO
import monix.execution.Scheduler
import org.scalajs.dom.window
import outwatch.util.Store
sealed trait Action
final case class Replace(path: Path) extends Action
final case class RouterState(currentPath: Path)
case class 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)
case _ => state
}
def store[F[_]: LiftIO](implicit S : Scheduler): F[RouterStore] =
Store.create[RouterState, Action](
RouterState(Root),
Store.Reducer.justState(routerReducer _)
).to[F]
}

5
outwatch-router/src/main/scala/outwatch/router/package.scala

@ -0,0 +1,5 @@
package outwatch
package object router {
type RouterStore = ProHandler[Action, RouterState]
}
Loading…
Cancel
Save