summaryrefslogtreecommitdiffstats
path: root/src/JsonDecode.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/JsonDecode.elm')
-rw-r--r--src/JsonDecode.elm36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/JsonDecode.elm b/src/JsonDecode.elm
new file mode 100644
index 0000000..0ec0ba8
--- /dev/null
+++ b/src/JsonDecode.elm
@@ -0,0 +1,36 @@
+module JsonDecode exposing (..)
+
+import Json.Decode exposing (..)
+import Time
+import Types exposing (Author, Post)
+
+
+decodeTime : Decoder Time.Posix
+decodeTime =
+ int
+ |> andThen
+ (\ms ->
+ succeed <| Time.millisToPosix (ms * 1000)
+ )
+
+
+postsDecoder : Decoder (List Post)
+postsDecoder =
+ list postDecoder
+
+
+postDecoder : Decoder Post
+postDecoder =
+ map7 Post
+ (field "id" string)
+ (field "content" string)
+ (field "author"
+ (map2 Author
+ (field "id" string)
+ (field "name" string)
+ )
+ )
+ (field "createdAt" decodeTime)
+ (field "likes" int)
+ (field "userPictureUrl" string)
+ (maybe <| field "imageUrl" string)