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