mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-23 02:48:26 +00:00
Introduce ui settings and let user set page size for item search
This commit is contained in:
93
modules/webapp/src/main/elm/Comp/UiSettingsForm.elm
Normal file
93
modules/webapp/src/main/elm/Comp/UiSettingsForm.elm
Normal file
@ -0,0 +1,93 @@
|
||||
module Comp.UiSettingsForm exposing
|
||||
( Model
|
||||
, Msg
|
||||
, init
|
||||
, initWith
|
||||
, update
|
||||
, view
|
||||
)
|
||||
|
||||
import Comp.IntField
|
||||
import Data.UiSettings exposing (StoredUiSettings, UiSettings)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ defaults : UiSettings
|
||||
, input : StoredUiSettings
|
||||
, searchPageSizeModel : Comp.IntField.Model
|
||||
}
|
||||
|
||||
|
||||
initWith : UiSettings -> Model
|
||||
initWith defaults =
|
||||
{ defaults = defaults
|
||||
, input = Data.UiSettings.toStoredUiSettings defaults
|
||||
, searchPageSizeModel =
|
||||
Comp.IntField.init
|
||||
(Just 10)
|
||||
(Just 500)
|
||||
False
|
||||
"Item search page"
|
||||
}
|
||||
|
||||
|
||||
init : Model
|
||||
init =
|
||||
initWith Data.UiSettings.defaults
|
||||
|
||||
|
||||
changeInput : (StoredUiSettings -> StoredUiSettings) -> Model -> StoredUiSettings
|
||||
changeInput change model =
|
||||
change model.input
|
||||
|
||||
|
||||
type Msg
|
||||
= SearchPageSizeMsg Comp.IntField.Msg
|
||||
|
||||
|
||||
getSettings : Model -> UiSettings
|
||||
getSettings model =
|
||||
Data.UiSettings.merge model.input model.defaults
|
||||
|
||||
|
||||
|
||||
--- Update
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Maybe UiSettings )
|
||||
update msg model =
|
||||
case msg of
|
||||
SearchPageSizeMsg lm ->
|
||||
let
|
||||
( m, n ) =
|
||||
Comp.IntField.update lm model.searchPageSizeModel
|
||||
|
||||
model_ =
|
||||
{ model
|
||||
| searchPageSizeModel = m
|
||||
, input = changeInput (\s -> { s | itemSearchPageSize = n }) model
|
||||
}
|
||||
|
||||
nextSettings =
|
||||
Maybe.map (\_ -> getSettings model_) n
|
||||
in
|
||||
( model_, nextSettings )
|
||||
|
||||
|
||||
|
||||
--- View
|
||||
|
||||
|
||||
view : Model -> Html Msg
|
||||
view model =
|
||||
div [ class "ui form" ]
|
||||
[ Html.map SearchPageSizeMsg
|
||||
(Comp.IntField.viewWithInfo
|
||||
"Maximum results in one page when searching items."
|
||||
model.input.itemSearchPageSize
|
||||
""
|
||||
model.searchPageSizeModel
|
||||
)
|
||||
]
|
119
modules/webapp/src/main/elm/Comp/UiSettingsManage.elm
Normal file
119
modules/webapp/src/main/elm/Comp/UiSettingsManage.elm
Normal file
@ -0,0 +1,119 @@
|
||||
module Comp.UiSettingsManage exposing
|
||||
( Model
|
||||
, Msg
|
||||
, init
|
||||
, update
|
||||
, view
|
||||
)
|
||||
|
||||
import Api.Model.BasicResult exposing (BasicResult)
|
||||
import Comp.UiSettingsForm
|
||||
import Data.Flags exposing (Flags)
|
||||
import Data.UiSettings exposing (UiSettings)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (onClick)
|
||||
import Ports
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ formModel : Comp.UiSettingsForm.Model
|
||||
, settings : Maybe UiSettings
|
||||
, message : Maybe BasicResult
|
||||
}
|
||||
|
||||
|
||||
type Msg
|
||||
= UiSettingsFormMsg Comp.UiSettingsForm.Msg
|
||||
| Submit
|
||||
| SettingsSaved
|
||||
|
||||
|
||||
init : UiSettings -> Model
|
||||
init defaults =
|
||||
{ formModel = Comp.UiSettingsForm.initWith defaults
|
||||
, settings = Nothing
|
||||
, message = Nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
--- update
|
||||
|
||||
|
||||
update : Flags -> Msg -> Model -> ( Model, Cmd Msg, Sub Msg )
|
||||
update flags msg model =
|
||||
case msg of
|
||||
UiSettingsFormMsg lm ->
|
||||
let
|
||||
( m_, sett ) =
|
||||
Comp.UiSettingsForm.update lm model.formModel
|
||||
in
|
||||
( { model
|
||||
| formModel = m_
|
||||
, settings = sett
|
||||
, message = Nothing
|
||||
}
|
||||
, Cmd.none
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
Submit ->
|
||||
case model.settings of
|
||||
Just s ->
|
||||
( { model | message = Nothing }
|
||||
, Ports.storeUiSettings flags s
|
||||
, Ports.onUiSettingsSaved SettingsSaved
|
||||
)
|
||||
|
||||
Nothing ->
|
||||
( { model | message = Just (BasicResult False "Settings unchanged or invalid.") }
|
||||
, Cmd.none
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
SettingsSaved ->
|
||||
( { model | message = Just (BasicResult True "Settings saved.") }
|
||||
, Cmd.none
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
|
||||
|
||||
--- View
|
||||
|
||||
|
||||
isError : Model -> Bool
|
||||
isError model =
|
||||
Maybe.map .success model.message == Just False
|
||||
|
||||
|
||||
isSuccess : Model -> Bool
|
||||
isSuccess model =
|
||||
Maybe.map .success model.message == Just True
|
||||
|
||||
|
||||
view : String -> Model -> Html Msg
|
||||
view classes model =
|
||||
div [ class classes ]
|
||||
[ Html.map UiSettingsFormMsg (Comp.UiSettingsForm.view model.formModel)
|
||||
, div [ class "ui divider" ] []
|
||||
, button
|
||||
[ class "ui primary button"
|
||||
, onClick Submit
|
||||
]
|
||||
[ text "Submit"
|
||||
]
|
||||
, div
|
||||
[ classList
|
||||
[ ( "ui message", True )
|
||||
, ( "success", isSuccess model )
|
||||
, ( "error", isError model )
|
||||
, ( "hidden invisible", model.message == Nothing )
|
||||
]
|
||||
]
|
||||
[ Maybe.map .message model.message
|
||||
|> Maybe.withDefault ""
|
||||
|> text
|
||||
]
|
||||
]
|
Reference in New Issue
Block a user