|
@ -1,15 +1,18 @@ |
|
|
package nova.monadic_sfx.ui.components.todo |
|
|
|
|
|
|
|
|
package outwatchapp.ui.components.todo |
|
|
|
|
|
|
|
|
import scala.scalajs.js.annotation.JSExportAll |
|
|
import scala.scalajs.js.annotation.JSExportAll |
|
|
|
|
|
|
|
|
import cats.kernel.Eq |
|
|
import cats.kernel.Eq |
|
|
import com.softwaremill.quicklens._ |
|
|
import com.softwaremill.quicklens._ |
|
|
import io.circe.generic.JsonCodec |
|
|
import io.circe.generic.JsonCodec |
|
|
|
|
|
import io.odin.Logger |
|
|
import monix.bio.Task |
|
|
import monix.bio.Task |
|
|
import nova.monadic_sfx.util.reactive.store.Reducer |
|
|
|
|
|
import nova.monadic_sfx.util.reactive.store.Store |
|
|
|
|
|
|
|
|
import outwatchapp.util.reactive.store.Middlewares.actionLoggerMiddleware |
|
|
|
|
|
import outwatchapp.util.reactive.store.Reducer |
|
|
|
|
|
import outwatchapp.util.reactive.store.Store |
|
|
|
|
|
|
|
|
@JSExportAll |
|
|
@JSExportAll |
|
|
|
|
|
@JsonCodec |
|
|
case class Todo(id: Int, content: String) |
|
|
case class Todo(id: Int, content: String) |
|
|
object Todo { |
|
|
object Todo { |
|
|
implicit val eqForTodo = Eq.fromUniversalEquals[Todo] |
|
|
implicit val eqForTodo = Eq.fromUniversalEquals[Todo] |
|
@ -37,18 +40,16 @@ object TodoListStore { |
|
|
implicit val eqForState = Eq.fromUniversalEquals[State] |
|
|
implicit val eqForState = Eq.fromUniversalEquals[State] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
def reducer()( |
|
|
|
|
|
|
|
|
def reducer(logger: Logger[Task])( |
|
|
state: State, |
|
|
state: State, |
|
|
action: Action |
|
|
action: Action |
|
|
): (State, Option[Task[Action]]) = |
|
|
): (State, Option[Task[Action]]) = |
|
|
action match { |
|
|
action match { |
|
|
case Init => (state, None) |
|
|
case Init => (state, None) |
|
|
case Add(content) => |
|
|
case Add(content) => |
|
|
println("hello") |
|
|
|
|
|
val nextAction = Some(for { |
|
|
val nextAction = Some(for { |
|
|
// do some validation |
|
|
// do some validation |
|
|
// _ <- logger.debug(s"Received $content") |
|
|
// _ <- logger.debug(s"Received $content") |
|
|
_ <- Task(println(s"Received $content")) |
|
|
|
|
|
res <- Task.pure(InternalAdd(content)) |
|
|
res <- Task.pure(InternalAdd(content)) |
|
|
} yield res) |
|
|
} yield res) |
|
|
(state, nextAction) |
|
|
(state, nextAction) |
|
@ -68,19 +69,21 @@ object TodoListStore { |
|
|
.using(_ :+ Todo(state.counter, content)) |
|
|
.using(_ :+ Todo(state.counter, content)) |
|
|
.modify(_.counter) |
|
|
.modify(_.counter) |
|
|
.using(_ + 1) |
|
|
.using(_ + 1) |
|
|
(nextState, Some(Task.pure(End))) |
|
|
|
|
|
|
|
|
(nextState, Some(logger.debug(s"Received $content") >> Task.pure(End))) |
|
|
case End => (state, None) |
|
|
case End => (state, None) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
def apply(): Task[Store[Action, State]] = |
|
|
|
|
|
|
|
|
def apply(logger: Logger[Task]): Task[Store[Action, State]] = |
|
|
Task.deferAction(implicit s => |
|
|
Task.deferAction(implicit s => |
|
|
for { |
|
|
for { |
|
|
|
|
|
logMware <- actionLoggerMiddleware[Action, State](logger, "TodoStore") |
|
|
store <- |
|
|
store <- |
|
|
Store |
|
|
Store |
|
|
.createL[Action, State]( |
|
|
.createL[Action, State]( |
|
|
Init, |
|
|
Init, |
|
|
State(Vector.empty[Todo], 0), |
|
|
State(Vector.empty[Todo], 0), |
|
|
Reducer.withOptionalEffects(reducer() _) |
|
|
|
|
|
|
|
|
Reducer.withOptionalEffects(reducer(logger) _), |
|
|
|
|
|
Seq(logMware) |
|
|
) |
|
|
) |
|
|
} yield store |
|
|
} yield store |
|
|
) |
|
|
) |
|
|