aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/DecodeFlags.elm
blob: 5acc4f9d350218c69bc9ca4648142140ab7131e7 (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
52
53
54
module DecodeFlags exposing (Flags, init)

import Json.Decode exposing (Decoder, andThen, fail, 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 Json.Decode.decodeValue decodeUsers flags of
        Ok user ->
            ( Model user, Cmd.none )

        Err err ->
            Debug.crash err


decodeUsers : Decoder (List User)
decodeUsers =
    Json.Decode.list decodeUser


decodeUser : Decoder User
decodeUser =
    decode User
        |> required "type" decodeUserType
        |> required "value" string


decodeUserType : Json.Decode.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 ++ "?")
            )