forked from ochrons/scalajs-spa-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotd.scala
43 lines (36 loc) · 1.24 KB
/
Motd.scala
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
package spatutorial.client.components
import autowire._
import japgolly.scalajs.react.vdom.prefix_<^._
import japgolly.scalajs.react.{BackendScope, ReactComponentB}
import spatutorial.client.components.Bootstrap._
import spatutorial.client.services.AjaxClient
import spatutorial.shared.Api
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow
/**
* This is a simple component demonstrating how to interact with the server
*/
object Motd {
case class State(message: String)
class Backend(t: BackendScope[Unit, State]) {
def refresh() {
// load a new message from the server
AjaxClient[Api].motd("User X").call().foreach { message =>
t.modState(_ => State(message))
}
}
}
// create the React component for holding the Message of the Day
val Motd = ReactComponentB[Unit]("Motd")
.initialState(State("loading...")) // show a loading text while message is being fetched from the server
.backend(new Backend(_))
.render((_, S, B) => {
Panel(Panel.Props("Message of the day"), <.div(S.message),
Button(Button.Props(B.refresh, CommonStyle.danger), Icon.refresh, " Update")
)
})
.componentDidMount(scope => {
scope.backend.refresh()
})
.buildU
def apply() = Motd()
}