aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/DecodeFlags.elm
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-09-13 21:48:04 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-09-13 21:48:04 +0200
commit5348b220c8ec071eee01578f0ea525e28d3a5c3f (patch)
tree29725415cc884227081c44b832d3daff46c9068d /src/client/elm/DecodeFlags.elm
parent83034116b02497576d97bffc9e91a5476cc60d41 (diff)
Mode DecodeFlags and Model to seperate files
Diffstat (limited to 'src/client/elm/DecodeFlags.elm')
-rw-r--r--src/client/elm/DecodeFlags.elm54
1 files changed, 54 insertions, 0 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 ++ "?")
+ )