mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 01:57:02 +00:00
ui: Add session update logic into Session
This commit is contained in:
@@ -10,8 +10,10 @@ module Data.Session exposing
|
|||||||
)
|
)
|
||||||
|
|
||||||
import Browser.Navigation as Nav
|
import Browser.Navigation as Nav
|
||||||
import Json.Decode exposing (..)
|
import Json.Decode as D
|
||||||
import Json.Decode.Pipeline exposing (..)
|
import Json.Decode.Pipeline exposing (..)
|
||||||
|
import Json.Encode as E
|
||||||
|
import Ports
|
||||||
import Url exposing (Url)
|
import Url exposing (Url)
|
||||||
|
|
||||||
|
|
||||||
@@ -43,40 +45,51 @@ init key location persistent =
|
|||||||
Session key location.host "" True persistent
|
Session key location.host "" True persistent
|
||||||
|
|
||||||
|
|
||||||
update : Msg -> Session -> Session
|
update : Msg -> Session -> ( Session, Cmd a )
|
||||||
update msg session =
|
update msg session =
|
||||||
case msg of
|
let
|
||||||
None ->
|
newSession =
|
||||||
session
|
case msg of
|
||||||
|
None ->
|
||||||
|
session
|
||||||
|
|
||||||
SetFlash flash ->
|
SetFlash flash ->
|
||||||
{ session | flash = flash }
|
{ session | flash = flash }
|
||||||
|
|
||||||
ClearFlash ->
|
ClearFlash ->
|
||||||
{ session | flash = "" }
|
{ session | flash = "" }
|
||||||
|
|
||||||
DisableRouting ->
|
DisableRouting ->
|
||||||
{ session | routing = False }
|
{ session | routing = False }
|
||||||
|
|
||||||
EnableRouting ->
|
EnableRouting ->
|
||||||
{ session | routing = True }
|
{ session | routing = True }
|
||||||
|
|
||||||
AddRecent mailbox ->
|
AddRecent mailbox ->
|
||||||
if List.head session.persistent.recentMailboxes == Just mailbox then
|
if List.head session.persistent.recentMailboxes == Just mailbox then
|
||||||
session
|
session
|
||||||
|
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
recent =
|
recent =
|
||||||
session.persistent.recentMailboxes
|
session.persistent.recentMailboxes
|
||||||
|> List.filter ((/=) mailbox)
|
|> List.filter ((/=) mailbox)
|
||||||
|> List.take 7
|
|> List.take 7
|
||||||
|> (::) mailbox
|
|> (::) mailbox
|
||||||
|
|
||||||
persistent =
|
persistent =
|
||||||
session.persistent
|
session.persistent
|
||||||
in
|
in
|
||||||
{ session | persistent = { persistent | recentMailboxes = recent } }
|
{ session | persistent = { persistent | recentMailboxes = recent } }
|
||||||
|
in
|
||||||
|
if session.persistent == newSession.persistent then
|
||||||
|
-- No change
|
||||||
|
( newSession, Cmd.none )
|
||||||
|
|
||||||
|
else
|
||||||
|
( newSession
|
||||||
|
, Ports.storeSession (encode newSession.persistent)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
none : Msg
|
none : Msg
|
||||||
@@ -84,12 +97,19 @@ none =
|
|||||||
None
|
None
|
||||||
|
|
||||||
|
|
||||||
decoder : Decoder Persistent
|
decoder : D.Decoder Persistent
|
||||||
decoder =
|
decoder =
|
||||||
succeed Persistent
|
D.succeed Persistent
|
||||||
|> optional "recentMailboxes" (list string) []
|
|> optional "recentMailboxes" (D.list D.string) []
|
||||||
|
|
||||||
|
|
||||||
decodeValueWithDefault : Value -> Persistent
|
decodeValueWithDefault : D.Value -> Persistent
|
||||||
decodeValueWithDefault =
|
decodeValueWithDefault =
|
||||||
decodeValue decoder >> Result.withDefault { recentMailboxes = [] }
|
D.decodeValue decoder >> Result.withDefault { recentMailboxes = [] }
|
||||||
|
|
||||||
|
|
||||||
|
encode : Persistent -> E.Value
|
||||||
|
encode persistent =
|
||||||
|
E.object
|
||||||
|
[ ( "recentMailboxes", E.list E.string persistent.recentMailboxes )
|
||||||
|
]
|
||||||
|
|||||||
@@ -224,20 +224,12 @@ changeRouteTo route model =
|
|||||||
updateSession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
|
updateSession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
|
||||||
updateSession ( model, cmd, sessionMsg ) =
|
updateSession ( model, cmd, sessionMsg ) =
|
||||||
let
|
let
|
||||||
session =
|
( session, newCmd ) =
|
||||||
Session.update sessionMsg model.session
|
Session.update sessionMsg model.session
|
||||||
|
|
||||||
newModel =
|
|
||||||
{ model | session = session }
|
|
||||||
in
|
in
|
||||||
if session.persistent == model.session.persistent then
|
( { model | session = session }
|
||||||
-- No change
|
, Cmd.batch [ newCmd, cmd ]
|
||||||
( newModel, cmd )
|
)
|
||||||
|
|
||||||
else
|
|
||||||
( newModel
|
|
||||||
, Cmd.batch [ cmd, Ports.storeSession session.persistent ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
{-| Map page updates to Main Model and Msg types.
|
{-| Map page updates to Main Model and Msg types.
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ port module Ports exposing
|
|||||||
, storeSession
|
, storeSession
|
||||||
)
|
)
|
||||||
|
|
||||||
import Data.Session exposing (Persistent)
|
|
||||||
import Json.Encode exposing (Value)
|
import Json.Encode exposing (Value)
|
||||||
|
|
||||||
|
|
||||||
@@ -18,4 +17,4 @@ port monitorMessage : (Value -> msg) -> Sub msg
|
|||||||
port onSessionChange : (Value -> msg) -> Sub msg
|
port onSessionChange : (Value -> msg) -> Sub msg
|
||||||
|
|
||||||
|
|
||||||
port storeSession : Persistent -> Cmd msg
|
port storeSession : Value -> Cmd msg
|
||||||
|
|||||||
Reference in New Issue
Block a user