-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
69 lines (52 loc) · 1.41 KB
/
Main.elm
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
import Html exposing (Html, button, div, text, input, label)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
main =
Html.beginnerProgram { model = model, view = view, update = update }
-- MODEL
type alias Model =
{ message : String
, sendButton : SendMessage
}
type SendMessage
= NotDone
| Error String
| Sended String
-- UPDATE
type Msg
= Message String
| Submit
model : Model
model = { message = "", sendButton = NotDone }
update : Msg -> Model -> Model
update action model =
case action of
Message message ->
{ model | message = message }
Submit ->
{ model | sendButton = validate model}
validate : Model -> SendMessage
validate model =
if (String.length model.message) < 1 then
Error "Please enter a message"
else
Sended model.message
-- VIEW
view : Model -> Html Msg
view model =
div []
[ label [ for "message-box" ] [ text "Entrez un message" ]
, input [ id "message-box", type_ "text", placeholder "Message", onInput Message ] []
, button [ onClick Submit ] [ text "submit" ]
, viewValidation model
]
viewValidation : Model -> Html msg
viewValidation model =
let
(color, message) =
case model.sendButton of
NotDone -> ("", "")
Error message -> ("red", message)
Sended message -> ("green", message)
in
div [ style [("color", color)] ] [ text message ]