Update part6

This commit is contained in:
Richard Feldman
2016-04-03 06:33:20 -07:00
parent 08abf49174
commit 49926901e5
2 changed files with 35 additions and 19 deletions

View File

@@ -16,19 +16,17 @@ import Signal exposing (Address)
searchFeed : String -> Task x Action searchFeed : String -> Task x Action
searchFeed query = searchFeed query =
let let
-- See https://developer.github.com/v3/search/#example for how to customize!
url = url =
"https://api.github.com/search/repositories?access_token=" "https://api.github.com/search/repositories?access_token="
++ Auth.token ++ Auth.token
++ "&q=" ++ "&q="
++ query ++ query
++ "+language:elm&sort=stars&order=desc" ++ "+language:elm&sort=stars&order=desc"
task =
Http.get responseDecoder url
|> Task.map SetResults
in in
Task.onError task (\_ -> Task.succeed (SetResults [])) performAction
(\response -> HandleSearchResponse response)
(\error -> HandleSearchError error)
(Http.get responseDecoder url)
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
@@ -44,9 +42,21 @@ searchResultDecoder =
|> required "stargazers_count" Json.Decode.int |> required "stargazers_count" Json.Decode.int
{-| Note: this will be a standard function in Elm 0.17
-}
performAction : (a -> b) -> (y -> b) -> Task y a -> Task x b
performAction successToAction errorToAction task =
let
successTask =
Task.map successToAction task
in
Task.onError successTask (\err -> Task.succeed (errorToAction err))
type alias Model = type alias Model =
{ query : String { query : String
, results : List SearchResult , results : List SearchResult
, errorMessage : String
} }
@@ -65,6 +75,7 @@ initialModel : Model
initialModel = initialModel =
{ query = "tutorial" { query = "tutorial"
, results = [] , results = []
, errorMessage = ""
} }
@@ -111,7 +122,8 @@ type Action
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
| SetResults (List SearchResult) | HandleSearchResponse (List SearchResult)
| HandleSearchError Http.Error
update : Action -> Model -> ( Model, Effects Action ) update : Action -> Model -> ( Model, Effects Action )
@@ -120,16 +132,20 @@ update action model =
Search -> Search ->
( model, Effects.task (searchFeed model.query) ) ( model, Effects.task (searchFeed model.query) )
HandleSearchResponse response ->
-- TODO update the model to incorporate these search results.
-- Hint: where would you look to find out the type of `response` here?
( model, Effects.none )
HandleSearchError error ->
-- TODO if decoding failed, store the message in model.errorMessage
-- Hint: look for "decode" in the documentation for this union type:
-- http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#Error
( model, Effects.none )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Effects.none )
SetResults results ->
let
newModel =
{ model | results = results }
in
( newModel, Effects.none )
DeleteById idToHide -> DeleteById idToHide ->
let let
newResults = newResults =

View File

@@ -16,9 +16,9 @@ to fail; in that case, just run `elm package install` again.)
elm live Main.elm --open -- --output=elm.js elm live Main.elm --open -- --output=elm.js
``` ```
## Running Tests ## References
```bash * [HTTP Tasks tutorial](http://elm-lang.org/guide/reactivity#http-tasks)
cd test * [HTTP Error documentation](http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#Error)
elm test TestRunner.elm * [Modules syntax reference](http://elm-lang.org/docs/syntax#modules)
``` * [Syntax reference for **case-expressions** and **if-expressions**](http://elm-lang.org/docs/syntax#conditionals)