Skip to content

Commit

Permalink
Add react router redux facade (#34)
Browse files Browse the repository at this point in the history
* Create react-router-redux facade

* Add test cases to react-router-redux facade

* Update documents

* Use Scala 2.12.2 in travis

Close #30
  • Loading branch information
shogowada authored Apr 29, 2017
1 parent 5609a51 commit 308c311
Show file tree
Hide file tree
Showing 20 changed files with 549 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist: trusty
language: scala

scala:
- 2.12.1
- 2.12.2

jdk:
- oraclejdk8
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ ReactDOM.render(<.div(^.id := "hello-world")("Hello, World!"), mountNode)
2. Depend on the libraries.
```
libraryDependencies ++= Seq(
"io.github.shogowada" %%% "scalajs-reactjs" % "0.12.0", // For react facade
"io.github.shogowada" %%% "scalajs-reactjs-router-dom" % "0.12.0", // Optional. For react-router-dom facade
"io.github.shogowada" %%% "scalajs-reactjs-redux" % "0.12.0", // Optional. For react-redux facade
"io.github.shogowada" %%% "scalajs-reactjs-redux-devtools" % "0.12.0" // Optional. For redux-devtools facade
"io.github.shogowada" %%% "scalajs-reactjs" % "0.13.0", // For react facade
"io.github.shogowada" %%% "scalajs-reactjs-router-dom" % "0.13.0", // Optional. For react-router-dom facade
"io.github.shogowada" %%% "scalajs-reactjs-redux" % "0.13.0", // Optional. For react-redux facade
"io.github.shogowada" %%% "scalajs-reactjs-redux-devtools" % "0.13.0" // Optional. For redux-devtools facade
)
```

## Examples

- [Basics](./example)
- [TODO App](./example/todo-app/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoapp/Main.scala)
- [Routing](./example/routing/src/main/scala/io/github/shogowada/scalajs/reactjs/example/routing/Main.scala)
- [Redux](./example/todo-app-redux/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoappredux)
- [Redux DevTools](./example/redux-devtools/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/devtools/Main.scala)
- [Redux Middleware](./example/redux-middleware/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/middleware/Main.scala)
- [I don't like `<` and `^`. How can I change them?](./example/custom-virtual-dom)
- [Basics](/example)
- [TODO App](/example/todo-app/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoapp/Main.scala)
- [Router](/example/routing/src/main/scala/io/github/shogowada/scalajs/reactjs/example/router/Main.scala)
- [Redux](/example/todo-app-redux/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoappredux)
- [Redux Middleware](/example/redux-middleware/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/middleware/Main.scala)
- [Router Redux](/example/router-redux/src/main/scala/io/github/shogowada/scalajs/reactjs/example/router/redux/Main.scala)
- [Redux DevTools](/example/redux-devtools/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/devtools/Main.scala)
- [I don't like `<` and `^`. How can I change them?](/example/custom-virtual-dom)
51 changes: 45 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
val CreateReactClassVersion = "^15.5.1"
val HistoryVersion = "^4.6.1"
val ReactVersion = "^15.5.3"
val ReactReduxVersion = "^5.0.3"
val ReactRouterVersion = "^4.0.0"
val ReactRouterReduxVersion = "next"
val ReduxVersion = "^3.6.0"
val ReduxDevToolsVersion = "^2.13.0"
val WebpackVersion = "^2.3.2"
Expand All @@ -26,7 +28,7 @@ publishArtifact := false
val commonSettings = Seq(
organization := "io.github.shogowada",
name := "scalajs-reactjs",
version := "0.12.0",
version := "0.13.0",
licenses := Seq("MIT" -> url("https://opensource.org/licenses/MIT")),
homepage := Some(url("https://github.com/shogowada/scalajs-reactjs")),
scalaVersion := "2.12.2",
Expand Down Expand Up @@ -74,6 +76,19 @@ lazy val core = project.in(file("core"))
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)

lazy val history = project.in(file("history"))
.settings(commonSettings: _*)
.settings(
name := "scalajs-history",
npmDependencies in Compile ++= Seq(
"history" -> HistoryVersion
),
(webpack in(Compile, fastOptJS)) := Seq(),
(webpack in(Compile, fullOptJS)) := Seq(),
publishArtifact := true
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)

lazy val router = project.in(file("router"))
.settings(commonSettings: _*)
.settings(
Expand All @@ -86,7 +101,7 @@ lazy val router = project.in(file("router"))
publishArtifact := true
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core)
.dependsOn(core, history)

lazy val routerDom = project.in(file("router-dom"))
.settings(commonSettings: _*)
Expand Down Expand Up @@ -131,6 +146,20 @@ lazy val reduxDevTools = project.in(file("redux-devtools"))
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core, redux)

lazy val routerRedux = project.in(file("router-redux"))
.settings(commonSettings: _*)
.settings(
name += "-router-redux",
npmDependencies in Compile ++= Seq(
"react-router-redux" -> ReactRouterReduxVersion
),
(webpack in(Compile, fastOptJS)) := Seq(),
(webpack in(Compile, fullOptJS)) := Seq(),
publishArtifact := true
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core, history, router, redux)

val exampleCommonSettings = commonSettings ++ Seq(
name += "-example",
(unmanagedResourceDirectories in Compile) += baseDirectory.value / "src" / "main" / "webapp"
Expand Down Expand Up @@ -192,14 +221,22 @@ lazy val exampleReduxMiddleware = project.in(file("example") / "redux-middleware
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core, redux, reduxDevTools)

lazy val exampleRouting = project.in(file("example") / "routing")
lazy val exampleRouter = project.in(file("example") / "router")
.settings(exampleCommonSettings: _*)
.settings(
name += "-routing"
name += "-router"
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core, routerDom)

lazy val exampleRouterRedux = project.in(file("example") / "router-redux")
.settings(exampleCommonSettings: _*)
.settings(
name += "-router-redux"
)
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.dependsOn(core, redux, routerDom, routerRedux, reduxDevTools)

lazy val exampleStyle = project.in(file("example") / "style")
.settings(exampleCommonSettings: _*)
.settings(
Expand Down Expand Up @@ -244,7 +281,8 @@ lazy val exampleTest = project.in(file("example") / "test")
s"-Dtarget.path.lifecycle=${(crossTarget in exampleLifecycle).value}",
s"-Dtarget.path.redux-devtools=${(crossTarget in exampleReduxDevTools).value}",
s"-Dtarget.path.redux-middleware=${(crossTarget in exampleReduxMiddleware).value}",
s"-Dtarget.path.routing=${(crossTarget in exampleRouting).value}",
s"-Dtarget.path.router=${(crossTarget in exampleRouter).value}",
s"-Dtarget.path.router-redux=${(crossTarget in exampleRouterRedux).value}",
s"-Dtarget.path.style=${(crossTarget in exampleStyle).value}",
s"-Dtarget.path.todo-app=${(crossTarget in exampleTodoApp).value}",
s"-Dtarget.path.todo-app-redux=${(crossTarget in exampleTodoAppRedux).value}",
Expand All @@ -256,7 +294,8 @@ lazy val exampleTest = project.in(file("example") / "test")
(webpack in fastOptJS in Compile in exampleLifecycle).value,
(webpack in fastOptJS in Compile in exampleReduxDevTools).value,
(webpack in fastOptJS in Compile in exampleReduxMiddleware).value,
(webpack in fastOptJS in Compile in exampleRouting).value,
(webpack in fastOptJS in Compile in exampleRouter).value,
(webpack in fastOptJS in Compile in exampleRouterRedux).value,
(webpack in fastOptJS in Compile in exampleStyle).value,
(webpack in fastOptJS in Compile in exampleTodoApp).value,
(webpack in fastOptJS in Compile in exampleTodoAppRedux).value
Expand Down
5 changes: 3 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def onChange(self: Self[Unit, State]) = // Use a higher-order function (a functi
Sure, you can! All the projects in this folder are fully working examples, but here are some top picks:

- [TODO App](/example/todo-app/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoapp/Main.scala)
- [Routing](/example/routing/src/main/scala/io/github/shogowada/scalajs/reactjs/example/routing/Main.scala)
- [Router](/example/routing/src/main/scala/io/github/shogowada/scalajs/reactjs/example/router/Main.scala)
- [Redux](/example/todo-app-redux/src/main/scala/io/github/shogowada/scalajs/reactjs/example/todoappredux)
- [Redux Middleware](/example/redux-middleware/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/middleware/Main.scala)
- [Router Redux](/example/router-redux/src/main/scala/io/github/shogowada/scalajs/reactjs/example/router/redux/Main.scala)
- [Redux DevTools](/example/redux-devtools/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/devtools/Main.scala)
- [Redux Middleware](./example/redux-middleware/src/main/scala/io/github/shogowada/scalajs/reactjs/example/redux/middleware/Main.scala)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ case class State(text: String)
case class SetText(text: String) extends Action

object Reducer {
def apply(maybeState: Option[State], action: Action): State =
def apply(maybeState: Option[State], action: Any): State =
action match {
case action: SetText => State(text = action.text)
case _ => State(text = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ case class Snapshot(snapshot: Int) extends Action
case class Error(throwable: Throwable) extends Action

object Reducer {
def reduce(maybeState: Option[State], action: Action): State =
def reduce(maybeState: Option[State], action: Any): State =
State(
result = reduceResult(maybeState.map(_.result), action),
snapshot = reduceSnapshot(maybeState.flatMap(_.snapshot), action),
error = reduceError(maybeState.flatMap(_.error), action)
)

def reduceResult(maybeResult: Option[Int], action: Action): Int = {
def reduceResult(maybeResult: Option[Int], action: Any): Int = {
val result = maybeResult.getOrElse(0)
action match {
case action: Add => result + action.value
Expand All @@ -48,14 +48,14 @@ object Reducer {
}
}

def reduceSnapshot(maybeSnapshot: Option[Int], action: Action): Option[Int] = {
def reduceSnapshot(maybeSnapshot: Option[Int], action: Any): Option[Int] = {
action match {
case action: Snapshot => Option(action.snapshot)
case _ => maybeSnapshot
}
}

def reduceError(maybeError: Option[Throwable], action: Action): Option[Throwable] = {
def reduceError(maybeError: Option[Throwable], action: Any): Option[Throwable] = {
action match {
case action: Error => Option(action.throwable)
case _ => maybeError
Expand Down
Loading

0 comments on commit 308c311

Please sign in to comment.