aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/DecodeFlags.elm
blob: 7709e5ddb9a9510aa078891300203f772c1f0bce (plain)
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
module DecodeFlags exposing (Flags, init)

import Json.Decode exposing (Decoder, andThen, decodeValue, fail, list, string, succeed)
import Json.Decode.Pipeline exposing (decode, required)
import Model exposing (..)


type alias Flags =
    Json.Decode.Value


init : Flags -> ( Model, Cmd msg )
init flags =
    case decodeValue decodeUsers flags of
        Ok user ->
            ( Model user, Cmd.none )

        Err err ->
            Debug.crash err


decodeUsers : Decoder (List User)
decodeUsers =
    list
        (decode User
            |> required "type" decodeUserType
            |> required "value" string
        )


decodeUserType : Decoder UserType
decodeUserType =
    string
        |> andThen
            (\s ->
                case s of
                    "s" ->
                        succeed Student

                    "c" ->
                        succeed Class

                    "t" ->
                        succeed Teacher

                    "r" ->
                        succeed Room

                    _ ->
                        fail ("What the f*ck is " ++ s ++ "?")
            )