aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/component/index/Books.scala
blob: 5d7c92e7be83e6a1f6b023de6dc6c1da2e95931f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package reading.component.index

import rx._
import Ctx.Owner.Unsafe._

import scalatags.JsDom.all._
import scalacss.Defaults._
import scalacss.ScalatagsCss._

import reading.component.index.style.{ Books => BooksStyle }
import reading.component.widget.Popup
import reading.models.{ Book }
import reading.utils.{ RxTag, RxAttr }

object Books {
  def apply(books: Rx[Seq[Book]]): Frag = {
    val focus: Var[Option[Book]] = Var(None)

    div(
      BooksStyle.render,

      RxTag { implicit context =>
        div(
          div(
            BooksStyle.books,

            books().sorted.map { book =>
              div(
                BooksStyle.book,
                img(
                  BooksStyle.cover,
                  src := s"cover/${book.title}.jpg",
                  RxAttr(onclick, Rx(() => focus() = Some(book)))
                )
              )
            }
          ),

          RxTag { implicit context =>
            focus() match {
              case Some(book) => Popup(onClose = focus() = None)(BookDetail(book))
              case None => span("")
            }
          }
        )
      }
    )
  }
}