summaryrefslogtreecommitdiffstats
path: root/src/Pages/Feed.elm
blob: a8482000ad7de70412abc30669a5e488f02fdf81 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Pages.Feed exposing (..)

import Css exposing (..)
import DateFormat.Relative exposing (relativeTime)
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css)
import Html.Styled.Events exposing (onClick)
import Time exposing (now)
import Types exposing (DisplayableError(..), Model, Msg(..), Post)


styles =
    { page =
        css
            [ maxWidth (px 600)
            , margin2 (px 10) auto
            ]
    , post =
        css
            [ padding2 (px 15) (px 20)
            , backgroundColor (hex "#eee")
            ]
    , postTitle =
        css
            [ margin (px 0) ]
    , postLikeButton =
        css
            [ marginRight (px 5)
            , cursor pointer
            ]
    }


derrorToHtml : DisplayableError -> Html Msg
derrorToHtml derror =
    case derror of
        DNoError ->
            text ""

        DHttpError httpErr ->
            text <| Debug.toString httpErr


derrorDiv : Model -> Html Msg
derrorDiv model =
    div []
        [ derrorToHtml model.displayError ]


postDiv : Time.Posix -> Post -> Html Msg
postDiv now post =
    div [ styles.post ]
        [ h3 [ styles.postTitle ] [ text post.author.name ]
        , i []
            [ text <| relativeTime now post.createdAt
            ]
        , p [] [ text post.content ]
        , span []
            [ button [ styles.postLikeButton, onClick (LikePost post) ] [ text "❤️ Like" ]
            , text <| String.fromInt post.likes
            ]
        ]


postsDiv : Model -> Html Msg
postsDiv model =
    div [] <| List.map (postDiv model.now) model.posts


headerSection : Model -> Html msg
headerSection _ =
    header []
        [ h1 [] [ text "Shoob book" ]
        ]


mainSection : Model -> Html Msg
mainSection model =
    main_ [] <|
        [ derrorDiv model
        , postsDiv model
        ]


body : Model -> Html Msg
body model =
    div [ styles.page ]
        [ headerSection model
        , mainSection model
        ]


feedView : Model -> List (Html Msg)
feedView model =
    [ body model ]