From b791be68ba3235d5b3179c6500a6dded22c9ba86 Mon Sep 17 00:00:00 2001 From: Zak Patterson Date: Mon, 4 Feb 2019 00:04:22 -0500 Subject: [PATCH] Add router store that updates window location on push --- .../main/scala/outwatch/router/Router.scala | 27 +++++++++++++++++++ .../main/scala/outwatch/router/package.scala | 5 ++++ 2 files changed, 32 insertions(+) create mode 100644 outwatch-router/src/main/scala/outwatch/router/Router.scala create mode 100644 outwatch-router/src/main/scala/outwatch/router/package.scala diff --git a/outwatch-router/src/main/scala/outwatch/router/Router.scala b/outwatch-router/src/main/scala/outwatch/router/Router.scala new file mode 100644 index 0000000..a6d4c08 --- /dev/null +++ b/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] +} diff --git a/outwatch-router/src/main/scala/outwatch/router/package.scala b/outwatch-router/src/main/scala/outwatch/router/package.scala new file mode 100644 index 0000000..6566cc3 --- /dev/null +++ b/outwatch-router/src/main/scala/outwatch/router/package.scala @@ -0,0 +1,5 @@ +package outwatch + +package object router { + type RouterStore = ProHandler[Action, RouterState] +}