onasty/web/src/Layouts/Header.elm (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com refactor(web): make use of types for handling auth (#137)..., 11 months ago
ss2316544@gmail.com refactor(web): make use of types for handling auth (#137)..., 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 exposing (Html) |
| 6 | import Html.Attributes as Attr |
| 7 | import Html.Events |
| 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 | [ viewNavbar shared |> Html.map toContentMsg |
| 71 | , Html.main_ [] content.body |
| 72 | ] |
| 73 | } |
| 74 | |
| 75 | |
| 76 | viewNavbar : Shared.Model -> Html Msg |
| 77 | viewNavbar shared = |
| 78 | Html.header [ Attr.class "navbar" ] |
| 79 | [ Html.nav [ Attr.class "f-row justify-content:space-between" ] |
| 80 | [ Html.ul [ Attr.attribute "role" "list" ] |
| 81 | [ Html.li [] [ viewNavLink ( "home", Route.Path.Home_ ) ] ] |
| 82 | , Html.ul [ Attr.attribute "role" "list" ] |
| 83 | (case shared.user of |
| 84 | Auth.User.SignedIn _ -> |
| 85 | [ Html.li [] [ viewNavLink ( "profile", Route.Path.Profile_Me ) ] |
| 86 | , Html.li [] [ Html.a [ Html.Events.onClick UserClickedLogout ] [ Html.text "logout" ] ] |
| 87 | ] |
| 88 | |
| 89 | Auth.User.NotSignedIn -> |
| 90 | viewNotSignedInNav |
| 91 | |
| 92 | Auth.User.RefreshingTokens -> |
| 93 | viewNotSignedInNav |
| 94 | ) |
| 95 | ] |
| 96 | ] |
| 97 | |
| 98 | |
| 99 | viewNotSignedInNav : List (Html msg) |
| 100 | viewNotSignedInNav = |
| 101 | [ Html.li [] [ viewNavLink ( "sign in", Route.Path.Auth ) ] |
| 102 | ] |
| 103 | |
| 104 | |
| 105 | viewNavLink : ( String, Route.Path.Path ) -> Html msg |
| 106 | viewNavLink ( label, path ) = |
| 107 | Html.a |
| 108 | [ Route.Path.href path ] |
| 109 | [ Html.text label ] |