all repos

onasty @ b59aa76595abf2171bde3a5a97551f1f008aa622

a one-time notes service

onasty/web/src/Layouts/Header.elm (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
web: switch to tailwind (#141)..., 11 months ago
1
module Layouts.Header exposing (Model, Msg, Props, layout)
2
3
import Auth.User
4
import Effect exposing (Effect)
5
import Html as H exposing (Html)
6
import Html.Attributes as A
7
import Html.Events as E
8
import Layout exposing (Layout)
9
import Route exposing (Route)
10
import Route.Path
11
import Shared
12
import View exposing (View)
13
14
15
type alias Props =
16
    {}
17
18
19
layout : Props -> Shared.Model -> Route () -> Layout () Model Msg contentMsg
20
layout _ shared _ =
21
    Layout.new
22
        { init = init
23
        , update = update
24
        , view = view shared
25
        , subscriptions = subscriptions
26
        }
27
28
29
30
-- MODEL
31
32
33
type alias Model =
34
    {}
35
36
37
init : () -> ( Model, Effect Msg )
38
init _ =
39
    ( {}, Effect.none )
40
41
42
43
-- UPDATE
44
45
46
type Msg
47
    = UserClickedLogout
48
49
50
update : Msg -> Model -> ( Model, Effect Msg )
51
update msg model =
52
    case msg of
53
        UserClickedLogout ->
54
            ( model, Effect.logout )
55
56
57
subscriptions : Model -> Sub Msg
58
subscriptions _ =
59
    Sub.none
60
61
62
63
-- VIEW
64
65
66
view : Shared.Model -> { toContentMsg : Msg -> contentMsg, content : View contentMsg, model : Model } -> View contentMsg
67
view shared { toContentMsg, content } =
68
    { title = content.title
69
    , body =
70
        [ viewHeader shared.user |> H.map toContentMsg
71
        , H.main_ [] content.body
72
        ]
73
    }
74
75
76
viewHeader : Auth.User.SignInStatus -> Html Msg
77
viewHeader user =
78
    H.header [ A.class "w-full border-b border-gray-200 bg-white" ]
79
        [ H.div [ A.class "max-w-7xl mx-auto px-4 sm:px-6 lg:px-8" ]
80
            [ H.div [ A.class "flex justify-between items-center h-16" ]
81
                [ H.div [ A.class "flex items-center" ]
82
                    [ H.a
83
                        [ A.class "text-lg font-semibold text-black hover:text-gray-700 transition-colors"
84
                        , Route.Path.href Route.Path.Home_
85
                        ]
86
                        [ H.text "Onasty" ]
87
                    ]
88
                , H.nav [ A.class "flex items-center space-x-6" ] (viewNav user)
89
                ]
90
            ]
91
        ]
92
93
94
viewNav : Auth.User.SignInStatus -> List (Html Msg)
95
viewNav user =
96
    case user of
97
        Auth.User.SignedIn _ ->
98
            viewSignedInNav
99
100
        Auth.User.NotSignedIn ->
101
            viewNotSignedInNav
102
103
        Auth.User.RefreshingTokens ->
104
            viewNotSignedInNav
105
106
107
viewSignedInNav : List (Html Msg)
108
viewSignedInNav =
109
    [ viewLink "Profile" Route.Path.Profile_Me
110
    , H.button
111
        [ A.class "text-gray-600 hover:text-red-600 transition-colors"
112
        , E.onClick UserClickedLogout
113
        ]
114
        [ H.text "Logout" ]
115
    ]
116
117
118
viewNotSignedInNav : List (Html Msg)
119
viewNotSignedInNav =
120
    -- TODO: or add about page, or delete the link
121
    [ viewLink "About" Route.Path.Home_
122
    , H.a
123
        [ A.class "px-4 py-2 border border-gray-300 rounded-md text-black hover:bg-gray-50 transition-colors"
124
        , Route.Path.href Route.Path.Auth
125
        ]
126
        [ H.text "Sign In/Up" ]
127
    ]
128
129
130
viewLink : String -> Route.Path.Path -> Html Msg
131
viewLink text path =
132
    H.a
133
        [ A.class "text-gray-600 hover:text-black transition-colors"
134
        , Route.Path.href path
135
        ]
136
        [ H.text text ]