Add router store that updates window location on push

This commit is contained in:
Zak Patterson 2019-02-04 00:04:22 -05:00
parent 6f94fdd135
commit b791be68ba
2 changed files with 32 additions and 0 deletions

View File

@ -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]
}

View File

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