summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-11-05 09:29:02 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-11-05 09:29:02 +0200
commitf07cf83e6b3e25e5957e4c813b50203f970d4f54 (patch)
tree3f8164ab25d9e3d7bdf8e1442611406e2789c085
parentb8cdee378647f81bbbbc5aafd4c58b1e15cb0192 (diff)
Refactor JSON codeHEADmain
-rw-r--r--src/JsonDecode.elm36
-rw-r--r--src/JsonEncode.elm38
-rw-r--r--src/Main.elm76
3 files changed, 80 insertions, 70 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)
diff --git a/src/JsonEncode.elm b/src/JsonEncode.elm
new file mode 100644
index 0000000..8356e6f
--- /dev/null
+++ b/src/JsonEncode.elm
@@ -0,0 +1,38 @@
+module JsonEncode exposing (..)
+
+import Json.Encode exposing (..)
+import Json.Encode.Extra exposing (..)
+import Time
+import Types exposing (Model, Post)
+
+
+composePostEncoder : Model -> Value
+composePostEncoder model =
+ let
+ imageUrl : Maybe String
+ imageUrl =
+ if model.composeImageInputValue /= "" then
+ Just model.composeImageInputValue
+
+ else
+ Nothing
+ in
+ object
+ [ ( "content", string model.composeTextInputValue )
+ , ( "createdAt", Json.Encode.int <| Time.posixToMillis model.now // 1000 )
+ , ( "likes", Json.Encode.int 0 )
+ , ( "userPictureUrl", Json.Encode.string model.userData.pictureUrl )
+ , ( "imageUrl", maybe Json.Encode.string imageUrl )
+ , ( "author"
+ , Json.Encode.object
+ [ ( "id", Json.Encode.string model.userData.email )
+ , ( "name", Json.Encode.string model.userData.name )
+ ]
+ )
+ ]
+
+
+likePostEncoder : Post -> Value
+likePostEncoder post =
+ Json.Encode.object
+ [ ( "likes", Json.Encode.int (post.likes + 1) ) ]
diff --git a/src/Main.elm b/src/Main.elm
index 75bc498..0a40474 100644
--- a/src/Main.elm
+++ b/src/Main.elm
@@ -6,18 +6,17 @@ import Config exposing (makeApiUrl)
import Header exposing (headerView)
import Html.Styled exposing (..)
import Http
-import Json.Decode exposing (Decoder, field, int, list, map2, map7, maybe, string)
-import Json.Encode
-import Json.Encode.Extra
+import JsonDecode exposing (postDecoder, postsDecoder)
+import JsonEncode exposing (composePostEncoder, likePostEncoder)
import Pages.Feed exposing (feedView)
import Pages.Profile exposing (profileView)
import RemoteData
import Routes exposing (routeParser)
import Task
import Time
-import Types exposing (Author, Flags, Model, Msg(..), Post, Route(..))
+import Types exposing (Flags, Model, Msg(..), Post, Route(..))
import Url exposing (Url)
-import Url.Parser exposing ((</>), Parser, oneOf, s)
+import Url.Parser
import Utils exposing (httpErrorToString, listFlat)
@@ -179,11 +178,7 @@ likePost model post =
{ method = "PATCH"
, headers = []
, url = makeApiUrl model (Config.ApiLikePost post.id)
- , body =
- Http.jsonBody
- (Json.Encode.object
- [ ( "likes", Json.Encode.int (post.likes + 1) ) ]
- )
+ , body = Http.jsonBody (likePostEncoder post)
, expect = Http.expectJson LikedPost postDecoder
, timeout = Nothing
, tracker = Nothing
@@ -192,34 +187,10 @@ likePost model post =
composePost : Model -> Cmd Msg
composePost model =
- let
- imageUrl : Maybe String
- imageUrl =
- if model.composeImageInputValue /= "" then
- Just model.composeImageInputValue
-
- else
- Nothing
- in
Http.post
{ url = makeApiUrl model Config.ApiCompose
, expect = Http.expectJson ComposedPost postDecoder
- , body =
- Http.jsonBody
- (Json.Encode.object
- [ ( "content", Json.Encode.string model.composeTextInputValue )
- , ( "createdAt", Json.Encode.int <| Time.posixToMillis model.now // 1000 )
- , ( "likes", Json.Encode.int 0 )
- , ( "userPictureUrl", Json.Encode.string model.userData.pictureUrl )
- , ( "imageUrl", Json.Encode.Extra.maybe Json.Encode.string imageUrl )
- , ( "author"
- , Json.Encode.object
- [ ( "id", Json.Encode.string model.userData.email )
- , ( "name", Json.Encode.string model.userData.name )
- ]
- )
- ]
- )
+ , body = Http.jsonBody (composePostEncoder model)
}
@@ -232,41 +203,6 @@ subscriptions _ =
--- JSON
-
-
-decodeTime : Decoder Time.Posix
-decodeTime =
- int
- |> Json.Decode.andThen
- (\ms ->
- Json.Decode.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)
-
-
-
-- VIEW