1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

ui: Add refresh button to mailbox page (#179)

`socketConnected` is not implemented, but will be used when we implement #92
This commit is contained in:
James Hillyerd
2020-09-21 20:11:32 -07:00
committed by GitHub
parent 4648d8e593
commit 407ae87a3b
2 changed files with 103 additions and 41 deletions

View File

@@ -1,6 +1,7 @@
module Effect exposing module Effect exposing
( Effect ( Effect
, addRecent , addRecent
, append
, batch , batch
, deleteMessage , deleteMessage
, disableRouting , disableRouting
@@ -68,6 +69,13 @@ type SessionEffect
| RoutingEnable | RoutingEnable
{-| Appends a new effect to a model/effect tuple.
-}
append : Effect msg -> ( a, Effect msg ) -> ( a, Effect msg )
append e ( model, effect ) =
( model, batch [ effect, e ] )
{-| Packs a List of Effects into a single Effect {-| Packs a List of Effects into a single Effect
-} -}
batch : List (Effect msg) -> Effect msg batch : List (Effect msg) -> Effect msg

View File

@@ -94,6 +94,7 @@ type alias Model =
{ session : Session { session : Session
, mailboxName : String , mailboxName : String
, state : State , state : State
, socketConnected : Bool
, bodyMode : Body , bodyMode : Body
, searchInput : String , searchInput : String
, promptPurge : Bool , promptPurge : Bool
@@ -107,6 +108,7 @@ init session mailboxName selection =
( { session = session ( { session = session
, mailboxName = mailboxName , mailboxName = mailboxName
, state = LoadingList selection , state = LoadingList selection
, socketConnected = False
, bodyMode = SafeHtmlBody , bodyMode = SafeHtmlBody
, searchInput = "" , searchInput = ""
, promptPurge = False , promptPurge = False
@@ -136,6 +138,7 @@ subscriptions _ =
type Msg type Msg
= ListLoaded (Result HttpUtil.Error (List MessageHeader)) = ListLoaded (Result HttpUtil.Error (List MessageHeader))
| ClickMessage MessageID | ClickMessage MessageID
| ClickRefresh
| ListKeyPress String Int | ListKeyPress String Int
| CloseMessage | CloseMessage
| MessageLoaded (Result HttpUtil.Error Message) | MessageLoaded (Result HttpUtil.Error Message)
@@ -165,6 +168,21 @@ update msg model =
] ]
) )
ClickRefresh ->
let
selection =
case model.state of
ShowingList _ (ShowingMessage message) ->
Just message.id
_ ->
Nothing
in
-- Reset to loading state, preserving the current message selection.
( { model | state = LoadingList selection }
, Effect.getHeaderList ListLoaded model.mailboxName
)
CloseMessage -> CloseMessage ->
case model.state of case model.state of
ShowingList list _ -> ShowingList list _ ->
@@ -191,23 +209,7 @@ update msg model =
( model, Effect.none ) ( model, Effect.none )
ListLoaded (Ok headers) -> ListLoaded (Ok headers) ->
case model.state of updateListLoaded model headers
LoadingList selection ->
let
newModel =
{ model
| state = ShowingList (MessageList headers Nothing "") NoMessage
}
in
case selection of
Just id ->
updateOpenMessage newModel id
Nothing ->
( newModel, Effect.addRecent newModel.mailboxName )
_ ->
( model, Effect.none )
ListLoaded (Err err) -> ListLoaded (Err err) ->
( model, Effect.showFlash (HttpUtil.errorFlash err) ) ( model, Effect.showFlash (HttpUtil.errorFlash err) )
@@ -262,6 +264,33 @@ update msg model =
( { model | now = now }, Effect.none ) ( { model | now = now }, Effect.none )
updateListLoaded : Model -> List MessageHeader -> ( Model, Effect Msg )
updateListLoaded model headers =
case model.state of
LoadingList selection ->
let
newModel =
{ model
| state = ShowingList (MessageList headers Nothing "") NoMessage
}
in
Effect.append (Effect.addRecent newModel.mailboxName) <|
case selection of
Just id ->
-- Don't try to load selected message if not present in headers.
if List.any (\header -> Just header.id == selection) headers then
updateOpenMessage newModel id
else
( newModel, Effect.updateRoute (Route.Mailbox model.mailboxName) )
Nothing ->
( newModel, Effect.none )
_ ->
( model, Effect.none )
{-| Replace the currently displayed message. {-| Replace the currently displayed message.
-} -}
updateMessageResult : Model -> Message -> ( Model, Effect Msg ) updateMessageResult : Model -> Message -> ( Model, Effect Msg )
@@ -412,10 +441,7 @@ updateMarkMessageSeen model =
updateOpenMessage : Model -> String -> ( Model, Effect Msg ) updateOpenMessage : Model -> String -> ( Model, Effect Msg )
updateOpenMessage model id = updateOpenMessage model id =
( updateSelected model id ( updateSelected model id
, Effect.batch , Effect.getMessage MessageLoaded model.mailboxName id
[ Effect.addRecent model.mailboxName
, Effect.getMessage MessageLoaded model.mailboxName id
]
) )
@@ -438,26 +464,7 @@ view model =
, modal = viewModal model.promptPurge , modal = viewModal model.promptPurge
, content = , content =
[ div [ class ("mailbox " ++ mode) ] [ div [ class ("mailbox " ++ mode) ]
[ aside [ class "message-list-controls" ] [ viewMessageListControls model
[ input
[ type_ "text"
, placeholder "search"
, Events.onInput OnSearchInput
, value model.searchInput
]
[]
, button
[ Events.onClick (OnSearchInput "")
, disabled (model.searchInput == "")
, alt "Clear Search"
]
[ i [ class "fas fa-times" ] [] ]
, button
[ Events.onClick PurgeMailboxPrompt
, alt "Purge Mailbox"
]
[ i [ class "fas fa-trash" ] [] ]
]
, viewMessageList model , viewMessageList model
, main_ , main_
[ class "message" ] [ class "message" ]
@@ -498,6 +505,53 @@ viewModal promptPurge =
Nothing Nothing
viewMessageListControls : Model -> Html Msg
viewMessageListControls model =
let
clearButton =
Just <|
button
[ Events.onClick (OnSearchInput "")
, disabled (model.searchInput == "")
, alt "Clear Search"
]
[ i [ class "fas fa-times" ] [] ]
purgeButton =
Just <|
button
[ Events.onClick PurgeMailboxPrompt
, alt "Purge Mailbox"
]
[ i [ class "fas fa-trash" ] [] ]
refreshButton =
if model.socketConnected then
Nothing
else
Just <|
button
[ Events.onClick ClickRefresh
, alt "Refresh Mailbox"
]
[ i [ class "fas fa-sync" ] [] ]
searchInput =
Just <|
input
[ type_ "text"
, placeholder "search"
, Events.onInput OnSearchInput
, value model.searchInput
]
[]
in
[ searchInput, clearButton, refreshButton, purgeButton ]
|> List.filterMap identity
|> aside [ class "message-list-controls" ]
viewMessageList : Model -> Html Msg viewMessageList : Model -> Html Msg
viewMessageList model = viewMessageList model =
aside [ class "message-list" ] <| aside [ class "message-list" ] <|