Swap part7 and part8
This commit is contained in:
@@ -1,23 +1,26 @@
|
||||
module ElmHub exposing (..)
|
||||
|
||||
import Auth
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (class, target, href, property, defaultValue)
|
||||
import Html.Events exposing (..)
|
||||
import Auth
|
||||
import Http
|
||||
import Task exposing (Task)
|
||||
import Json.Decode exposing (Decoder)
|
||||
import Json.Decode.Pipeline exposing (..)
|
||||
import Json.Encode
|
||||
|
||||
|
||||
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"
|
||||
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)
|
||||
|
||||
|
||||
responseDecoder : Decoder (List SearchResult)
|
||||
@@ -98,26 +101,34 @@ type Msg
|
||||
= Search
|
||||
| SetQuery String
|
||||
| DeleteById ResultId
|
||||
| SetResults (List SearchResult)
|
||||
| SetErrorMessage (Maybe String)
|
||||
| DoNothing
|
||||
| HandleSearchResponse (List SearchResult)
|
||||
| HandleSearchError Http.Error
|
||||
|
||||
|
||||
update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg )
|
||||
update searchFeed msg model =
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
Search ->
|
||||
( model, searchFeed (getQueryUrl model.query) )
|
||||
( 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 )
|
||||
|
||||
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 =
|
||||
@@ -128,15 +139,3 @@ update searchFeed 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!")
|
||||
|
||||
@@ -1,31 +1,13 @@
|
||||
port module Main exposing (..)
|
||||
module Main exposing (..)
|
||||
|
||||
import ElmHub exposing (..)
|
||||
import Html.App
|
||||
import Json.Decode exposing (Value)
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
Html.App.program
|
||||
{ view = view
|
||||
, update = update githubSearch
|
||||
, init = ( initialModel, githubSearch (getQueryUrl initialModel.query) )
|
||||
, subscriptions = \_ -> githubResponse decodeResponse
|
||||
, update = update
|
||||
, init = ( initialModel, searchFeed initialModel.query )
|
||||
, inputs = []
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -26,3 +26,9 @@ 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#<|)
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -4,36 +4,17 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>ElmHub</title>
|
||||
<script type="text/javascript" src="github.js"></script>
|
||||
<script type="text/javascript" src="elm.js"></script>
|
||||
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="icon" type="image/png" href="elm-hub.png">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="elm-landing-pad"></div>
|
||||
</body>
|
||||
|
||||
<script type="text/javascript">
|
||||
// documentation: https://github.com/michael/github
|
||||
var github = new Github();
|
||||
|
||||
var app = Elm.Main.embed(
|
||||
document.getElementById("elm-landing-pad"),
|
||||
{githubResponse: []});
|
||||
|
||||
function searchGithub(query) {
|
||||
console.log("Searching for", query);
|
||||
var search = github.getSearch(query);
|
||||
|
||||
search.repositories({}, function (err, repositories) {
|
||||
console.log("Got response", repositories);
|
||||
|
||||
// TODO: app.ports.portNameGoesHere.send(repositories);
|
||||
});
|
||||
}
|
||||
|
||||
// TODO app.ports.portNameGoesHere.subscribe(searchGithub);
|
||||
var app = Elm.Main.fullscreen();
|
||||
</script>
|
||||
|
||||
</html>
|
||||
|
||||
@@ -23,9 +23,8 @@ main =
|
||||
let
|
||||
response =
|
||||
"""{ "items": [
|
||||
{ "id": 5, "full_name": "foo", "stargazers_count": 42 },
|
||||
{ "id": 3, "full_name": "bar", "stargazers_count": 77 }
|
||||
] }"""
|
||||
/* TODO: dummy JSON goes here */
|
||||
] }"""
|
||||
in
|
||||
Expect.equal
|
||||
(Ok
|
||||
@@ -41,14 +40,13 @@ main =
|
||||
"""{ "pizza": [] }"""
|
||||
|
||||
isErrorResult result =
|
||||
case result of
|
||||
Ok _ ->
|
||||
False
|
||||
|
||||
Err _ ->
|
||||
True
|
||||
-- TODO return True if the given Result is an Err of some sort,
|
||||
-- and False if it is an Ok of some sort.
|
||||
--
|
||||
-- Result docs: http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result
|
||||
False
|
||||
in
|
||||
Expect.equal True
|
||||
Expect.true "Expected decoding an invalid response to return an Err."
|
||||
(isErrorResult (decodeString responseDecoder response))
|
||||
]
|
||||
|> Runner.run emit
|
||||
|
||||
Reference in New Issue
Block a user