Fix updating search view

For example, when content-search was activated the view was not
correctly updated after deleting or drag-and-drop.
This commit is contained in:
Eike Kettner 2020-11-28 00:51:16 +01:00
parent 32c9113bd4
commit d62c4a5a72
3 changed files with 99 additions and 68 deletions

View File

@ -1,6 +1,7 @@
module Page.Home.Data exposing
( Model
, Msg(..)
, SearchParam
, SearchType(..)
, SelectActionMode(..)
, SelectViewModel
@ -49,8 +50,8 @@ type alias Model =
, moreInProgress : Bool
, throttle : Throttle Msg
, searchTypeDropdown : Comp.FixedDropdown.Model SearchType
, searchType : SearchType
, searchTypeForm : SearchType
, searchTypeDropdownValue : SearchType
, lastSearchType : SearchType
, contentOnlySearch : Maybe String
, dragDropData : DD.DragDropData
, scrollToCard : Maybe String
@ -104,8 +105,8 @@ init flags viewMode =
, searchTypeDropdown =
Comp.FixedDropdown.initMap searchTypeString
searchTypeOptions
, searchType = BasicSearch
, searchTypeForm = defaultSearchType flags
, searchTypeDropdownValue = defaultSearchType flags
, lastSearchType = BasicSearch
, contentOnlySearch = Nothing
, dragDropData =
DD.DragDropData DD.init Nothing
@ -156,14 +157,14 @@ type Msg
| ItemCardListMsg Comp.ItemCardList.Msg
| ItemSearchResp Bool (Result Http.Error ItemLightList)
| ItemSearchAddResp (Result Http.Error ItemLightList)
| DoSearch
| DoSearch SearchType
| ToggleSearchMenu
| ToggleSelectView
| LoadMore
| UpdateThrottle
| SetBasicSearch String
| SearchTypeMsg (Comp.FixedDropdown.Msg SearchType)
| KeyUpMsg (Maybe KeyCode)
| KeyUpSearchbarMsg (Maybe KeyCode)
| SetContentOnly String
| ScrollResult (Result Dom.Error ())
| ClearItemDetailId
@ -191,6 +192,15 @@ type SelectActionMode
| EditSelected
type alias SearchParam =
{ flags : Flags
, searchType : SearchType
, pageSize : Int
, offset : Int
, scroll : Bool
}
searchTypeString : SearchType -> String
searchTypeString st =
case st of
@ -215,51 +225,51 @@ itemNav id model =
}
doSearchCmd : Flags -> UiSettings -> Int -> Bool -> Model -> Cmd Msg
doSearchCmd flags settings offset scroll model =
case model.searchType of
doSearchCmd : SearchParam -> Model -> Cmd Msg
doSearchCmd param model =
case param.searchType of
BasicSearch ->
doSearchDefaultCmd flags settings offset scroll model
doSearchDefaultCmd param model
ContentOnlySearch ->
doSearchIndexCmd flags settings offset scroll model
doSearchIndexCmd param model
doSearchDefaultCmd : Flags -> UiSettings -> Int -> Bool -> Model -> Cmd Msg
doSearchDefaultCmd flags settings offset scroll model =
doSearchDefaultCmd : SearchParam -> Model -> Cmd Msg
doSearchDefaultCmd param model =
let
smask =
Comp.SearchMenu.getItemSearch model.searchMenuModel
mask =
{ smask
| limit = settings.itemSearchPageSize
, offset = offset
| limit = param.pageSize
, offset = param.offset
}
in
if offset == 0 then
Api.itemSearch flags mask (ItemSearchResp scroll)
if param.offset == 0 then
Api.itemSearch param.flags mask (ItemSearchResp param.scroll)
else
Api.itemSearch flags mask ItemSearchAddResp
Api.itemSearch param.flags mask ItemSearchAddResp
doSearchIndexCmd : Flags -> UiSettings -> Int -> Bool -> Model -> Cmd Msg
doSearchIndexCmd flags settings offset scroll model =
doSearchIndexCmd : SearchParam -> Model -> Cmd Msg
doSearchIndexCmd param model =
case model.contentOnlySearch of
Just q ->
let
mask =
{ query = q
, limit = settings.itemSearchPageSize
, offset = offset
, limit = param.pageSize
, offset = param.offset
}
in
if offset == 0 then
Api.itemIndexSearch flags mask (ItemSearchResp scroll)
if param.offset == 0 then
Api.itemIndexSearch param.flags mask (ItemSearchResp param.scroll)
else
Api.itemIndexSearch flags mask ItemSearchAddResp
Api.itemIndexSearch param.flags mask ItemSearchAddResp
Nothing ->
-- If there is no fulltext query, render simply the most
@ -269,9 +279,9 @@ doSearchIndexCmd flags settings offset scroll model =
Api.Model.ItemSearch.empty
mask =
{ emptyMask | limit = settings.itemSearchPageSize }
{ emptyMask | limit = param.pageSize }
in
Api.itemSearch flags mask (ItemSearchResp scroll)
Api.itemSearch param.flags mask (ItemSearchResp param.scroll)
resultsBelowLimit : UiSettings -> Model -> Bool

View File

@ -6,7 +6,6 @@ import Api.Model.ItemLightList exposing (ItemLightList)
import Api.Model.ItemSearch
import Browser.Navigation as Nav
import Comp.FixedDropdown
import Comp.ItemCard
import Comp.ItemCardList
import Comp.ItemDetail.EditMenu exposing (SaveNameState(..))
import Comp.ItemDetail.FormChange exposing (FormChange(..))
@ -28,7 +27,6 @@ import Time
import Util.Html exposing (KeyCode(..))
import Util.ItemDragDrop as DD
import Util.Maybe
import Util.String
import Util.Update
@ -36,9 +34,18 @@ update : Maybe String -> Nav.Key -> Flags -> UiSettings -> Msg -> Model -> ( Mod
update mId key flags settings msg model =
case msg of
Init ->
let
searchParam =
{ flags = flags
, searchType = model.lastSearchType
, pageSize = settings.itemSearchPageSize
, offset = 0
, scroll = True
}
in
Util.Update.andThen2
[ update mId key flags settings (SearchMenuMsg Comp.SearchMenu.Init)
, doSearch flags settings True
, doSearch searchParam
]
model
@ -47,7 +54,6 @@ update mId key flags settings msg model =
nm =
{ model
| searchOffset = 0
, searchType = defaultSearchType flags
, contentOnlySearch = Nothing
}
in
@ -64,7 +70,7 @@ update mId key flags settings msg model =
model.searchMenuModel
dropCmd =
DD.makeUpdateCmd flags (\_ -> DoSearch) nextState.dragDrop.dropped
DD.makeUpdateCmd flags (\_ -> DoSearch model.lastSearchType) nextState.dragDrop.dropped
newModel =
{ model
@ -74,7 +80,7 @@ update mId key flags settings msg model =
( m2, c2, s2 ) =
if nextState.stateChange && not model.searchInProgress then
doSearch flags settings False newModel
doSearch (SearchParam flags BasicSearch settings.itemSearchPageSize 0 False) newModel
else
withSub ( newModel, Cmd.none )
@ -181,16 +187,24 @@ update mId key flags settings msg model =
, Cmd.none
)
DoSearch ->
DoSearch stype ->
let
nm =
{ model | searchOffset = 0 }
param =
{ flags = flags
, searchType = stype
, pageSize = settings.itemSearchPageSize
, offset = 0
, scroll = False
}
in
if model.searchInProgress then
withSub ( model, Cmd.none )
else
doSearch flags settings False nm
doSearch param nm
ToggleSearchMenu ->
let
@ -247,7 +261,7 @@ update mId key flags settings msg model =
SetBasicSearch str ->
let
smMsg =
case model.searchTypeForm of
case model.searchTypeDropdownValue of
BasicSearch ->
SearchMenuMsg (Comp.SearchMenu.SetAllName str)
@ -268,12 +282,12 @@ update mId key flags settings msg model =
Comp.FixedDropdown.update lm model.searchTypeDropdown
mvChange =
Util.Maybe.filter (\a -> a /= model.searchTypeForm) mv
Util.Maybe.filter (\a -> a /= model.searchTypeDropdownValue) mv
m0 =
{ model
| searchTypeDropdown = sm
, searchTypeForm = Maybe.withDefault model.searchTypeForm mv
, searchTypeDropdownValue = Maybe.withDefault model.searchTypeDropdownValue mv
}
next =
@ -300,10 +314,10 @@ update mId key flags settings msg model =
Nothing ->
withSub ( m0, Cmd.none )
KeyUpMsg (Just Enter) ->
update mId key flags settings DoSearch model
KeyUpSearchbarMsg (Just Enter) ->
update mId key flags settings (DoSearch model.searchTypeDropdownValue) model
KeyUpMsg _ ->
KeyUpSearchbarMsg _ ->
withSub ( model, Cmd.none )
ScrollResult _ ->
@ -390,8 +404,16 @@ update mId key flags settings msg model =
let
nm =
{ model | viewMode = SearchView }
param =
{ flags = flags
, searchType = model.lastSearchType
, pageSize = settings.itemSearchPageSize
, offset = 0
, scroll = False
}
in
doSearch flags settings False nm
doSearch param nm
else
noSub ( model, Cmd.none )
@ -540,7 +562,7 @@ update mId key flags settings msg model =
model_ =
{ model | viewMode = viewMode }
in
update mId key flags settings DoSearch model_
update mId key flags settings (DoSearch model.lastSearchType) model_
@ -648,33 +670,24 @@ loadEditModel flags =
Cmd.map EditMenuMsg (Comp.ItemDetail.EditMenu.loadModel flags)
doSearch : Flags -> UiSettings -> Bool -> Model -> ( Model, Cmd Msg, Sub Msg )
doSearch flags settings scroll model =
doSearch : SearchParam -> Model -> ( Model, Cmd Msg, Sub Msg )
doSearch param model =
let
stype =
if
not (menuCollapsed model)
|| Util.String.isNothingOrBlank model.contentOnlySearch
then
BasicSearch
else
model.searchTypeForm
model_ =
{ model | searchType = stype }
param_ =
{ param | offset = 0 }
searchCmd =
doSearchCmd flags settings 0 scroll model_
doSearchCmd param_ model
( newThrottle, cmd ) =
Throttle.try searchCmd model.throttle
in
withSub
( { model_
( { model
| searchInProgress = cmd /= Cmd.none
, searchOffset = 0
, throttle = newThrottle
, lastSearchType = param.searchType
}
, cmd
)
@ -708,8 +721,16 @@ linkTargetMsg linkTarget =
doSearchMore : Flags -> UiSettings -> Model -> ( Model, Cmd Msg )
doSearchMore flags settings model =
let
param =
{ flags = flags
, searchType = model.lastSearchType
, pageSize = settings.itemSearchPageSize
, offset = model.searchOffset
, scroll = False
}
cmd =
doSearchCmd flags settings model.searchOffset False model
doSearchCmd param model
in
( { model | moreInProgress = True }
, cmd

View File

@ -83,7 +83,7 @@ view flags settings model =
]
, a
[ class "borderless item"
, onClick DoSearch
, onClick (DoSearch BasicSearch)
, title "Run search query"
, href ""
, disabled model.searchInProgress
@ -281,11 +281,11 @@ viewSearchBar flags model =
let
searchTypeItem =
Comp.FixedDropdown.Item
model.searchTypeForm
(searchTypeString model.searchTypeForm)
model.searchTypeDropdownValue
(searchTypeString model.searchTypeDropdownValue)
searchInput =
case model.searchTypeForm of
case model.searchTypeDropdownValue of
BasicSearch ->
model.searchMenuModel.allNameModel
@ -326,9 +326,9 @@ viewSearchBar flags model =
, ( "loading spinner icon", model.searchInProgress )
]
, href "#"
, onClick DoSearch
, onClick (DoSearch model.searchTypeDropdownValue)
]
(if hasMoreSearch model && model.searchTypeForm == BasicSearch then
(if hasMoreSearch model && model.searchTypeDropdownValue == BasicSearch then
[ i [ class "icons search-corner-icons" ]
[ i [ class "tiny blue circle icon" ] []
]
@ -341,7 +341,7 @@ viewSearchBar flags model =
[ type_ "text"
, placeholder "Quick Search "
, onInput SetBasicSearch
, Util.Html.onKeyUpCode KeyUpMsg
, Util.Html.onKeyUpCode KeyUpSearchbarMsg
, Maybe.map value searchInput
|> Maybe.withDefault (value "")
]
@ -381,7 +381,7 @@ hasMoreSearch model =
Comp.SearchMenu.getItemSearch model.searchMenuModel
is_ =
case model.searchType of
case model.lastSearchType of
BasicSearch ->
{ is | allNames = Nothing }