feat: index page for desktop

This commit is contained in:
efim
2023-09-24 19:58:05 +00:00
parent aca8599d6e
commit ae33d7e72a
3 changed files with 247 additions and 214 deletions

View File

@@ -30,7 +30,7 @@ case class Routes(countries: List[Country])(implicit
}
val engine: TemplateEngine = buildTemplateEngine()
private val pageSize = 10
private val pageSize = 12
@cask.get("/")
def indexPage(region: Option[String] = None, page: Int = 0) = {
@@ -62,6 +62,38 @@ case class Routes(countries: List[Country])(implicit
)
}
/**
* this method returns directly set of cards
* and new anchor for loading next page of cards
*
* intended to be called from "next-page-anchor" with htmx
*/
@cask.get("/countries-cards")
def getPageOfCountriesCards(region: Option[String] = None, page: Int = 0) = {
val context = new Context()
val selectedCountries = region match {
case None => countries
case Some("") => countries
case Some(selectedRegion) => countries.filter(_.region == selectedRegion)
}
val startIndex = page * pageSize
val countriesPage =
selectedCountries.slice(startIndex, startIndex + pageSize)
// if current page is not full - there will be no next page
val nextPage = if (countriesPage.length == pageSize) page + 1 else -1
context.setVariable("countriesList", countriesPage.asJava)
context.setVariable("selectedRegion", region.getOrElse(""))
context.setVariable("nextPage", nextPage)
val cards = engine.process("index", Set("cards-of-countries", "infiniteScrollAnchor").asJava, context)
Response(
cards,
headers = Seq("Content-Type" -> "text/html; charset=utf-8")
)
}
@cask.get("/country")
def getCountryPage(countryName: String) = {
val context = new Context()