Add router store that updates window location on push
This commit is contained in:
parent
6f94fdd135
commit
b791be68ba
27
outwatch-router/src/main/scala/outwatch/router/Router.scala
Normal file
27
outwatch-router/src/main/scala/outwatch/router/Router.scala
Normal 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]
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package outwatch
|
||||
|
||||
package object router {
|
||||
type RouterStore = ProHandler[Action, RouterState]
|
||||
}
|
Loading…
Reference in New Issue
Block a user