diff --git a/.gitignore b/.gitignore index a5311a0..472442c 100644 --- a/.gitignore +++ b/.gitignore @@ -140,3 +140,5 @@ GitHub.sublime-settings # End of https://www.gitignore.io/api/sbt,scala,bloop,metals,intellij,sublimetext /.idea/ + +/outwatch-router/yarn.lock diff --git a/README.md b/README.md index 0969ef3..1c16834 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Most of this code is adapted from [http4s](http4s.org)'s route parsing and path Add to library dependencies: ``` - "com.clovellytech" %%% "outwatch-router" % "0.0.4" + "com.clovellytech" %%% "outwatch-router" % "0.0.5" ``` See [documentation][doc-root] diff --git a/build.sbt b/build.sbt index f791fb9..1f25304 100644 --- a/build.sbt +++ b/build.sbt @@ -81,7 +81,7 @@ lazy val router = project webpackBundlingMode in fastOptJS := BundlingMode.LibraryOnly(), resolvers += "jitpack" at "https://jitpack.io", libraryDependencies ++= Seq( - "io.github.outwatch" % "outwatch" % "ea240c6d04", + "io.github.outwatch" % "outwatch" % "e0f28a8fbb", "org.scalatest" %%% "scalatest" % "3.0.5" % Test ), copyFastOptJS := { diff --git a/outwatch-router/src/main/scala/outwatch/router/Router.scala b/outwatch-router/src/main/scala/outwatch/router/Router.scala index 7edea59..fcdfadd 100644 --- a/outwatch-router/src/main/scala/outwatch/router/Router.scala +++ b/outwatch-router/src/main/scala/outwatch/router/Router.scala @@ -1,6 +1,6 @@ package outwatch.router -import cats.effect.LiftIO +import cats.effect.IO import monix.execution.Scheduler import org.scalajs.dom.window import outwatch.dom._ @@ -12,7 +12,7 @@ final case class Replace(path: Path) extends Action final case class RouterState[P](page: P) -class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) { +class AppRouter[P](root: Path, f: Path => P) { def routerReducer(state: RouterState[P], action: Action): RouterState[P] = action match { case Replace(path) => window.history.replaceState("", "", Path(root, path).toString) @@ -20,24 +20,24 @@ class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) { case _ => state } - def store(implicit S : Scheduler): F[RouterStore[P]] = { + def store(implicit S : Scheduler): IO[RouterStore[P]] = { val startingPath = Path(window.location.pathname) - Store.create[RouterState[P], Action]( + Store.create[Action, RouterState[P]]( + Replace(startingPath), RouterState(f(startingPath)), Store.Reducer.justState(routerReducer _) - ).to[F] + ) } } object AppRouter{ def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): Observable[VDomModifier] = - store.map(state => resolver(state.page)) + store.map{ case (_, RouterState(p)) => resolver(p) } - def create[F[_]: LiftIO, P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] = - create[F, P](Root, notFound)(f) - - def create[F[_]: LiftIO, P](parent: Path, notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] = - new AppRouter[F, P](parent, f.lift.andThen(_.getOrElse(notFound))) + def create[P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[P] = + create[P](Root, notFound)(f) + def create[P](parent: Path, notFound: P)(f: PartialFunction[Path, P]): AppRouter[P] = + new AppRouter[P](parent, f.lift.andThen(_.getOrElse(notFound))) } diff --git a/outwatch-router/src/main/scala/outwatch/router/dsl/C.scala b/outwatch-router/src/main/scala/outwatch/router/dsl/C.scala new file mode 100644 index 0000000..73e15df --- /dev/null +++ b/outwatch-router/src/main/scala/outwatch/router/dsl/C.scala @@ -0,0 +1,13 @@ +package outwatch.router +package dsl + +import outwatch.dom.VDomModifier +import outwatch.dom.{dsl => O, _} + +object C { + def a[P](linkHref: String)(attrs: VDomModifier*)(implicit store: RouterStore[P]): BasicVNode = + O.a( + O.href := linkHref, + O.onClick.preventDefault.mapTo(Replace(Path(linkHref))) --> store + )(attrs) +} diff --git a/outwatch-router/src/main/scala/outwatch/router/package.scala b/outwatch-router/src/main/scala/outwatch/router/package.scala index 2857f31..1087418 100644 --- a/outwatch-router/src/main/scala/outwatch/router/package.scala +++ b/outwatch-router/src/main/scala/outwatch/router/package.scala @@ -3,7 +3,7 @@ package outwatch import outwatch.dom.VDomModifier package object router { - type RouterStore[P] = ProHandler[Action, RouterState[P]] + type RouterStore[P] = ProHandler[Action, (Action, RouterState[P])] type RouterResolve[P] = PartialFunction[P, VDomModifier] } diff --git a/project/Version.scala b/project/Version.scala index def7504..8b7ea6e 100644 --- a/project/Version.scala +++ b/project/Version.scala @@ -1,4 +1,4 @@ object Version{ - val version = "0.0.4" + val version = "0.0.5" val scalaVersion = "2.12.8" }