2018-12-09 21:29:34 +00:00
|
|
|
import Browser exposing (element)
|
2016-07-20 23:33:06 +00:00
|
|
|
import Html exposing (Html, div, h1, input, text)
|
2016-07-20 23:57:24 +00:00
|
|
|
import Html.Attributes exposing (placeholder)
|
2016-07-20 23:33:06 +00:00
|
|
|
import Html.Events exposing (onInput)
|
2016-07-20 21:59:04 +00:00
|
|
|
import Table
|
|
|
|
|
|
|
|
main =
|
2018-12-09 21:29:34 +00:00
|
|
|
Browser.element
|
|
|
|
{ init = (init presidents)
|
2016-07-20 21:59:04 +00:00
|
|
|
, update = update
|
|
|
|
, view = view
|
|
|
|
, subscriptions = \_ -> Sub.none
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-- MODEL
|
|
|
|
|
|
|
|
|
|
|
|
type alias Model =
|
|
|
|
{ people : List Person
|
|
|
|
, tableState : Table.State
|
2016-07-20 23:33:06 +00:00
|
|
|
, query : String
|
2016-07-20 21:59:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 21:29:34 +00:00
|
|
|
type alias Flags = {}
|
2016-07-20 21:59:04 +00:00
|
|
|
|
2018-12-09 21:29:34 +00:00
|
|
|
init : (List Person) -> Flags -> ( Model, Cmd Msg )
|
2018-12-09 21:36:55 +00:00
|
|
|
init people _ =
|
2016-07-20 23:33:06 +00:00
|
|
|
let
|
|
|
|
model =
|
|
|
|
{ people = people
|
|
|
|
, tableState = Table.initialSort "Year"
|
|
|
|
, query = ""
|
|
|
|
}
|
|
|
|
in
|
|
|
|
( model, Cmd.none )
|
2016-07-20 21:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- UPDATE
|
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
2016-07-20 23:57:24 +00:00
|
|
|
= SetQuery String
|
|
|
|
| SetTableState Table.State
|
2016-07-20 21:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
2016-07-20 23:57:24 +00:00
|
|
|
SetQuery newQuery ->
|
|
|
|
( { model | query = newQuery }
|
2016-07-20 23:33:06 +00:00
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2016-07-20 23:57:24 +00:00
|
|
|
SetTableState newState ->
|
2016-07-20 23:33:06 +00:00
|
|
|
( { model | tableState = newState }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2016-07-20 21:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- VIEW
|
|
|
|
|
|
|
|
|
|
|
|
view : Model -> Html Msg
|
2016-07-20 23:33:06 +00:00
|
|
|
view { people, tableState, query } =
|
|
|
|
let
|
2016-07-20 23:57:24 +00:00
|
|
|
lowerQuery =
|
|
|
|
String.toLower query
|
|
|
|
|
2016-07-20 23:33:06 +00:00
|
|
|
acceptablePeople =
|
2016-07-20 23:57:24 +00:00
|
|
|
List.filter (String.contains lowerQuery << String.toLower << .name) people
|
2016-07-20 23:33:06 +00:00
|
|
|
in
|
|
|
|
div []
|
|
|
|
[ h1 [] [ text "Birthplaces of U.S. Presidents" ]
|
2016-07-20 23:57:24 +00:00
|
|
|
, input [ placeholder "Search by Name", onInput SetQuery ] []
|
2016-07-20 23:33:06 +00:00
|
|
|
, Table.view config tableState acceptablePeople
|
|
|
|
]
|
2016-07-20 21:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
config : Table.Config Person Msg
|
|
|
|
config =
|
|
|
|
Table.config
|
|
|
|
{ toId = .name
|
2016-07-20 23:57:24 +00:00
|
|
|
, toMsg = SetTableState
|
2016-07-20 21:59:04 +00:00
|
|
|
, columns =
|
|
|
|
[ Table.stringColumn "Name" .name
|
|
|
|
, Table.intColumn "Year" .year
|
|
|
|
, Table.stringColumn "City" .city
|
|
|
|
, Table.stringColumn "State" .state
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- PEOPLE
|
|
|
|
|
|
|
|
|
|
|
|
type alias Person =
|
|
|
|
{ name : String
|
|
|
|
, year : Int
|
|
|
|
, city : String
|
|
|
|
, state : String
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
presidents : List Person
|
|
|
|
presidents =
|
|
|
|
[ Person "George Washington" 1732 "Westmoreland County" "Virginia"
|
|
|
|
, Person "John Adams" 1735 "Braintree" "Massachusetts"
|
|
|
|
, Person "Thomas Jefferson" 1743 "Shadwell" "Virginia"
|
|
|
|
, Person "James Madison" 1751 "Port Conway" "Virginia"
|
|
|
|
, Person "James Monroe" 1758 "Monroe Hall" "Virginia"
|
|
|
|
, Person "Andrew Jackson" 1767 "Waxhaws Region" "South/North Carolina"
|
|
|
|
, Person "John Quincy Adams" 1767 "Braintree" "Massachusetts"
|
|
|
|
, Person "William Henry Harrison" 1773 "Charles City County" "Virginia"
|
|
|
|
, Person "Martin Van Buren" 1782 "Kinderhook" "New York"
|
|
|
|
, Person "Zachary Taylor" 1784 "Barboursville" "Virginia"
|
|
|
|
, Person "John Tyler" 1790 "Charles City County" "Virginia"
|
|
|
|
, Person "James Buchanan" 1791 "Cove Gap" "Pennsylvania"
|
|
|
|
, Person "James K. Polk" 1795 "Pineville" "North Carolina"
|
|
|
|
, Person "Millard Fillmore" 1800 "Summerhill" "New York"
|
|
|
|
, Person "Franklin Pierce" 1804 "Hillsborough" "New Hampshire"
|
|
|
|
, Person "Andrew Johnson" 1808 "Raleigh" "North Carolina"
|
|
|
|
, Person "Abraham Lincoln" 1809 "Sinking spring" "Kentucky"
|
|
|
|
, Person "Ulysses S. Grant" 1822 "Point Pleasant" "Ohio"
|
|
|
|
, Person "Rutherford B. Hayes" 1822 "Delaware" "Ohio"
|
|
|
|
, Person "Chester A. Arthur" 1829 "Fairfield" "Vermont"
|
|
|
|
, Person "James A. Garfield" 1831 "Moreland Hills" "Ohio"
|
|
|
|
, Person "Benjamin Harrison" 1833 "North Bend" "Ohio"
|
|
|
|
, Person "Grover Cleveland" 1837 "Caldwell" "New Jersey"
|
|
|
|
, Person "William McKinley" 1843 "Niles" "Ohio"
|
|
|
|
, Person "Woodrow Wilson" 1856 "Staunton" "Virginia"
|
|
|
|
, Person "William Howard Taft" 1857 "Cincinnati" "Ohio"
|
|
|
|
, Person "Theodore Roosevelt" 1858 "New York City" "New York"
|
|
|
|
, Person "Warren G. Harding" 1865 "Blooming Grove" "Ohio"
|
|
|
|
, Person "Calvin Coolidge" 1872 "Plymouth" "Vermont"
|
|
|
|
, Person "Herbert Hoover" 1874 "West Branch" "Iowa"
|
|
|
|
, Person "Franklin D. Roosevelt" 1882 "Hyde Park" "New York"
|
|
|
|
, Person "Harry S. Truman" 1884 "Lamar" "Missouri"
|
|
|
|
, Person "Dwight D. Eisenhower" 1890 "Denison" "Texas"
|
|
|
|
, Person "Lyndon B. Johnson" 1908 "Stonewall" "Texas"
|
|
|
|
, Person "Ronald Reagan" 1911 "Tampico" "Illinois"
|
|
|
|
, Person "Richard M. Nixon" 1913 "Yorba Linda" "California"
|
|
|
|
, Person "Gerald R. Ford" 1913 "Omaha" "Nebraska"
|
|
|
|
, Person "John F. Kennedy" 1917 "Brookline" "Massachusetts"
|
|
|
|
, Person "George H. W. Bush" 1924 "Milton" "Massachusetts"
|
|
|
|
, Person "Jimmy Carter" 1924 "Plains" "Georgia"
|
|
|
|
, Person "George W. Bush" 1946 "New Haven" "Connecticut"
|
|
|
|
, Person "Bill Clinton" 1946 "Hope" "Arkansas"
|
|
|
|
, Person "Barack Obama" 1961 "Honolulu" "Hawaii"
|
2016-11-16 21:54:49 +00:00
|
|
|
, Person "Donald Trump" 1946 "New York City" "New York"
|
|
|
|
]
|