Make it so you can search for different queries.

This commit is contained in:
Richard Feldman
2016-03-05 05:12:57 -08:00
parent 3fc26018d3
commit 555c9af284

View File

@@ -8,6 +8,7 @@ import Http
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=)) import Json.Decode exposing (Decoder, (:=))
import Json.Encode
import Signal exposing (Address) import Signal exposing (Address)
@@ -21,7 +22,7 @@ app =
StartApp.start StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed "") ) , init = ( initialModel, Effects.task (searchFeed initialModel.query) )
, inputs = [] , inputs = []
} }
@@ -36,7 +37,9 @@ searchFeed query =
let let
-- See https://developer.github.com/v3/search/#example for how to customize! -- See https://developer.github.com/v3/search/#example for how to customize!
url = url =
"https://api.github.com/search/repositories?q=tutorial+language:elm&sort=stars&order=desc" "https://api.github.com/search/repositories?q="
++ query
++ "+language:elm&sort=stars&order=desc"
task = task =
Http.get responseDecoder url Http.get responseDecoder url
@@ -59,7 +62,9 @@ searchResultDecoder =
type alias Model = type alias Model =
{ results : List SearchResult } { query : String
, results : List SearchResult
}
type alias SearchResult = type alias SearchResult =
@@ -74,7 +79,9 @@ type alias ResultId =
initialModel : Model initialModel : Model
initialModel = initialModel =
{ results = [] } { query = "tutorial"
, results = []
}
view : Address Action -> Model -> Html view : Address Action -> Model -> Html
@@ -82,19 +89,30 @@ view address model =
div div
[] []
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, input [ onInput address SetQuery, defaultValue model.query ] []
, button [ onClick address Search ] [ text "Search" ]
, div , div
[ class "results" ] [ class "results" ]
(List.map viewSearchResult model.results) (List.map viewSearchResult model.results)
] ]
onInput address wrap =
on "input" targetValue (\val -> Signal.message address (wrap val))
defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : SearchResult -> Html viewSearchResult : SearchResult -> Html
viewSearchResult result = viewSearchResult result =
div [] [ text result.name ] div [] [ text result.name ]
type Action type Action
= Search String = Search
| SetQuery String
| HideById ResultId | HideById ResultId
| SetResults (List SearchResult) | SetResults (List SearchResult)
@@ -102,8 +120,11 @@ type Action
update : Action -> Model -> ( Model, Effects Action ) update : Action -> Model -> ( Model, Effects Action )
update action model = update action model =
case action of case action of
Search query -> Search ->
( model, Effects.task (searchFeed query) ) ( model, Effects.task (searchFeed (Debug.log "searching for" model.query)) )
SetQuery query ->
( { model | query = query }, Effects.none )
SetResults results -> SetResults results ->
let let