Make presidents searchable by name

This commit is contained in:
Evan Czaplicki 2016-07-20 16:33:06 -07:00
parent a0ac286ac3
commit 0ea9f2c904
1 changed files with 38 additions and 11 deletions

View File

@ -1,5 +1,8 @@
import Html exposing (Html, div, h1, text) import Html exposing (Html, div, h1, input, text)
import Html.App as App import Html.App as App
import Html.Attributes exposing (placeholder, value)
import Html.Events exposing (onInput)
import String
import Table import Table
@ -20,14 +23,20 @@ main =
type alias Model = type alias Model =
{ people : List Person { people : List Person
, tableState : Table.State , tableState : Table.State
, query : String
} }
init : List Person -> ( Model, Cmd Msg ) init : List Person -> ( Model, Cmd Msg )
init people = init people =
( Model people (Table.initialSort "Year") let
, Cmd.none model =
) { people = people
, tableState = Table.initialSort "Year"
, query = ""
}
in
( model, Cmd.none )
@ -35,14 +44,22 @@ init people =
type Msg type Msg
= UpdateTableState Table.State = UpdateQuery String
| UpdateTableState Table.State
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
update msg model = update msg model =
case msg of case msg of
UpdateQuery newQuery ->
( { model | query = String.toLower newQuery }
, Cmd.none
)
UpdateTableState newState -> UpdateTableState newState ->
( Model model.people newState, Cmd.none ) ( { model | tableState = newState }
, Cmd.none
)
@ -50,10 +67,20 @@ update msg model =
view : Model -> Html Msg view : Model -> Html Msg
view {people, tableState} = view { people, tableState, query } =
let
acceptablePeople =
List.filter (String.contains query << String.toLower << .name) people
in
div [] div []
[ h1 [] [ text "Birthplaces of U.S. Presidents" ] [ h1 [] [ text "Birthplaces of U.S. Presidents" ]
, Table.view config tableState people , input
[ value query
, placeholder "Search by Name"
, onInput UpdateQuery
]
[]
, Table.view config tableState acceptablePeople
] ]