Created
July 1, 2019 17:21
-
-
Save elyphas/d2c446769819b3f83c2f5c140511493e to your computer and use it in GitHub Desktop.
This file contains 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 outwatch.util.Store | |
import monix.execution.Scheduler.Implicits.global | |
import shapeless.Generic | |
import spatutorial.shared.{Articulo, DatosGralesPedido, LstItems, Renglon} | |
/***********************************************************/ | |
import boopickle.Default._ | |
import mycelium.client._ | |
import manik1.services.{WSCovenant, WSMyCelium} | |
import scala.util.{Failure, Success} | |
import scala.concurrent.duration._ | |
/***********************************************************/ | |
class LstBienesStore { | |
sealed trait ActionsStore | |
case object Search extends ActionsStore | |
case class UpdateLstItems(l: LstItems) extends ActionsStore | |
case class UpdateItem(item: Renglon) extends ActionsStore | |
case class UpdateItem2(f: String, v: String) extends ActionsStore | |
case class InsertItem(item: Renglon) extends ActionsStore | |
case object CleanStore extends ActionsStore | |
case class AppState( lstBienes: LstItems = LstItems(Seq.empty[Renglon]), | |
datosGralesPedido: DatosGralesPedido = DatosGralesPedido(), | |
rowActive: Renglon = Renglon() ) | |
val reduce: (AppState, ActionsStore) => AppState = (s, a) => a match { | |
case CleanStore => s.copy(lstBienes = LstItems()) | |
case UpdateLstItems(lst) => s.copy(lstBienes = lst) | |
case InsertItem(item) => | |
val newItems: Seq[Renglon] = s.lstBienes.items ++ Seq(item) | |
s.copy(lstBienes = s.lstBienes.copy(items = newItems), rowActive = item) | |
case UpdateItem(renglon) => | |
def condIndex(a: Renglon, b: Renglon): Boolean = { | |
val genericRenglon1 = Generic[Renglon].to(a) | |
val genericRenglon2 = Generic[Renglon].to(b) | |
genericRenglon1.take(3) == genericRenglon2.take(3) | |
} | |
val genericRenglon = Generic[Renglon].to(renglon) | |
val condInd = condIndex(renglon, _: Renglon) | |
val newListBienes = s.lstBienes.updated(renglon, condInd) | |
s.copy(lstBienes = newListBienes) | |
case UpdateItem2(f, v) => | |
val rowActiveMap = gridcomponent.components.TransformData ( s.rowActive ) | |
val rowActiveMap2 = rowActiveMap + (f -> v) | |
import components.maptocc._ | |
def to[A]: ConvertHelper[A] = new ConvertHelper[A] | |
val newRow = to[Renglon].from(rowActiveMap2).getOrElse(Renglon()) | |
def condIndex(a: Renglon, b: Renglon): Boolean = { | |
val genericRenglon1 = Generic[Renglon].to(a) | |
val genericRenglon2 = Generic[Renglon].to(b) | |
genericRenglon1.take(4) == genericRenglon2.take(4) | |
} | |
val condInd = condIndex(newRow, _: Renglon) | |
val newListBienes = s.lstBienes.updated(newRow, condInd) | |
getDescripById(v, "getArticulo") | |
s.copy(lstBienes = newListBienes) | |
} | |
val initialState = AppState() | |
val store = Store.create[ActionsStore, AppState](CleanStore, initialState, reduce).unsafeRunSync() | |
def getDescripById( id: String, resource: String) = { | |
val wsMyCelium = new WSMyCelium("10.51.253.252", "8090") | |
val payload = Pickle.intoBytes[String](id) | |
val respondWS = wsMyCelium.ws.send(resource :: Nil, payload, SendType.WhenConnected, 30 seconds) | |
respondWS.failed.foreach(println) | |
respondWS.onComplete { | |
case Success(value) => | |
value match { | |
case Right(value) => | |
val unPickle = Unpickle[Either[String, Articulo]].fromBytes(value) | |
unPickle match { | |
case Right(value) => | |
val renglon = Renglon( cve_oficina = "1221", ejercicio = 2019, | |
folio = 1, renglon = 1, partida = "21101", descripcion_partida = "PAPELERIA", cve_articulo = value.id, | |
descripcion_articulo = value.descripcion.getOrElse(""), unidad = value.unidad, presentacion = value.presentacion, | |
unid_med_pres = Some("Pieza"), cantidad = 0, precio = 0.0, subtotal = 0.0, iva_monto = 0.0, total = 0.0) | |
store.onNext(UpdateItem(renglon)) | |
case Left(error) => org.scalajs.dom.window.prompt(error) | |
} | |
case Left(value) => org.scalajs.dom.window.prompt(value) | |
} | |
case Failure(exception) => org.scalajs.dom.window.prompt(exception.getMessage) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment