summaryrefslogtreecommitdiffstats
path: root/src/JsonDecode.elm
blob: 0ec0ba863eec2b13989dab8f473d94e97ad76a39 (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
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)