-
Notifications
You must be signed in to change notification settings - Fork 2
/
Class.elm
39 lines (28 loc) · 870 Bytes
/
Class.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
port module Class exposing (..)
import Dict exposing (Dict, union, empty, get)
import Json.Decode exposing (dict, string, Decoder, Value, decodeValue)
type Msg = ReceiveClass Model
type alias Model = Dict String String
port fetchClasses : String -> Cmd msg
port receiveClasses : (Value -> msg) -> Sub msg
subscriptions : Sub Msg
subscriptions = receiveClasses (\(v) -> ReceiveClass (getDecodedResult v))
decoder : Decoder Model
decoder = dict string
getDecodedResult : Value -> Model
getDecodedResult v =
case decodeValue decoder v of
Ok val -> val
Err _ -> empty
update : Msg -> Model -> (Model, Cmd Msg)
update message model =
case message of
ReceiveClass d ->
(union d model, Cmd.none)
getClass : String -> Model -> String
getClass key dict =
case get key dict of
Just class -> class
Nothing -> ""
init : Model
init = empty