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

ui: Add session update logic into Session

This commit is contained in:
James Hillyerd
2018-11-23 13:57:42 -08:00
parent e71377f966
commit 0f9585a52b
3 changed files with 57 additions and 46 deletions

View File

@@ -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,8 +45,10 @@ 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 =
let
newSession =
case msg of case msg of
None -> None ->
session session
@@ -77,6 +81,15 @@ update msg session =
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 )
]

View File

@@ -224,19 +224,11 @@ 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 ]
) )

View File

@@ -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