aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/component/index/Books.scala
blob: c22639f0201a19b1d4d3105685429a4cef485a24 (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 scalatags.JsDom.all._
import scalacss.Defaults._
import scalacss.ScalatagsCss._

import reading.component.index.style.{ Books => BooksStyle }
import reading.component.widget.Modal
import reading.models.{ Book }
import reading.utils.RxUtils._

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

    div(
      BooksStyle.render,

      Rx {
        div(
          div(
            BooksStyle.books,

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

          Rx {
            focus() match {
              case Some(book) => Modal(onClose = focus() = None)(BookDetail(book))
              case None => span("")
            }
          }
        )
      }
    )
  }
}