-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.elm
102 lines (79 loc) · 2.37 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
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
import Http.Extra as Http
import Task exposing (Task)
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Signal exposing (Signal, Mailbox)
import StartApp
import Effects exposing (Never, Effects)
import Json.Decode as Decode exposing (Value, (:=))
import Native.AsIs
type alias Model = Int
type alias ExampleAdder =
{ add : Int
}
type ServerCommand
= ConsoleLog
| Add Int
| Sub Int
| RecordAdd ExampleAdder
type Action
= SendCommand ServerCommand
| UpdateModel Model
| Noop
asIs : a -> Value
asIs =
Native.AsIs.asIs
init : (Model, Effects.Effects Action)
init = (0, Effects.none)
decodeModel : Decode.Decoder Model
decodeModel =
"model" := Decode.int
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case action of
Noop ->
(model, Effects.none)
SendCommand command ->
let
task =
Http.post "/api"
|> Http.withHeader "Content-Type" "application/json"
|> Http.withJsonBody (asIs command)
|> Http.send (Http.jsonReader decodeModel) Http.stringReader
|> Task.toMaybe
|> Task.map (\x ->
case x of
Just model ->
UpdateModel model.data
Nothing ->
Noop
)
in
(model, Effects.task task)
UpdateModel model ->
(model, Effects.none)
view : Signal.Address Action -> Model -> Html
view address model =
div
[]
[ div [ onClick address (SendCommand ConsoleLog) ] [ text "log" ]
, div [ onClick address (SendCommand (Add 1)) ] [ text "Add 1" ]
, div [ onClick address (SendCommand (Sub 1)) ] [ text "Sub 1" ]
, div [ onClick address (SendCommand (RecordAdd { add = 5 })) ] [ text "Sub 1" ]
, text ("current model: " ++ toString model)
]
app : StartApp.App Model
app =
StartApp.start
{ init = init
, update = update
, inputs = []
, view = view
}
main : Signal Html
main =
app.html
port tasks : Signal (Task.Task Never ())
port tasks =
app.tasks