summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Main.elm49
-rw-r--r--src/Pages/Feed.elm8
-rw-r--r--src/Types.elm1
-rw-r--r--src/index.html22
4 files changed, 53 insertions, 27 deletions
diff --git a/src/Main.elm b/src/Main.elm
index bd145e0..9d55d9c 100644
--- a/src/Main.elm
+++ b/src/Main.elm
@@ -4,6 +4,7 @@ import Browser exposing (Document)
import Browser.Navigation exposing (Key)
import Config exposing (makeApiUrl)
import Html.Styled exposing (..)
+import Html.Styled.Events exposing (onClick)
import Http
import Json.Decode exposing (Decoder, field, int, list, map2, map5, string)
import Json.Encode
@@ -33,6 +34,9 @@ main =
port showAlert : String -> Cmd msg
+port requestLogout : () -> Cmd msg
+
+
-- INIT
@@ -60,6 +64,9 @@ update msg model =
SetNowPosix now ->
( { model | now = now }, Cmd.none )
+ RequestLogout ->
+ ( model, requestLogout () )
+
GetPosts ->
( model, getPosts )
@@ -68,7 +75,7 @@ update msg model =
GotPosts (Result.Err httpErr) ->
( model
- , showAlert (Debug.toString httpErr)
+ , showAlert <| httpErrorToString httpErr
)
LikePost post ->
@@ -81,7 +88,7 @@ update msg model =
LikedPost (Result.Err httpErr) ->
( model
- , showAlert (Debug.toString httpErr)
+ , showAlert <| httpErrorToString httpErr
)
ComposeInputChanged value ->
@@ -106,7 +113,7 @@ update msg model =
ComposedPost (Result.Err httpErr) ->
( model
- , showAlert (Debug.toString httpErr)
+ , showAlert <| httpErrorToString httpErr
)
@@ -215,5 +222,39 @@ postDecoder =
view : Model -> Document Msg
view model =
{ title = "Shoob book"
- , body = feedView model |> List.map toUnstyled
+ , body = button [ onClick RequestLogout ] [ text "Logout" ] :: feedView model |> List.map toUnstyled
}
+
+
+
+-- UTILS
+
+
+httpErrorToString : Http.Error -> String
+httpErrorToString err =
+ case err of
+ Http.BadUrl url ->
+ "URL {0} is invalid" |> templ [ url ]
+
+ Http.Timeout ->
+ "Request has timed out"
+
+ Http.NetworkError ->
+ "Unable to reach the server, check your network connection"
+
+ Http.BadStatus status ->
+ "Server responded with status {0}" |> templ [ String.fromInt status ]
+
+ Http.BadBody msg ->
+ msg
+
+
+templ : List String -> String -> String
+templ rs original =
+ let
+ templElement_ : ( Int, String ) -> String -> String
+ templElement_ ( index, r ) orig_ =
+ String.replace ("{" ++ String.fromInt index ++ "}") r orig_
+ in
+ List.indexedMap Tuple.pair rs
+ |> List.foldl templElement_ original
diff --git a/src/Pages/Feed.elm b/src/Pages/Feed.elm
index 583c097..824552a 100644
--- a/src/Pages/Feed.elm
+++ b/src/Pages/Feed.elm
@@ -15,6 +15,12 @@ styles =
[ maxWidth (px 600)
, margin2 (px 10) auto
]
+ , postList =
+ css
+ [ overflow scroll
+ , maxHeight (vh 70)
+ , marginBottom (px 10)
+ ]
, post =
css
[ padding2 (px 15) (px 20)
@@ -60,7 +66,7 @@ postsDiv : Model -> Html Msg
postsDiv model =
case model.posts of
Just posts ->
- div [] <| List.map (postDiv model.now) posts
+ div [ styles.postList ] <| List.map (postDiv model.now) posts
Nothing ->
div [] [ text "Loading posts..." ]
diff --git a/src/Types.elm b/src/Types.elm
index 3736bbf..ce487b6 100644
--- a/src/Types.elm
+++ b/src/Types.elm
@@ -13,6 +13,7 @@ type alias Model =
type Msg
= NoOp
+ | RequestLogout
-- TIME
| SetNowPosix Time.Posix
-- POSTS
diff --git a/src/index.html b/src/index.html
deleted file mode 100644
index ccf508a..0000000
--- a/src/index.html
+++ /dev/null
@@ -1,22 +0,0 @@
-<html>
-
-<head>
- <meta charset="UTF-8">
- <title>Main</title>
- <link rel="stylesheet" href="main.css">
- </link>
- <script src="main.js"></script>
-</head>
-
-<body>
- <div id="app"></div>
- <script>
- var app = Elm.Main.init({
- node: document.getElementById("app")
- });
-
- app.ports.showAlert.subscribe((message) => window.alert(message));
- </script>
-</body>
-
-</html> \ No newline at end of file