Created
October 26, 2019 20:11
-
-
Save elyphas/b63eaff10c23c57415e20fcc0b5877ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package manik1.modules.lstBienesStore | |
import manik1.services.WSCovenant | |
import outwatch.ext.monix.util._ | |
import monix.execution.Scheduler.Implicits.global | |
import monix.reactive.Observable | |
import spatutorial.shared.{LstItems, QryRenglon, Renglon} | |
import cats.effect.IO | |
/***********************************************************/ | |
import boopickle.Default._ | |
import mycelium.client._ | |
import manik1.services.{WSMyCelium} | |
import scala.concurrent.duration._ | |
/***********************************************************/ | |
class LstBienesStore { | |
sealed trait ActionsStore | |
case object Search extends ActionsStore | |
case class UpdateLstItems(l: LstItems[QryRenglon]) extends ActionsStore | |
case class UpdateField(field: String, value: String) extends ActionsStore | |
case class UpdateItemPrecio(field: String, value: String, col: Int, row: Int) extends ActionsStore | |
case class UpdateIdAndDescription(id: String, description: String) extends ActionsStore | |
case class UpdateStoreAndSaveDB(precio: Double) extends ActionsStore | |
case object InsertItem extends ActionsStore | |
case object Clean extends ActionsStore | |
case object Saved extends ActionsStore | |
case object NotSaved extends ActionsStore | |
case class UpdateRowActive(row: Int) extends ActionsStore | |
case class ServerFailure(msg: String) extends ActionsStore | |
case class RowActive(rowActive: Int = 0) | |
case class ItemNew(row: Int) | |
/*************************************************************************************************** | |
Vamos a usar variables para ver si esta editanto o es nuevo, despues se tiene que usar FSM. | |
****************************************************************************************************/ | |
case class State ( lstBienes: LstItems[QryRenglon] = LstItems(Seq.empty[QryRenglon]), | |
rowActive: RowActive = RowActive(), | |
itemNew: ItemNew = ItemNew(row = 0), | |
processingSideEffect: Option[String] = None) | |
val reduce: (State, ActionsStore) => (State, Observable[ActionsStore]) = (s, a) => { | |
a match { | |
case UpdateRowActive(rowAct) => | |
val setRowActive = RowActive(rowActive = rowAct) | |
(s.copy(rowActive = setRowActive), Observable.empty) | |
case Clean => | |
(s.copy(lstBienes = LstItems(Seq.empty[QryRenglon])), Observable.empty) | |
case UpdateLstItems(lst) => | |
val newList = lst.items.sortBy(_.renglon) | |
val lastRenglon = newList.size + 1 | |
val newItem = QryRenglon(renglon = lastRenglon) | |
val newItems = (newList :+ newItem).sortBy(_.renglon) | |
(s.copy(lstBienes = LstItems(newItems), itemNew = ItemNew(lastRenglon)), Observable.empty) | |
case InsertItem => | |
println("Testing inserting an Item!!!") | |
println("###########################################################################################################################") | |
val lastRenglon = s.lstBienes.items.size + 1 | |
val newItem = QryRenglon(renglon = lastRenglon) | |
val newItems = (s.lstBienes.items :+ newItem).sortBy(_.renglon) | |
(s.copy(lstBienes = s.lstBienes.copy(items = newItems)), Observable.empty) | |
case UpdateIdAndDescription(id, descripc) => | |
val curItem = s.lstBienes.items(s.rowActive.rowActive - 1) | |
val curItemUpdated = curItem.copy(cve_articulo = id, descripcion_articulo = descripc) | |
val lstUpdated = s.lstBienes.updatedByIdx(s.rowActive.rowActive - 1, curItemUpdated) | |
(s.copy(lstBienes = lstUpdated), Observable.empty) | |
case UpdateField(field, value) => | |
val curItemUpdated = if (field == "cantidad") | |
s.lstBienes.items(s.rowActive.rowActive - 1).copy(cantidad = value.toInt) | |
else QryRenglon() | |
val lstUpdated = s.lstBienes.updatedByIdx(s.rowActive.rowActive - 1, curItemUpdated) | |
(s.copy(lstBienes = lstUpdated), Observable.empty) | |
case UpdateStoreAndSaveDB(precio) => | |
val curItemUpdated = s.lstBienes.items(s.rowActive.rowActive - 1).copy(precio = precio) | |
val lstUpdated = s.lstBienes.updatedByIdx(s.rowActive.rowActive - 1, curItemUpdated) | |
val actionSave = if (s.rowActive.rowActive == s.itemNew.row) insertRenglon(curItemUpdated) | |
else saveRenglon(curItemUpdated) | |
(s.copy(lstBienes = lstUpdated), actionSave) | |
case ServerFailure(msg) => | |
println(msg) | |
(s, Observable.empty) | |
case Saved => (s, Observable.empty) | |
case NotSaved => (s, Observable.empty) | |
case otros => | |
println("¡Algo malo paso y no sabemos que!!!") | |
(s, Observable.empty) | |
} | |
} | |
private def saveRenglon(reng: QryRenglon) = Observable.fromFuture { | |
val item = Renglon(ejercicio = reng.ejercicio,cve_oficina = reng.cve_oficina,folio = reng.folio,renglon = reng.renglon,cve_articulo = reng.cve_articulo,cantidad = reng.cantidad,precio = reng.precio) | |
WSCovenant.api.saveRenglon(item).map { | |
case Right(value) => Saved | |
case Left(error) => ServerFailure(error) | |
} | |
} | |
private def insertRenglon(reng: QryRenglon) = Observable.fromFuture { | |
val item = Renglon(ejercicio = reng.ejercicio,cve_oficina = reng.cve_oficina,folio = reng.folio,renglon = reng.renglon,cve_articulo = reng.cve_articulo,cantidad = reng.cantidad,precio = reng.precio) | |
WSCovenant.api.insertRenglon(item).map { | |
case Right(value) => Saved | |
case Left(error) => ServerFailure(error) | |
} | |
} | |
val initState = State() | |
//val store = Store.create[IO, ActionsStore, State](Clean, initState, reduce).unsafeRunSync() | |
val store = Store.create[IO](Clean, initState, reduce).unsafeRunSync() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment