From 41e0fe9581ed286804108d05fc34db1c63df563b Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Thu, 19 Aug 2021 01:23:54 +0200 Subject: [PATCH] Add text search Closes #10 --- src/Pages/Top.elm | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Pages/Top.elm b/src/Pages/Top.elm index 4559273..7da5c5b 100644 --- a/src/Pages/Top.elm +++ b/src/Pages/Top.elm @@ -1,7 +1,7 @@ module Pages.Top exposing (Model, Msg, Params, page) import Enum exposing (Enum) -import Html exposing (Html, a, button, div, h2, h5, i, img, li, p, span, text, ul, form, label, select, option) +import Html exposing (Html, a, button, div, h2, h5, i, img, li, p, span, text, ul, form, label, select, option, input) import Html.Attributes exposing (alt, class, href, src, style, id, for, value) import Html.Events exposing (onClick, onInput) import Http @@ -50,6 +50,7 @@ type alias App = type alias Model = { filter_score : Maybe Int , filter_category : Maybe Category + , filter_text : Maybe String , status : Status , apps : (List App) , results : (List App) @@ -91,7 +92,8 @@ default_image = default_model : Model default_model = - { filter_score = Nothing, filter_category = Nothing, status = Loading, apps = [], results = [] } + { filter_score = Nothing, filter_category = Nothing, filter_text = Nothing, + status = Loading, apps = [], results = [] } -- UPDATE @@ -101,6 +103,7 @@ type Msg = MorePlease | FilterScore String | FilterCategory String + | FilterText String | GotApps (Result Http.Error (List App)) @@ -124,6 +127,22 @@ filterAppsCategory apps category = Nothing -> apps +filterAppsText : List App -> Maybe String -> List App +filterAppsText apps text = + case text of + Just "" -> + apps + Just t -> + List.filter (\app -> + String.contains (String.toLower t) ( + String.toLower app.name ++ String.toLower ( + Maybe.withDefault "" app.description + ) + ) + ) apps + Nothing -> + apps + update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of @@ -137,7 +156,9 @@ update msg model = ( { model | filter_score = filter_score , results = filterAppsScore ( - filterAppsCategory model.apps model.filter_category + filterAppsCategory ( + filterAppsText model.apps model.filter_text + ) model.filter_category ) (String.toInt filter) }, Cmd.none ) @@ -148,10 +169,22 @@ update msg model = ( { model | filter_category = category , results = filterAppsCategory ( - filterAppsScore model.apps model.filter_score + filterAppsScore ( + filterAppsText model.apps model.filter_text + )model.filter_score ) category }, Cmd.none ) + FilterText filter -> + ( { model + | filter_text = Just filter + , results = filterAppsText ( + filterAppsScore ( + filterAppsCategory model.apps model.filter_category + ) model.filter_score + ) (Just filter) + }, Cmd.none ) + GotApps result -> case result of Ok apps -> @@ -320,10 +353,12 @@ viewApps model = , option [ value "4" ] [ text "4 (pre-alpha)" ] ] , label [ for "category" ] [ text "Category" ] - , select [ class "ml-2 form-control", id "category", onInput FilterCategory ] ( + , select [ class "ml-2 mr-4 form-control", id "category", onInput FilterCategory ] ( option [ ] [ text "any" ] :: (List.map viewCategoryOption categories.list) ) + , label [ for "text" ] [ text "Search" ] + , input [ class "ml-2 form-control", id "text", onInput FilterText ] [] , div [ class "ml-auto badge badge-secondary" ] [ text (String.fromInt (List.length model.results) ++ " recipes") ]