Commit 51478423 authored by André Espaze's avatar André Espaze
Browse files

Fuzzy search on series

parent c3a844d3c92a
......@@ -12,7 +12,8 @@
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"justgage/tachyons-elm": "4.1.3",
"rtfeldman/elm-css": "16.0.1"
"rtfeldman/elm-css": "16.0.1",
"tripokey/elm-fuzzy": "5.2.1"
},
"indirect": {
"Skinney/murmur3": "2.0.8",
......
......@@ -2,9 +2,10 @@ module Main exposing (main)
import Browser
import Dict
import Fuzzy exposing (match)
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (class, classList)
import Html.Styled.Events exposing (onMouseDown)
import Html.Styled.Events exposing (onInput, onMouseDown)
import Http
import Json.Decode as Decode
import Tachyons.Classes as T
......@@ -12,6 +13,7 @@ import Tachyons.Classes as T
type alias Model =
{ series : List String
, searchedSeries : List String
, selectedSeries : List String
, status : Maybe String
}
......@@ -24,6 +26,7 @@ type alias SeriesCatalog =
type Msg
= CatalogReceived (Result Http.Error SeriesCatalog)
| ToggleItem String
| SearchSeries String
classes : List String -> Attribute msg
......@@ -43,10 +46,17 @@ update msg model =
newModel x =
( x, Cmd.none )
fuzzyMatch xm xs =
List.sortBy (\x -> match [] [] xm x |> .score) xs
in
case msg of
CatalogReceived (Ok x) ->
newModel { model | series = Dict.keys x }
let
series =
Dict.keys x
in
newModel { model | series = series, searchedSeries = series }
CatalogReceived (Err _) ->
newModel { model | status = Just "Error on CatalogReceived" }
......@@ -54,6 +64,9 @@ update msg model =
ToggleItem x ->
newModel { model | selectedSeries = toggleItem x model.selectedSeries }
SearchSeries x ->
newModel { model | searchedSeries = fuzzyMatch x model.series }
view : Model -> Html Msg
view model =
......@@ -81,13 +94,25 @@ view model =
classes [ T.mw5, T.mw6_ns, T.center, T.pt4 ]
div_class =
classes [ T.aspect_ratio, T.aspect_ratio__1x1, T.mb4, T.cb ]
classes [ T.aspect_ratio, T.aspect_ratio__1x1, T.mb4 ]
cols =
List.map (\x -> div [ classes [ T.fl, T.w_50, T.pa1 ] ] [ x ])
[ renderSeries model.series, renderSeries model.selectedSeries ]
fuzzySelector =
let
input_class =
classes [ T.input_reset, T.ba, T.b__black_20, T.pa2, T.db, T.w_100 ]
searchInput =
input [ input_class, onInput SearchSeries ] []
cols =
List.map (\x -> div [ classes [ T.dtc, T.pa1 ] ] [ renderSeries x ])
[ model.searchedSeries, model.selectedSeries ]
in
[ div [ classes [ T.dt, T.dt__fixed ] ] [ searchInput ]
, div [ classes [ T.dt, T.dt__fixed ] ] cols
]
in
article [ article_class ] [ div [ div_class ] cols ]
article [ article_class ] [ div [ div_class ] fuzzySelector ]
main : Program () Model Msg
......@@ -100,7 +125,7 @@ main =
}
init _ =
( Model [] [] Nothing, initialGet )
( Model [] [] [] Nothing, initialGet )
in
Browser.element
{ init = init
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment