diff --git a/part7/ElmHub.elm b/part7/ElmHub.elm index 819af7e..ef71143 100644 --- a/part7/ElmHub.elm +++ b/part7/ElmHub.elm @@ -1,26 +1,23 @@ module ElmHub exposing (..) -import Auth import Html exposing (..) import Html.Attributes exposing (class, target, href, property, defaultValue) import Html.Events exposing (..) -import Http +import Auth import Task exposing (Task) import Json.Decode exposing (Decoder) import Json.Decode.Pipeline exposing (..) +import Json.Encode -searchFeed : String -> Cmd Msg -searchFeed query = - let - url = - "https://api.github.com/search/repositories?access_token=" - ++ Auth.token - ++ "&q=" - ++ query - ++ "+language:elm&sort=stars&order=desc" - in - Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url) +getQueryUrl : String -> String +getQueryUrl query = + -- See https://developer.github.com/v3/search/#example for how to customize! + "https://api.github.com/search/repositories?access_token=" + ++ Auth.token + ++ "&q=" + ++ query + ++ "+language:elm&sort=stars&order=desc" responseDecoder : Decoder (List SearchResult) @@ -101,34 +98,26 @@ type Msg = Search | SetQuery String | DeleteById ResultId - | HandleSearchResponse (List SearchResult) - | HandleSearchError Http.Error + | SetResults (List SearchResult) + | SetErrorMessage (Maybe String) + | DoNothing -update : Msg -> Model -> ( Model, Cmd Msg ) -update msg model = +update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg ) +update searchFeed msg model = case msg of Search -> - ( model, searchFeed model.query ) - - HandleSearchResponse results -> - ( { model | results = results }, Cmd.none ) - - HandleSearchError error -> - let - errorMessage = - case error of - Http.UnexpectedPayload message -> - Just message - - _ -> - Nothing - in - ( { model | errorMessage = errorMessage }, Cmd.none ) + ( model, searchFeed (getQueryUrl model.query) ) SetQuery query -> ( { model | query = query }, Cmd.none ) + SetResults results -> + ( { model | results = results }, Cmd.none ) + + SetErrorMessage errorMessage -> + ( { model | errorMessage = errorMessage }, Cmd.none ) + DeleteById idToHide -> let newResults = @@ -139,3 +128,15 @@ update msg model = { model | results = newResults } in ( newModel, Cmd.none ) + + DoNothing -> + ( model, Cmd.none ) + + +decodeGithubResponse : Json.Encode.Value -> Msg +decodeGithubResponse value = + -- TODO use Json.Decode.DecodeValue to decode the response into an Action. + -- + -- Hint: look at ElmHub.elm, specifically the definition of Action and + -- the deefinition of responseDecoder + SetErrorMessage (Just "TODO decode the response!") diff --git a/part7/Main.elm b/part7/Main.elm index c60133a..a736ba5 100644 --- a/part7/Main.elm +++ b/part7/Main.elm @@ -1,13 +1,31 @@ -module Main exposing (..) +port module Main exposing (..) import ElmHub exposing (..) +import Html.App +import Json.Decode main : Program Never main = Html.App.program { view = view - , update = update - , init = ( initialModel, searchFeed initialModel.query ) - , inputs = [] + , update = update githubSearch + , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) ) + , subscriptions = \_ -> githubResponse decodeResponse } + + +decodeResponse : Json.Decode.Value -> Msg +decodeResponse json = + case Json.Decode.decodeValue responseDecoder json of + Err err -> + SetErrorMessage (Just err) + + Ok results -> + SetResults results + + +port githubSearch : String -> Cmd msg + + +port githubResponse : (Json.Decode.Value -> msg) -> Sub msg diff --git a/part7/README.md b/part7/README.md index 6ef1372..d52699c 100644 --- a/part7/README.md +++ b/part7/README.md @@ -18,17 +18,3 @@ to fail; in that case, just run `elm-package install` again.) ```bash elm-live Main.elm --open --output=elm.js ``` - -## Running Tests - -```bash -cd test -elm-package install -elm test TestRunner.elm -``` - -## References - -* [Using Elm packages](https://github.com/elm-lang/elm-package/blob/master/README.md#basic-usage) -* [elm-test documentation](http://package.elm-lang.org/packages/deadfoxygrandpa/elm-test/3.1.1/) -* [`(<|)` documentation](http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Basics#<|) diff --git a/part8/github.js b/part7/github.js similarity index 100% rename from part8/github.js rename to part7/github.js diff --git a/part7/index.html b/part7/index.html index 93fc00c..f717a9a 100644 --- a/part7/index.html +++ b/part7/index.html @@ -4,17 +4,36 @@