From f07cf83e6b3e25e5957e4c813b50203f970d4f54 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 5 Nov 2021 09:29:02 +0200 Subject: Refactor JSON code --- src/JsonDecode.elm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/JsonDecode.elm (limited to 'src/JsonDecode.elm') 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) -- cgit v1.3