mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
ui: Refactor update and setRoute with updateWith
This commit is contained in:
@@ -39,7 +39,7 @@ init sessionValue location key =
|
|||||||
session =
|
session =
|
||||||
Session.init key location (Session.decodeValueWithDefault sessionValue)
|
Session.init key location (Session.decodeValueWithDefault sessionValue)
|
||||||
|
|
||||||
( subModel, _ ) =
|
( subModel, _, _ ) =
|
||||||
Home.init
|
Home.init
|
||||||
|
|
||||||
model =
|
model =
|
||||||
@@ -51,7 +51,7 @@ init sessionValue location key =
|
|||||||
route =
|
route =
|
||||||
Route.fromUrl location
|
Route.fromUrl location
|
||||||
in
|
in
|
||||||
applySession (setRoute route model)
|
changeRouteTo route model |> updateSession
|
||||||
|
|
||||||
|
|
||||||
type Msg
|
type Msg
|
||||||
@@ -106,7 +106,7 @@ pageSubscriptions page =
|
|||||||
|
|
||||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||||
update msg model =
|
update msg model =
|
||||||
applySession <|
|
updateSession <|
|
||||||
case msg of
|
case msg of
|
||||||
LinkClicked req ->
|
LinkClicked req ->
|
||||||
case req of
|
case req of
|
||||||
@@ -119,7 +119,7 @@ update msg model =
|
|||||||
UrlChanged url ->
|
UrlChanged url ->
|
||||||
-- Responds to new browser URL.
|
-- Responds to new browser URL.
|
||||||
if model.session.routing then
|
if model.session.routing then
|
||||||
setRoute (Route.fromUrl url) model
|
changeRouteTo (Route.fromUrl url) model
|
||||||
|
|
||||||
else
|
else
|
||||||
-- Skip once, but re-enable routing.
|
-- Skip once, but re-enable routing.
|
||||||
@@ -162,36 +162,30 @@ update msg model =
|
|||||||
-}
|
-}
|
||||||
updatePage : Msg -> Model -> ( Model, Cmd Msg, Session.Msg )
|
updatePage : Msg -> Model -> ( Model, Cmd Msg, Session.Msg )
|
||||||
updatePage msg model =
|
updatePage msg model =
|
||||||
let
|
|
||||||
-- Handles sub-model update by calling toUpdate with subMsg & subModel, then packing the
|
|
||||||
-- updated sub-model back into model.page.
|
|
||||||
modelUpdate toPage toMsg subUpdate subMsg subModel =
|
|
||||||
let
|
|
||||||
( newModel, subCmd, sessionMsg ) =
|
|
||||||
subUpdate model.session subMsg subModel
|
|
||||||
in
|
|
||||||
( { model | page = toPage newModel }, Cmd.map toMsg subCmd, sessionMsg )
|
|
||||||
in
|
|
||||||
case ( msg, model.page ) of
|
case ( msg, model.page ) of
|
||||||
( HomeMsg subMsg, Home subModel ) ->
|
( HomeMsg subMsg, Home subModel ) ->
|
||||||
modelUpdate Home HomeMsg Home.update subMsg subModel
|
Home.update model.session subMsg subModel
|
||||||
|
|> updateWith Home HomeMsg model
|
||||||
|
|
||||||
( MailboxMsg subMsg, Mailbox subModel ) ->
|
( MailboxMsg subMsg, Mailbox subModel ) ->
|
||||||
modelUpdate Mailbox MailboxMsg Mailbox.update subMsg subModel
|
Mailbox.update model.session subMsg subModel
|
||||||
|
|> updateWith Mailbox MailboxMsg model
|
||||||
|
|
||||||
( MonitorMsg subMsg, Monitor subModel ) ->
|
( MonitorMsg subMsg, Monitor subModel ) ->
|
||||||
modelUpdate Monitor MonitorMsg Monitor.update subMsg subModel
|
Monitor.update model.session subMsg subModel
|
||||||
|
|> updateWith Monitor MonitorMsg model
|
||||||
|
|
||||||
( StatusMsg subMsg, Status subModel ) ->
|
( StatusMsg subMsg, Status subModel ) ->
|
||||||
modelUpdate Status StatusMsg Status.update subMsg subModel
|
Status.update model.session subMsg subModel
|
||||||
|
|> updateWith Status StatusMsg model
|
||||||
|
|
||||||
( _, _ ) ->
|
( _, _ ) ->
|
||||||
-- Disregard messages destined for the wrong page.
|
-- Disregard messages destined for the wrong page.
|
||||||
( model, Cmd.none, Session.none )
|
( model, Cmd.none, Session.none )
|
||||||
|
|
||||||
|
|
||||||
setRoute : Route -> Model -> ( Model, Cmd Msg, Session.Msg )
|
changeRouteTo : Route -> Model -> ( Model, Cmd Msg, Session.Msg )
|
||||||
setRoute route model =
|
changeRouteTo route model =
|
||||||
let
|
let
|
||||||
( newModel, newCmd, newSession ) =
|
( newModel, newCmd, newSession ) =
|
||||||
case route of
|
case route of
|
||||||
@@ -199,50 +193,24 @@ setRoute route model =
|
|||||||
( model, Cmd.none, Session.SetFlash ("Unknown route requested: " ++ hash) )
|
( model, Cmd.none, Session.SetFlash ("Unknown route requested: " ++ hash) )
|
||||||
|
|
||||||
Route.Home ->
|
Route.Home ->
|
||||||
let
|
|
||||||
( subModel, subCmd ) =
|
|
||||||
Home.init
|
Home.init
|
||||||
in
|
|> updateWith Home HomeMsg model
|
||||||
( { model | page = Home subModel }
|
|
||||||
, Cmd.map HomeMsg subCmd
|
|
||||||
, Session.none
|
|
||||||
)
|
|
||||||
|
|
||||||
Route.Mailbox name ->
|
Route.Mailbox name ->
|
||||||
let
|
|
||||||
( subModel, subCmd ) =
|
|
||||||
Mailbox.init name Nothing
|
Mailbox.init name Nothing
|
||||||
in
|
|> updateWith Mailbox MailboxMsg model
|
||||||
( { model | page = Mailbox subModel }
|
|
||||||
, Cmd.map MailboxMsg subCmd
|
|
||||||
, Session.none
|
|
||||||
)
|
|
||||||
|
|
||||||
Route.Message mailbox id ->
|
Route.Message mailbox id ->
|
||||||
let
|
|
||||||
( subModel, subCmd ) =
|
|
||||||
Mailbox.init mailbox (Just id)
|
Mailbox.init mailbox (Just id)
|
||||||
in
|
|> updateWith Mailbox MailboxMsg model
|
||||||
( { model | page = Mailbox subModel }
|
|
||||||
, Cmd.map MailboxMsg subCmd
|
|
||||||
, Session.none
|
|
||||||
)
|
|
||||||
|
|
||||||
Route.Monitor ->
|
Route.Monitor ->
|
||||||
let
|
|
||||||
( subModel, subCmd ) =
|
|
||||||
Monitor.init
|
Monitor.init
|
||||||
in
|
|> updateWith Monitor MonitorMsg model
|
||||||
( { model | page = Monitor subModel }
|
|
||||||
, Cmd.map MonitorMsg subCmd
|
|
||||||
, Session.none
|
|
||||||
)
|
|
||||||
|
|
||||||
Route.Status ->
|
Route.Status ->
|
||||||
( { model | page = Status Status.init }
|
Status.init
|
||||||
, Cmd.map StatusMsg Status.load
|
|> updateWith Status StatusMsg model
|
||||||
, Session.none
|
|
||||||
)
|
|
||||||
in
|
in
|
||||||
case model.page of
|
case model.page of
|
||||||
Monitor _ ->
|
Monitor _ ->
|
||||||
@@ -253,8 +221,8 @@ setRoute route model =
|
|||||||
( newModel, newCmd, newSession )
|
( newModel, newCmd, newSession )
|
||||||
|
|
||||||
|
|
||||||
applySession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
|
updateSession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
|
||||||
applySession ( model, cmd, sessionMsg ) =
|
updateSession ( model, cmd, sessionMsg ) =
|
||||||
let
|
let
|
||||||
session =
|
session =
|
||||||
Session.update sessionMsg model.session
|
Session.update sessionMsg model.session
|
||||||
@@ -272,6 +240,21 @@ applySession ( model, cmd, sessionMsg ) =
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
{-| Map page updates to Main Model and Msg types.
|
||||||
|
-}
|
||||||
|
updateWith :
|
||||||
|
(subModel -> Page)
|
||||||
|
-> (subMsg -> Msg)
|
||||||
|
-> Model
|
||||||
|
-> ( subModel, Cmd subMsg, Session.Msg )
|
||||||
|
-> ( Model, Cmd Msg, Session.Msg )
|
||||||
|
updateWith toPage toMsg model ( subModel, subCmd, sessionMsg ) =
|
||||||
|
( { model | page = toPage subModel }
|
||||||
|
, Cmd.map toMsg subCmd
|
||||||
|
, sessionMsg
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- VIEW
|
-- VIEW
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type alias Model =
|
|||||||
{ greeting : String }
|
{ greeting : String }
|
||||||
|
|
||||||
|
|
||||||
init : ( Model, Cmd Msg )
|
init : ( Model, Cmd Msg, Session.Msg )
|
||||||
init =
|
init =
|
||||||
let
|
let
|
||||||
cmdGreeting =
|
cmdGreeting =
|
||||||
@@ -26,7 +26,7 @@ init =
|
|||||||
, expect = Http.expectString GreetingLoaded
|
, expect = Http.expectString GreetingLoaded
|
||||||
}
|
}
|
||||||
in
|
in
|
||||||
( Model "", cmdGreeting )
|
( Model "", cmdGreeting, Session.none )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -77,10 +77,11 @@ type alias Model =
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init : String -> Maybe MessageID -> ( Model, Cmd Msg )
|
init : String -> Maybe MessageID -> ( Model, Cmd Msg, Session.Msg )
|
||||||
init mailboxName selection =
|
init mailboxName selection =
|
||||||
( Model mailboxName (LoadingList selection) SafeHtmlBody "" (Time.millisToPosix 0)
|
( Model mailboxName (LoadingList selection) SafeHtmlBody "" (Time.millisToPosix 0)
|
||||||
, load mailboxName
|
, load mailboxName
|
||||||
|
, Session.none
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ type MonitorMessage
|
|||||||
| Message MessageHeader
|
| Message MessageHeader
|
||||||
|
|
||||||
|
|
||||||
init : ( Model, Cmd Msg )
|
init : ( Model, Cmd Msg, Session.Msg )
|
||||||
init =
|
init =
|
||||||
( Model False [], Ports.monitorCommand True )
|
( Model False [], Ports.monitorCommand True, Session.none )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module Page.Status exposing (Model, Msg, init, load, subscriptions, update, view)
|
module Page.Status exposing (Model, Msg, init, subscriptions, update, view)
|
||||||
|
|
||||||
import Data.Metrics as Metrics exposing (Metrics)
|
import Data.Metrics as Metrics exposing (Metrics)
|
||||||
import Data.Session as Session exposing (Session)
|
import Data.Session as Session exposing (Session)
|
||||||
@@ -46,9 +46,9 @@ type alias Metric =
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init : Model
|
init : ( Model, Cmd Msg, Session.Msg )
|
||||||
init =
|
init =
|
||||||
{ metrics = Nothing
|
( { metrics = Nothing
|
||||||
, xCounter = 60
|
, xCounter = 60
|
||||||
, sysMem = Metric "System Memory" 0 Filesize.format graphZero initDataSet 10
|
, sysMem = Metric "System Memory" 0 Filesize.format graphZero initDataSet 10
|
||||||
, heapSize = Metric "Heap Size" 0 Filesize.format graphZero initDataSet 10
|
, heapSize = Metric "Heap Size" 0 Filesize.format graphZero initDataSet 10
|
||||||
@@ -65,6 +65,9 @@ init =
|
|||||||
, retainedCount = Metric "Stored Messages" 0 fmtInt graphZero initDataSet 60
|
, retainedCount = Metric "Stored Messages" 0 fmtInt graphZero initDataSet 60
|
||||||
, retainedSize = Metric "Store Size" 0 Filesize.format graphZero initDataSet 60
|
, retainedSize = Metric "Store Size" 0 Filesize.format graphZero initDataSet 60
|
||||||
}
|
}
|
||||||
|
, getMetrics
|
||||||
|
, Session.none
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
initDataSet : Spark.DataSet
|
initDataSet : Spark.DataSet
|
||||||
@@ -73,11 +76,6 @@ initDataSet =
|
|||||||
|> List.map (\x -> ( toFloat x, 0 ))
|
|> List.map (\x -> ( toFloat x, 0 ))
|
||||||
|
|
||||||
|
|
||||||
load : Cmd Msg
|
|
||||||
load =
|
|
||||||
getMetrics
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- SUBSCRIPTIONS --
|
-- SUBSCRIPTIONS --
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user