-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
138 lines (92 loc) · 2.52 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
port module Main exposing (..)
import Browser
import Event
import Html exposing (Html)
import Html.Attributes
import Html.Events
import Json.Decode
import Json.Encode
import Ports
-- MAIN
main : Program Flags Model Msg
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- PORTS
port sendMessage : Json.Encode.Value -> Cmd msg
port messageReceiver : (Json.Decode.Value -> msg) -> Sub msg
-- FLAGS
type alias Flags =
{}
-- INIT
type alias Model =
{ event : Event.Model
}
init : Flags -> ( Model, Cmd Msg )
init flags =
( { event = Event.defaults Nothing }
, Cmd.none
)
-- UPDATE
type Msg
= EventMsg Event.Msg
| SubmittedEvent
| ReceivedMessage Json.Decode.Value
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
EventMsg sub ->
let
( subModel, subCmd ) =
Event.update sub model.event
in
( { model | event = subModel }
, Cmd.map EventMsg subCmd
)
SubmittedEvent ->
case Event.validate model.event of
False ->
( model, Cmd.none )
True ->
( model
, sendMessage <|
Ports.encodeSendMessage "ADD_EVENT" (Event.encode model.event)
)
ReceivedMessage value ->
let
_ =
Debug.log "ReceivedMessage" (Json.Encode.encode 2 value)
in
case Json.Decode.decodeValue Ports.receivePayloadDecoder value of
Err err ->
let
_ =
Debug.log "Error decoding message" err
in
( model, Cmd.none )
Ok receivePayload ->
case receivePayload of
Ports.ClickedEvent event ->
( { model | event = event }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions _ =
messageReceiver ReceivedMessage
-- VIEW
view : Model -> Html Msg
view model =
Html.div
[]
[ Html.div
[ Html.Attributes.id "calendar" ]
[]
, Event.view model.event |> Html.map EventMsg
, Html.button
[ Html.Events.onClick SubmittedEvent
]
[ Html.text "Submit" ]
]