|
|
@ -3,7 +3,8 @@ package outwatch.router |
|
|
|
import cats.effect.LiftIO |
|
|
|
import monix.execution.Scheduler |
|
|
|
import org.scalajs.dom.window |
|
|
|
import outwatch.dom._, dsl._ |
|
|
|
import outwatch.dom._ |
|
|
|
import monix.reactive.Observable |
|
|
|
import outwatch.util.Store |
|
|
|
|
|
|
|
sealed trait Action |
|
|
@ -11,11 +12,10 @@ final case class Replace(path: Path) extends Action |
|
|
|
|
|
|
|
final case class RouterState[P](page: P) |
|
|
|
|
|
|
|
class AppRouter[F[_]: LiftIO, P](f: Path => P) { |
|
|
|
class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) { |
|
|
|
def routerReducer(state: RouterState[P], action: Action): RouterState[P] = action match { |
|
|
|
case Replace(path) => |
|
|
|
println(s"Going to $path") |
|
|
|
Path.unapplySeq(path).foreach(p => window.history.replaceState("", "", p.mkString("/"))) |
|
|
|
Path.unapplySeq(Path(root, path)).foreach(p => window.history.replaceState("", "", p.mkString("/"))) |
|
|
|
state.copy(page = f(path)) |
|
|
|
case _ => state |
|
|
|
} |
|
|
@ -30,11 +30,14 @@ class AppRouter[F[_]: LiftIO, P](f: Path => P) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
object AppRouter{ |
|
|
|
def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): VDomModifier = |
|
|
|
div(store.map(state => resolver(state.page))) |
|
|
|
def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): Observable[VDomModifier] = |
|
|
|
store.map(state => resolver(state.page)) |
|
|
|
|
|
|
|
def create[F[_]: LiftIO, P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] = |
|
|
|
new AppRouter[F, P](f.lift.andThen(_.getOrElse(notFound))) |
|
|
|
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))) |
|
|
|
|
|
|
|
} |