onasty/web/src/Pages/Dashboard.elm(view raw)
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
module Pages.Dashboard exposing (Model, Msg, page)
import Api exposing (Response(..))
import Api.Note
import Auth
import Components.Box
import Components.Form
import Components.Utils
import Data.Note exposing (Note)
import Effect exposing (Effect)
import Html as H exposing (Html)
import Html.Attributes as A
import Html.Events as E
import Layouts
import Page exposing (Page)
import Ports
import Route exposing (Route)
import Route.Path
import Shared
import Time exposing (Posix)
import Time.Format
import View exposing (View)
page : Auth.User -> Shared.Model -> Route () -> Page Model Msg
page _ shared _ =
Page.new
{ init = init
, update = update
, subscriptions = subscriptions
, view = view shared
}
|> Page.withLayout (\_ -> Layouts.Header {})
type alias Model =
{ notes : Api.Response (List Note)
, noteToDeleteSlug : Maybe String
, apiError : Maybe Api.Error
}
init : () -> ( Model, Effect Msg )
init () =
( { notes = Api.Loading
, noteToDeleteSlug = Nothing
, apiError = Nothing
}
, Api.Note.getAll { onResponse = ApiNotesResponded }
)
-- UPDATE
type Msg
= UserClickedCreateNewNote
| UserClickedViewNote String
| UserClickedDeleteNote String
| UserConfirmedDeleteion Bool
| ApiNotesResponded (Result Api.Error (List Note))
| ApiNoteDeleted (Result Api.Error ())
update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
case msg of
UserClickedCreateNewNote ->
( model, Effect.pushRoutePath Route.Path.Home_ )
UserClickedViewNote slug ->
( model, Effect.pushRoutePath (Route.Path.Secret_Slug_ { slug = slug }) )
UserClickedDeleteNote slug ->
( { model | noteToDeleteSlug = Just slug }
, Effect.confirmRequest "Are you sure you want to delete this note?"
)
UserConfirmedDeleteion ok ->
case ( ok, model.noteToDeleteSlug ) of
( True, Just slug ) ->
let
newNotes =
case model.notes of
Success notes ->
Success (List.filter (\n -> n.slug /= slug) notes)
_ ->
model.notes
in
( { model | notes = newNotes, noteToDeleteSlug = Nothing }
, Api.Note.delete { onResponse = ApiNoteDeleted, slug = slug }
)
_ ->
( { model | noteToDeleteSlug = Nothing }, Effect.none )
ApiNotesResponded (Ok notes) ->
( { model | notes = Api.Success notes }, Effect.none )
ApiNotesResponded (Err error) ->
( { model | notes = Api.Failure error }, Effect.none )
ApiNoteDeleted (Ok _) ->
( { model | apiError = Nothing }, Effect.none )
ApiNoteDeleted (Err err) ->
( { model | apiError = Just err }, Effect.none )
subscriptions : Model -> Sub Msg
subscriptions _ =
Ports.confirmResponse UserConfirmedDeleteion
-- VIEW
view : Shared.Model -> Model -> View Msg
view shared model =
let
timeFormat =
Time.Format.toString shared.timeZone
in
{ title = "Dashboard"
, body =
[ Components.Utils.commonContainer
[ H.div [ A.class "w-full max-w-6xl mx-auto" ]
[ H.div [ A.class "bg-white rounded-lg border border-gray-200 shadow-sm" ]
[ Components.Utils.viewMaybe model.apiError (\e -> Components.Box.error (Api.errorMessage e))
, viewHeader
, H.div [ A.class "p-6" ] [ viewNotes model.notes timeFormat ]
]
]
]
]
}
viewCreateNoteButton : Html Msg
viewCreateNoteButton =
Components.Form.button
{ text = "Create New Note"
, onClick = UserClickedCreateNewNote
, style = Components.Form.PrimaryReverse True
, disabled = False
}
viewHeader : Html Msg
viewHeader =
H.div [ A.class "p-6 pb-4 border-b border-gray-200" ]
[ H.div [ A.class "flex justify-between items-start" ]
[ H.div []
[ H.h1 [ A.class "text-2xl font-bold text-gray-900" ] [ H.text "My notes" ]
, H.p [ A.class "text-gray-600 mt-2" ] [ H.text "Manage and organize all your created notes" ]
]
, H.div [] [ viewCreateNoteButton ]
]
]
viewNotes : Api.Response (List Note) -> (Posix -> String) -> Html Msg
viewNotes apiResp timeFormat =
case apiResp of
Success notes ->
if List.isEmpty notes then
viewEmptyNoteList
else
H.div [ A.class "space-y-4" ]
[ H.div [ A.class "pb-2 border-b border-gray-200" ]
[ H.span [ A.class "text-sm text-gray-600" ] [ H.text (String.fromInt (List.length notes) ++ " note(s) ") ] ]
, H.div [] (List.map (\n -> viewNoteCard n timeFormat) notes)
]
Failure err ->
H.text ("Something went wrong: " ++ Api.errorMessage err)
Loading ->
H.text "Loading notes"
viewNoteCard : Note -> (Posix -> String) -> Html Msg
viewNoteCard note timeFormat =
let
viewNoteTime text maybeTime =
Components.Utils.viewMaybe maybeTime
(\r ->
H.div [ A.class "flex items-center" ]
[ H.p []
[ H.span [ A.class "font-bold" ] [ H.text text ]
, H.span [] [ H.text (timeFormat r) ]
]
]
)
viewNoteBadges text cond colorClasses =
Components.Utils.viewIf cond
(H.span
[ A.class ("inline-flex items-center gap-1 px-2 py-1 text-xs rounded-full " ++ colorClasses) ]
[ H.span [] [ H.text text ] ]
)
in
H.div
[ A.class
(if note.readAt /= Nothing then
"border rounded-lg p-4 border-red-200 bg-red-50"
else
"border rounded-lg p-4 border-gray-200 hover:border-gray-300 transition-colors"
)
]
[ H.div [ A.class "flex items-start justify-between" ]
[ H.div [ A.class "flex-1 min-w-0" ]
[ H.p [ A.class "text-gray-700 text-sm mb-3" ] [ H.text (truncateContent note.content) ]
, H.div [ A.class "flex flex-wrap items-center gap-4 text-xs text-gray-500 mb-2" ]
[ H.div [ A.class "items-center" ]
[ H.p []
[ H.span [ A.class "font-bold" ] [ H.text "Created " ]
, H.span [] [ H.text (timeFormat note.createdAt) ]
]
, viewNoteTime "Read " note.readAt
, viewNoteTime "Expires " note.expiresAt
]
]
, H.div [ A.class "flex flex-wrap gap-2" ]
[ viewNoteBadges "Burn after reading" note.keepBeforeExpiration "bg-orange-100 text-orange-800"
, viewNoteBadges "Has password" note.hasPassword "bg-blue-100 text-blue-800"
, viewNoteBadges "Read" (note.readAt /= Nothing) "bg-red-100 text-red-100"
]
]
, H.div [ A.class "flex items-center gap-2 ml-4" ]
[ H.button
[ A.class "p-2 text-gray-400 hover:text-gray-600 bg-gray-50 hover:bg-gray-100 rounded-md transition-colors"
, E.onClick (UserClickedViewNote note.slug)
, A.title "View note"
, A.type_ "button"
]
[ H.text "👁️" ]
, H.button
[ A.class "p-2 text-gray-400 text-red-300 hover:text-red-600 bg-red-50 hover:bg-red-100 rounded-md transition-colors disabled:opacity-50"
, E.onClick (UserClickedDeleteNote note.slug)
, A.title "Delete note"
, A.type_ "button"
]
[ H.text "🗑️" ]
]
]
]
truncateContent : String -> String
truncateContent content =
if String.isEmpty content then
"<DELETED NOTE>"
else if String.length content <= 150 then
content
else
String.left 150 content ++ "..."
viewEmptyNoteList : Html msg
viewEmptyNoteList =
H.text "No notes found"
|