mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
ui: Initial Elm UI import
Merged from https://github.com/jhillyerd/inbucket-elm Uses https://github.com/halfzebra/create-elm-app
This commit is contained in:
91
ui/src/Data/Session.elm
Normal file
91
ui/src/Data/Session.elm
Normal file
@@ -0,0 +1,91 @@
|
||||
module Data.Session
|
||||
exposing
|
||||
( Session
|
||||
, Persistent
|
||||
, Msg(..)
|
||||
, decoder
|
||||
, decodeValueWithDefault
|
||||
, init
|
||||
, none
|
||||
, update
|
||||
)
|
||||
|
||||
import Json.Decode as Decode exposing (..)
|
||||
import Json.Decode.Pipeline exposing (..)
|
||||
|
||||
|
||||
type alias Session =
|
||||
{ flash : String
|
||||
, routing : Bool
|
||||
, persistent : Persistent
|
||||
}
|
||||
|
||||
|
||||
type alias Persistent =
|
||||
{ recentMailboxes : List String
|
||||
}
|
||||
|
||||
|
||||
type Msg
|
||||
= None
|
||||
| SetFlash String
|
||||
| ClearFlash
|
||||
| DisableRouting
|
||||
| EnableRouting
|
||||
| AddRecent String
|
||||
|
||||
|
||||
init : Persistent -> Session
|
||||
init persistent =
|
||||
Session "" True persistent
|
||||
|
||||
|
||||
update : Msg -> Session -> Session
|
||||
update msg session =
|
||||
case msg of
|
||||
None ->
|
||||
session
|
||||
|
||||
SetFlash flash ->
|
||||
{ session | flash = flash }
|
||||
|
||||
ClearFlash ->
|
||||
{ session | flash = "" }
|
||||
|
||||
DisableRouting ->
|
||||
{ session | routing = False }
|
||||
|
||||
EnableRouting ->
|
||||
{ session | routing = True }
|
||||
|
||||
AddRecent mailbox ->
|
||||
if List.head session.persistent.recentMailboxes == Just mailbox then
|
||||
session
|
||||
else
|
||||
let
|
||||
recent =
|
||||
session.persistent.recentMailboxes
|
||||
|> List.filter ((/=) mailbox)
|
||||
|> List.take 7
|
||||
|> (::) mailbox
|
||||
|
||||
persistent =
|
||||
session.persistent
|
||||
in
|
||||
{ session | persistent = { persistent | recentMailboxes = recent } }
|
||||
|
||||
|
||||
none : Msg
|
||||
none =
|
||||
None
|
||||
|
||||
|
||||
decoder : Decoder Persistent
|
||||
decoder =
|
||||
decode Persistent
|
||||
|> optional "recentMailboxes" (list string) []
|
||||
|
||||
|
||||
decodeValueWithDefault : Value -> Persistent
|
||||
decodeValueWithDefault =
|
||||
Decode.decodeValue decoder >> Result.withDefault { recentMailboxes = [] }
|
||||
Reference in New Issue
Block a user