This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
FormDataExample.scala
109 lines (100 loc) · 3.49 KB
/
FormDataExample.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import korolev._
import korolev.akka.{AkkaHttpServerConfig, SimpleAkkaHttpKorolevApp}
import korolev.akka._
import korolev.server._
import scala.concurrent.ExecutionContext.Implicits.global
import korolev.state.javaSerialization._
import korolev.web.FormData
import levsha.XmlNs
import scala.concurrent.Future
object FormDataExample extends SimpleAkkaHttpKorolevApp(AkkaHttpServerConfig(maxRequestBodySize = 20 * 1024 * 1024)) {
import State.globalContext._
import levsha.dsl._
import html._
val role = AttrDef(XmlNs.html, "role")
val myForm = elementId()
val pictureFieldName = "picture"
val textFieldName = "text"
val multiLineText = "multiLineText"
val service = akkaHttpService{
KorolevServiceConfig[Future, State, Any](
stateLoader = StateLoader.default(State()),
document = { state =>
Html(
head(
link(
rel :="stylesheet",
href :="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css",
integrity := "sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ",
crossorigin := "anonymous"
),
style("body { margin: 2em }"),
script(
src := "https://code.jquery.com/jquery-3.1.1.slim.min.js",
integrity := "sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n",
crossorigin := "anonymous"
),
script(
src := "https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js",
integrity := "sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb",
crossorigin := "anonymous"
),
script(
src := "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js",
integrity := "sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn",
crossorigin := "anonymous"
)
),
body (
form (`class` := "card",
myForm,
div (
`class` := "card-block",
legend ("FormData Example"),
p (
label ("The text"),
input (`type` := "text", name := textFieldName)
),
p (
label ("The text area"),
textarea (name := multiLineText)
),
p (
button ("Submit")
)
),
event("submit") { access =>
for {
formData <- access.downloadFormData(myForm)
_ <- access.resetForm(myForm)
_ <- access.transition(_ => State(Some(formData), None))
} yield ()
}
),
state.formData match {
case Some(formData) =>
table(
tbody(
formData.content map { entry =>
tr(
td(entry.name),
td(entry.asString)
)
}
)
)
case None =>
div()
}
)
)
},
maxFormDataEntrySize = 1024 * 1024 * 20
)
}
}
case class State(formData: Option[FormData] = None,
error: Option[String] = None)
object State {
val globalContext = Context[Future, State, Any]
}