package com.weEat.view import com.weEat.controllers.{FoodController,USDAController} import com.weEat.modules._ import com.weEat.shared.models.UnitType._ import com.weEat.shared.models.USDANodeNoId import com.weEat.util.MHtmlHelpers._ import gov.usda.nal.fdc.models._ import gov.usda.nal.fdc.models.DataType._ import mhtml.Rx import mhtml.future.syntax._ import org.scalajs.dom.raw.Event import scala.util.Success object UsdaImporter extends View { import com.weEat.Main.headers implicit val ctx = scala.concurrent.ExecutionContext.global val tag = "importer" val title = "USDA Import" override val permissions = Set("admin") def content = { val searchBar = SearchBar(term => USDAController.getFoodsSearch(term, Seq( Foundation, Survey, SRLegacy ).map(_.toString))().map({ // TODO: Save on network calls by waiting until we're near the end of each // page before loading the next one. // Represent this as a Seq[Rx[(Boolean, Criteria)]] where each node depends // on the one before it in the sequence. case SearchResult(criteria, n, cur, tot, baseList) => (cur + 1 to tot).map({ c => USDAController.postFoodsSearch()(criteria.copy(pageNumber = Some(c))) .map(_.foods).toRx.map({ case Some(Success(l)) => l case _ => Nil }) }).foldLeft(Rx(baseList))({ (soFar, cur) => soFar.zip(cur).map({ case (soFarList, curList) => soFarList :++ curList }) }) })) val searchResults = searchBar.result.flatMap({ case Some(Success(inner)) => inner case _ => Rx(Nil) }) val pageSizeSel = Select(Rx((10 to 40 by 10))) val numPages = pageSizeSel.value.zip(searchResults).map({ case (size, results) => (results.length + size - 1) / size }) val pageSel = PageSelect(numPages)

USDA Importer

{ searchBar.render }
{ PaginatedTable[SearchResultFood](Seq( ("", 1, {x => Rx()}), ("ID", 1, {x => Rx({x.fdcId.toString})}), ("Name", 3, {x => Rx({x.description})}), ("Desc", 4, {x => Rx( {x.additionalDescriptions.getOrElse("")} )}), ("Brand", 3, {x => Rx({x.brandOwner.getOrElse("")})}) ), searchResults, pageSel.page, pageSizeSel.value).render } { (pageSel.render).addClass("col-3 float-left") } { (pageSizeSel.render).addClass("col-1 float-right") }
} }