Store and load client settings

This commit is contained in:
Eike Kettner
2021-05-26 01:14:30 +02:00
parent 9f76357879
commit 9ccc3ce438
10 changed files with 309 additions and 245 deletions

View File

@ -193,6 +193,7 @@ type Msg
| KeyUpPowerSearchbarMsg (Maybe KeyCode)
| RequestReprocessSelected
| ReprocessSelectedConfirmed
| ClientSettingsSaveResp UiSettings (Result Http.Error BasicResult)
type SearchType

View File

@ -1,4 +1,7 @@
module Page.Home.Update exposing (update)
module Page.Home.Update exposing
( UpdateResult
, update
)
import Api
import Api.Model.ItemLightList exposing (ItemLightList)
@ -16,7 +19,6 @@ import Data.Items
import Data.UiSettings exposing (UiSettings)
import Page exposing (Page(..))
import Page.Home.Data exposing (..)
import Ports
import Process
import Scroll
import Set exposing (Set)
@ -28,7 +30,15 @@ import Util.ItemDragDrop as DD
import Util.Update
update : Maybe String -> Nav.Key -> Flags -> UiSettings -> Msg -> Model -> ( Model, Cmd Msg, Sub Msg )
type alias UpdateResult =
{ model : Model
, cmd : Cmd Msg
, sub : Sub Msg
, newSettings : Maybe UiSettings
}
update : Maybe String -> Nav.Key -> Flags -> UiSettings -> Msg -> Model -> UpdateResult
update mId key flags settings msg model =
case msg of
Init ->
@ -41,11 +51,12 @@ update mId key flags settings msg model =
, scroll = True
}
in
Util.Update.andThen2
[ update mId key flags settings (SearchMenuMsg Comp.SearchMenu.Init)
, doSearch searchParam
]
model
makeResult <|
Util.Update.andThen3
[ update mId key flags settings (SearchMenuMsg Comp.SearchMenu.Init)
, doSearch searchParam
]
model
ResetSearch ->
let
@ -79,21 +90,21 @@ update mId key flags settings msg model =
BasicSearch
}
( m2, c2, s2 ) =
result =
if nextState.stateChange && not model.searchInProgress then
doSearch (SearchParam flags BasicSearch settings.itemSearchPageSize 0 False) newModel
else
withSub ( newModel, Cmd.none )
in
( m2
, Cmd.batch
[ c2
, Cmd.map SearchMenuMsg nextState.cmd
, dropCmd
]
, s2
)
{ result
| cmd =
Cmd.batch
[ result.cmd
, Cmd.map SearchMenuMsg nextState.cmd
, dropCmd
]
}
SetLinkTarget lt ->
case linkTargetMsg lt of
@ -101,7 +112,7 @@ update mId key flags settings msg model =
update mId key flags settings m model
Nothing ->
( model, Cmd.none, Sub.none )
makeResult ( model, Cmd.none, Sub.none )
ItemCardListMsg m ->
let
@ -144,15 +155,16 @@ update mId key flags settings msg model =
, moreAvailable = list.groups /= []
}
in
Util.Update.andThen2
[ update mId key flags settings (ItemCardListMsg (Comp.ItemCardList.SetResults list))
, if scroll then
scrollToCard mId
makeResult <|
Util.Update.andThen3
[ update mId key flags settings (ItemCardListMsg (Comp.ItemCardList.SetResults list))
, if scroll then
scrollToCard mId
else
\next -> ( next, Cmd.none, Sub.none )
]
m
else
\next -> makeResult ( next, Cmd.none, Sub.none )
]
m
ItemSearchAddResp (Ok list) ->
let
@ -167,10 +179,7 @@ update mId key flags settings msg model =
, moreAvailable = list.groups /= []
}
in
Util.Update.andThen2
[ update mId key flags settings (ItemCardListMsg (Comp.ItemCardList.AddResults list))
]
m
update mId key flags settings (ItemCardListMsg (Comp.ItemCardList.AddResults list)) m
ItemSearchAddResp (Err _) ->
withSub
@ -514,10 +523,11 @@ update mId key flags settings msg model =
res.change
(MultiUpdateResp res.change)
in
( { model | viewMode = SelectView svm_ }
, Cmd.batch [ cmd_, upCmd ]
, sub_
)
makeResult
( { model | viewMode = SelectView svm_ }
, Cmd.batch [ cmd_, upCmd ]
, sub_
)
_ ->
noSub ( model, Cmd.none )
@ -540,10 +550,11 @@ update mId key flags settings msg model =
noSub ( nm, Cmd.none )
MultiUpdateResp change (Err _) ->
( updateSelectViewNameState False model change
, Cmd.none
, Sub.none
)
makeResult
( updateSelectViewNameState False model change
, Cmd.none
, Sub.none
)
ReplaceChangedItemsResp (Ok items) ->
noSub ( replaceItems model items, Cmd.none )
@ -592,10 +603,24 @@ update mId key flags settings msg model =
{ settings | cardPreviewFullWidth = not settings.cardPreviewFullWidth }
cmd =
Ports.storeUiSettings flags newSettings
Api.saveClientSettings flags newSettings (ClientSettingsSaveResp newSettings)
in
noSub ( model, cmd )
ClientSettingsSaveResp newSettings (Ok res) ->
if res.success then
{ model = model
, cmd = Cmd.none
, sub = Sub.none
, newSettings = Just newSettings
}
else
noSub ( model, Cmd.none )
ClientSettingsSaveResp _ (Err _) ->
noSub ( model, Cmd.none )
PowerSearchMsg lm ->
let
result =
@ -609,7 +634,7 @@ update mId key flags settings msg model =
in
case result.action of
Comp.PowerSearchInput.NoAction ->
( model_, cmd_, Sub.map PowerSearchMsg result.subs )
makeResult ( model_, cmd_, Sub.map PowerSearchMsg result.subs )
Comp.PowerSearchInput.SubmitSearch ->
update mId key flags settings (DoSearch model_.searchTypeDropdownValue) model_
@ -703,21 +728,22 @@ loadChangedItems flags ids =
Api.itemSearch flags search ReplaceChangedItemsResp
scrollToCard : Maybe String -> Model -> ( Model, Cmd Msg, Sub Msg )
scrollToCard : Maybe String -> Model -> UpdateResult
scrollToCard mId model =
let
scroll id =
Scroll.scrollElementY "item-card-list" id 0.5 0.5
in
case mId of
Just id ->
( { model | scrollToCard = mId }
, Task.attempt ScrollResult (scroll id)
, Sub.none
)
makeResult <|
case mId of
Just id ->
( { model | scrollToCard = mId }
, Task.attempt ScrollResult (scroll id)
, Sub.none
)
Nothing ->
( model, Cmd.none, Sub.none )
Nothing ->
( model, Cmd.none, Sub.none )
loadEditModel : Flags -> Cmd Msg
@ -725,7 +751,7 @@ loadEditModel flags =
Cmd.map EditMenuMsg (Comp.ItemDetail.MultiEditMenu.loadModel flags)
doSearch : SearchParam -> Model -> ( Model, Cmd Msg, Sub Msg )
doSearch : SearchParam -> Model -> UpdateResult
doSearch param model =
let
param_ =
@ -798,16 +824,26 @@ doSearchMore flags settings model =
)
withSub : ( Model, Cmd Msg ) -> ( Model, Cmd Msg, Sub Msg )
withSub : ( Model, Cmd Msg ) -> UpdateResult
withSub ( m, c ) =
( m
, c
, Throttle.ifNeeded
(Time.every 500 (\_ -> UpdateThrottle))
m.throttle
)
makeResult
( m
, c
, Throttle.ifNeeded
(Time.every 500 (\_ -> UpdateThrottle))
m.throttle
)
noSub : ( Model, Cmd Msg ) -> ( Model, Cmd Msg, Sub Msg )
noSub : ( Model, Cmd Msg ) -> UpdateResult
noSub ( m, c ) =
( m, c, Sub.none )
makeResult ( m, c, Sub.none )
makeResult : ( Model, Cmd Msg, Sub Msg ) -> UpdateResult
makeResult ( m, c, s ) =
{ model = m
, cmd = c
, sub = s
, newSettings = Nothing
}

View File

@ -11,7 +11,7 @@ import Data.UiSettings exposing (UiSettings)
import Page.UserSettings.Data exposing (..)
update : Flags -> UiSettings -> Msg -> Model -> ( Model, Cmd Msg, Sub Msg )
update : Flags -> UiSettings -> Msg -> Model -> ( Model, Cmd Msg, Maybe UiSettings )
update flags settings msg model =
case msg of
SetTab t ->
@ -25,17 +25,23 @@ update flags settings msg model =
( em, c ) =
Comp.EmailSettingsManage.init flags
in
( { m | emailSettingsModel = em }, Cmd.map EmailSettingsMsg c, Sub.none )
( { m | emailSettingsModel = em }
, Cmd.map EmailSettingsMsg c
, Nothing
)
ImapSettingsTab ->
let
( em, c ) =
Comp.ImapSettingsManage.init flags
in
( { m | imapSettingsModel = em }, Cmd.map ImapSettingsMsg c, Sub.none )
( { m | imapSettingsModel = em }
, Cmd.map ImapSettingsMsg c
, Nothing
)
ChangePassTab ->
( m, Cmd.none, Sub.none )
( m, Cmd.none, Nothing )
NotificationTab ->
let
@ -43,7 +49,7 @@ update flags settings msg model =
Cmd.map NotificationMsg
(Tuple.second (Comp.NotificationManage.init flags))
in
( m, initCmd, Sub.none )
( m, initCmd, Nothing )
ScanMailboxTab ->
let
@ -51,31 +57,40 @@ update flags settings msg model =
Cmd.map ScanMailboxMsg
(Tuple.second (Comp.ScanMailboxManage.init flags))
in
( m, initCmd, Sub.none )
( m, initCmd, Nothing )
UiSettingsTab ->
( m, Cmd.none, Sub.none )
( m, Cmd.none, Nothing )
ChangePassMsg m ->
let
( m2, c2 ) =
Comp.ChangePasswordForm.update flags m model.changePassModel
in
( { model | changePassModel = m2 }, Cmd.map ChangePassMsg c2, Sub.none )
( { model | changePassModel = m2 }
, Cmd.map ChangePassMsg c2
, Nothing
)
EmailSettingsMsg m ->
let
( m2, c2 ) =
Comp.EmailSettingsManage.update flags m model.emailSettingsModel
in
( { model | emailSettingsModel = m2 }, Cmd.map EmailSettingsMsg c2, Sub.none )
( { model | emailSettingsModel = m2 }
, Cmd.map EmailSettingsMsg c2
, Nothing
)
ImapSettingsMsg m ->
let
( m2, c2 ) =
Comp.ImapSettingsManage.update flags m model.imapSettingsModel
in
( { model | imapSettingsModel = m2 }, Cmd.map ImapSettingsMsg c2, Sub.none )
( { model | imapSettingsModel = m2 }
, Cmd.map ImapSettingsMsg c2
, Nothing
)
NotificationMsg lm ->
let
@ -84,7 +99,7 @@ update flags settings msg model =
in
( { model | notificationModel = m2 }
, Cmd.map NotificationMsg c2
, Sub.none
, Nothing
)
ScanMailboxMsg lm ->
@ -94,17 +109,17 @@ update flags settings msg model =
in
( { model | scanMailboxModel = m2 }
, Cmd.map ScanMailboxMsg c2
, Sub.none
, Nothing
)
UiSettingsMsg lm ->
let
( m2, c2, s2 ) =
res =
Comp.UiSettingsManage.update flags settings lm model.uiSettingsModel
in
( { model | uiSettingsModel = m2 }
, Cmd.map UiSettingsMsg c2
, Sub.map UiSettingsMsg s2
( { model | uiSettingsModel = res.model }
, Cmd.map UiSettingsMsg res.cmd
, res.newSettings
)
UpdateSettings ->