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

ui: Add search to Mailbox

This commit is contained in:
James Hillyerd
2018-11-04 18:21:02 -08:00
parent c8dabf8593
commit f0b4dda8e6

View File

@@ -5,7 +5,19 @@ import Data.MessageHeader as MessageHeader exposing (MessageHeader)
import Data.Session as Session exposing (Session) import Data.Session as Session exposing (Session)
import Json.Decode as Decode exposing (Decoder) import Json.Decode as Decode exposing (Decoder)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, classList, downloadAs, href, id, property, target) import Html.Attributes
exposing
( class
, classList
, downloadAs
, href
, id
, placeholder
, property
, target
, type_
, value
)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http exposing (Error) import Http exposing (Error)
import HttpUtil import HttpUtil
@@ -52,12 +64,13 @@ type alias Model =
{ mailboxName : String { mailboxName : String
, state : State , state : State
, bodyMode : Body , bodyMode : Body
, searchInput : String
} }
init : String -> Maybe MessageID -> Model init : String -> Maybe MessageID -> Model
init mailboxName selection = init mailboxName selection =
Model mailboxName (LoadingList selection) SafeHtmlBody Model mailboxName (LoadingList selection) SafeHtmlBody ""
load : String -> Cmd Msg load : String -> Cmd Msg
@@ -91,7 +104,6 @@ subscriptions model =
type Msg type Msg
= ClickMessage MessageID = ClickMessage MessageID
| ViewMessage MessageID
| DeleteMessage Message | DeleteMessage Message
| DeleteMessageResult (Result Http.Error ()) | DeleteMessageResult (Result Http.Error ())
| ListResult (Result Http.Error (List MessageHeader)) | ListResult (Result Http.Error (List MessageHeader))
@@ -99,7 +111,9 @@ type Msg
| MessageResult (Result Http.Error Message) | MessageResult (Result Http.Error Message)
| MessageBody Body | MessageBody Body
| OpenedTime Time | OpenedTime Time
| SearchInput String
| Tick Time | Tick Time
| ViewMessage MessageID
update : Session -> Msg -> Model -> ( Model, Cmd Msg, Session.Msg ) update : Session -> Msg -> Model -> ( Model, Cmd Msg, Session.Msg )
@@ -195,6 +209,39 @@ update session msg model =
MessageBody bodyMode -> MessageBody bodyMode ->
( { model | bodyMode = bodyMode }, Cmd.none, Session.none ) ( { model | bodyMode = bodyMode }, Cmd.none, Session.none )
SearchInput searchInput ->
let
updateList list =
{ list
| searchFilter =
if String.length searchInput > 1 then
searchInput
else
""
}
updateModel state =
( { model | searchInput = searchInput, state = state }
, Cmd.none
, Session.none
)
in
case model.state of
LoadingList _ ->
( model, Cmd.none, Session.none )
ShowingList list selection ->
updateModel (ShowingList (updateList list) selection)
LoadingMessage list id ->
updateModel (LoadingMessage (updateList list) id)
ShowingMessage list visible ->
updateModel (ShowingMessage (updateList list) visible)
Transitioning list visible id ->
updateModel (Transitioning (updateList list) visible id)
OpenedTime time -> OpenedTime time ->
case model.state of case model.state of
ShowingMessage list visible -> ShowingMessage list visible ->
@@ -348,7 +395,9 @@ view : Session -> Model -> Html Msg
view session model = view session model =
div [ id "page", class "mailbox" ] div [ id "page", class "mailbox" ]
[ aside [ id "message-list" ] [ aside [ id "message-list" ]
[ case model.state of [ div []
[ input [ type_ "search", placeholder "search", onInput SearchInput, value model.searchInput ] [] ]
, case model.state of
LoadingList _ -> LoadingList _ ->
div [] [] div [] []
@@ -387,7 +436,12 @@ view session model =
messageList : MessageList -> Maybe MessageID -> Html Msg messageList : MessageList -> Maybe MessageID -> Html Msg
messageList list selected = messageList list selected =
div [] (List.map (messageChip selected) (List.reverse list.headers)) div []
(list
|> filterMessageList
|> List.reverse
|> List.map (messageChip selected)
)
messageChip : Maybe MessageID -> MessageHeader -> Html Msg messageChip : Maybe MessageID -> MessageHeader -> Html Msg
@@ -495,3 +549,23 @@ attachmentRow baseUrl attach =
] ]
, td [] [ a [ href url, downloadAs attach.fileName, class "button" ] [ text "Download" ] ] , td [] [ a [ href url, downloadAs attach.fileName, class "button" ] [ text "Download" ] ]
] ]
-- UTILITY
filterMessageList : MessageList -> List MessageHeader
filterMessageList list =
if list.searchFilter == "" then
list.headers
else
let
searchFilter =
String.toLower list.searchFilter
matches header =
String.contains searchFilter (String.toLower header.subject)
|| String.contains searchFilter (String.toLower header.from)
in
List.filter matches list.headers