diff options
Diffstat (limited to 'src/client/elm')
-rw-r--r-- | src/client/elm/DecodeFlags.elm | 54 | ||||
-rw-r--r-- | src/client/elm/Main.elm | 73 | ||||
-rw-r--r-- | src/client/elm/Model.elm | 19 |
3 files changed, 75 insertions, 71 deletions
diff --git a/src/client/elm/DecodeFlags.elm b/src/client/elm/DecodeFlags.elm new file mode 100644 index 0000000..5acc4f9 --- /dev/null +++ b/src/client/elm/DecodeFlags.elm @@ -0,0 +1,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 ++ "?") + ) diff --git a/src/client/elm/Main.elm b/src/client/elm/Main.elm index de1f1fe..e59889d 100644 --- a/src/client/elm/Main.elm +++ b/src/client/elm/Main.elm @@ -1,9 +1,8 @@ module Main exposing (..) import Html exposing (..) -import Json.Decode exposing (Decoder, andThen, fail, string, succeed) -import Json.Decode.Pipeline exposing (decode, required) - +import Model exposing (Model) +import DecodeFlags exposing (Flags, init) main : Program Flags Model msg main = @@ -14,74 +13,6 @@ main = , subscriptions = subscriptions } - -type alias Flags = - Json.Decode.Value - - -type alias Model = - { users : List User - } - - -type alias User = - { type_ : UserType - , value : String - } - - -type UserType - = Class - | Teacher - | Room - | Student - - -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 ++ "?") - ) - - update : msg -> Model -> ( Model, Cmd msg ) update msg model = ( model, Cmd.none ) diff --git a/src/client/elm/Model.elm b/src/client/elm/Model.elm new file mode 100644 index 0000000..8f4c6a6 --- /dev/null +++ b/src/client/elm/Model.elm @@ -0,0 +1,19 @@ +module Model exposing (..) + + +type alias Model = + { users : List User + } + + +type alias User = + { type_ : UserType + , value : String + } + + +type UserType + = Class + | Teacher + | Room + | Student |