onasty/web/src/Layouts/Header.elm (view raw)
Olexandr Smirnov
Olexandr Smirnov
ss2316544@gmail.com web: general refactor (#158)..., 11 months ago
ss2316544@gmail.com web: general refactor (#158)..., 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 | let |
| 97 | viewLink text path = |
| 98 | H.a [ A.class "text-gray-600 hover:text-black transition-colors", Route.Path.href path ] |
| 99 | [ H.text text ] |
| 100 | in |
| 101 | case user of |
| 102 | Auth.User.SignedIn _ -> |
| 103 | [ viewLink "Profile" Route.Path.Profile_Me |
| 104 | , H.button |
| 105 | [ A.class "text-gray-600 hover:text-red-600 transition-colors" |
| 106 | , E.onClick UserClickedLogout |
| 107 | ] |
| 108 | [ H.text "Logout" ] |
| 109 | ] |
| 110 | |
| 111 | _ -> |
| 112 | [ viewLink "About" Route.Path.Home_ -- TODO: or add about page, or delete the link |
| 113 | , H.a |
| 114 | [ A.class "px-4 py-2 border border-gray-300 rounded-md text-black hover:bg-gray-50 transition-colors" |
| 115 | , Route.Path.href Route.Path.Auth |
| 116 | ] |
| 117 | [ H.text "Sign In/Up" ] |
| 118 | ] |