Some UX fixes for showing suggestions (#112)

* focus on query input when showing page
* less visible reloading of suggestions when typing
This commit is contained in:
Rok Garbas 2020-07-06 14:27:37 +02:00 committed by GitHub
parent 3dceb99740
commit 46c7eaa46b
Failed to generate hash of commit
3 changed files with 75 additions and 28 deletions

View file

@ -75,8 +75,14 @@ init :
-> Maybe Int -> Maybe Int
-> Maybe Model -> Maybe Model
-> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
init = init channel query show from size model =
Search.init let
( newModel, newCmd ) =
Search.init channel query show from size model
in
( newModel
, Cmd.map SearchMsg newCmd
)

View file

@ -110,8 +110,14 @@ init :
-> Maybe Int -> Maybe Int
-> Maybe Model -> Maybe Model
-> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
init = init channel query show from size model =
Search.init let
( newModel, newCmd ) =
Search.init channel query show from size model
in
( newModel
, Cmd.map SearchMsg newCmd
)

View file

@ -29,7 +29,6 @@ import Html
, form , form
, h1 , h1
, h4 , h4
, i
, input , input
, li , li
, p , p
@ -40,6 +39,7 @@ import Html
import Html.Attributes import Html.Attributes
exposing exposing
( attribute ( attribute
, autofocus
, class , class
, classList , classList
, href , href
@ -64,10 +64,6 @@ import Task
import Url.Builder import Url.Builder
type alias Char =
String
type alias Model a = type alias Model a =
{ channel : String { channel : String
, query : Maybe String , query : Maybe String
@ -123,14 +119,6 @@ type alias ResultItem a =
} }
itemHtml : Char -> Html Never
itemHtml char =
div []
[ i [ class "fa fa-rebel" ] []
, text (" " ++ char)
]
init : init :
Maybe String Maybe String
-> Maybe String -> Maybe String
@ -138,7 +126,7 @@ init :
-> Maybe Int -> Maybe Int
-> Maybe Int -> Maybe Int
-> Maybe (Model a) -> Maybe (Model a)
-> ( Model a, Cmd msg ) -> ( Model a, Cmd (Msg a) )
init channel query show from size model = init channel query show from size model =
let let
defaultChannel = defaultChannel =
@ -162,7 +150,19 @@ init channel query show from size model =
|> Debouncer.Messages.settleWhenQuietFor (Just <| Debouncer.Messages.fromSeconds 0.6) |> Debouncer.Messages.settleWhenQuietFor (Just <| Debouncer.Messages.fromSeconds 0.6)
|> Debouncer.Messages.toDebouncer |> Debouncer.Messages.toDebouncer
, query = query , query = query
, querySuggest = RemoteData.NotAsked , querySuggest =
query
|> Maybe.map
(\selected ->
if String.endsWith "." selected then
model
|> Maybe.map .querySuggest
|> Maybe.withDefault RemoteData.NotAsked
else
RemoteData.NotAsked
)
|> Maybe.withDefault RemoteData.NotAsked
, querySelectedSuggestion = Nothing , querySelectedSuggestion = Nothing
, result = , result =
model model
@ -172,7 +172,7 @@ init channel query show from size model =
, from = Maybe.withDefault defaultFrom from , from = Maybe.withDefault defaultFrom from
, size = Maybe.withDefault defaultSize size , size = Maybe.withDefault defaultSize size
} }
, Cmd.none , Browser.Dom.focus "search-query-input" |> Task.attempt (\_ -> NoOp)
) )
@ -189,7 +189,7 @@ type Msg a
| QueryInput String | QueryInput String
| QueryInputSuggestionsSubmit | QueryInputSuggestionsSubmit
| QueryInputSuggestionsResponse (RemoteData.WebData (SearchResult a)) | QueryInputSuggestionsResponse (RemoteData.WebData (SearchResult a))
| QuerySubmit | QueryInputSubmit
| QueryResponse (RemoteData.WebData (SearchResult a)) | QueryResponse (RemoteData.WebData (SearchResult a))
| ShowDetails String | ShowDetails String
| SuggestionsMoveDown | SuggestionsMoveDown
@ -262,7 +262,22 @@ update path navKey result_type options decodeResultItemSource msg model =
(QueryInputDebounce (Debouncer.Messages.provideInput QueryInputSuggestionsSubmit)) (QueryInputDebounce (Debouncer.Messages.provideInput QueryInputSuggestionsSubmit))
{ model { model
| query = Just query | query = Just query
, querySuggest = RemoteData.NotAsked , querySuggest =
case model.querySuggest of
RemoteData.Success result ->
let
suggestions =
getSuggestions (Just "XXXX") model.querySuggest
|> List.map .text
in
if List.member (Just query) suggestions then
RemoteData.NotAsked
else
model.querySuggest
_ ->
RemoteData.NotAsked
, querySelectedSuggestion = Nothing , querySelectedSuggestion = Nothing
} }
|> Tuple.mapSecond |> Tuple.mapSecond
@ -304,7 +319,17 @@ update path navKey result_type options decodeResultItemSource msg model =
) )
in in
( { model ( { model
| querySuggest = RemoteData.Loading | querySuggest =
model.query
|> Maybe.map
(\selected ->
if String.endsWith "." selected then
model.querySuggest
else
RemoteData.NotAsked
)
|> Maybe.withDefault RemoteData.NotAsked
, querySelectedSuggestion = Nothing , querySelectedSuggestion = Nothing
} }
, makeRequest , makeRequest
@ -324,7 +349,7 @@ update path navKey result_type options decodeResultItemSource msg model =
, Cmd.none , Cmd.none
) )
QuerySubmit -> QueryInputSubmit ->
( { model | result = RemoteData.Loading } ( { model | result = RemoteData.Loading }
, createUrl , createUrl
path path
@ -395,16 +420,24 @@ update path navKey result_type options decodeResultItemSource msg model =
Nothing -> Nothing ->
( model ( model
, Cmd.none , Task.attempt (\_ -> QueryInputSubmit) (Task.succeed ())
) )
SuggestionsClickSelect selected -> SuggestionsClickSelect selected ->
( { model ( { model
| querySuggest = RemoteData.NotAsked | querySuggest =
if String.endsWith "." selected then
model.querySuggest
else
RemoteData.NotAsked
, querySelectedSuggestion = Nothing , querySelectedSuggestion = Nothing
, query = Just selected , query = Just selected
} }
, Task.attempt (\_ -> QueryInputSuggestionsSubmit) (Task.succeed ()) , Cmd.batch
[ Task.attempt (\_ -> QueryInputSuggestionsSubmit) (Task.succeed ())
, Task.attempt (\_ -> QueryInputSubmit) (Task.succeed ())
]
) )
SuggestionsClose -> SuggestionsClose ->
@ -658,7 +691,7 @@ view path title model viewSuccess outMsg =
, ( "with-suggestions-loading", RemoteData.isLoading model.querySuggest ) , ( "with-suggestions-loading", RemoteData.isLoading model.querySuggest )
] ]
] ]
[ form [ onSubmit (outMsg QuerySubmit) ] [ form [ onSubmit (outMsg QueryInputSubmit) ]
[ p [ p
[] []
[ strong [] [ strong []
@ -691,6 +724,8 @@ view path title model viewSuccess outMsg =
] ]
[ input [ input
([ type_ "text" ([ type_ "text"
, id "search-query-input"
, autofocus True
, onInput (\x -> outMsg (QueryInput x)) , onInput (\x -> outMsg (QueryInput x))
, value <| Maybe.withDefault "" model.query , value <| Maybe.withDefault "" model.query
] ]