Misc fixes

This commit is contained in:
Richard Feldman
2016-06-25 08:42:42 -07:00
parent af12536f58
commit d3b89f4d14
6 changed files with 80 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
module Main exposing (..) module Main exposing (..)
import ElmHub exposing (..) import ElmHub exposing (..)
import Html.App
main : Program Never main : Program Never
@@ -9,5 +10,5 @@ main =
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, searchFeed initialModel.query ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , subscriptions = \_ -> Sub.none
} }

View File

@@ -18,9 +18,7 @@
// documentation: https://github.com/michael/github // documentation: https://github.com/michael/github
var github = new Github(); var github = new Github();
var app = Elm.Main.embed( var app = Elm.Main.embed(document.getElementById("elm-landing-pad"));
document.getElementById("elm-landing-pad"),
{githubResponse: []});
function searchGithub(query) { function searchGithub(query) {
console.log("Searching for", query); console.log("Searching for", query);

View File

@@ -1,26 +1,23 @@
module ElmHub exposing (..) module ElmHub exposing (..)
import Auth
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property, defaultValue) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http import Auth
import Task exposing (Task) import Task exposing (Task)
import Json.Decode exposing (Decoder) import Json.Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Json.Encode
searchFeed : String -> Cmd Msg getQueryUrl : String -> String
searchFeed query = getQueryUrl query =
let -- See https://developer.github.com/v3/search/#example for how to customize!
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"
in
Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url)
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
@@ -101,34 +98,26 @@ type Msg
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
| HandleSearchResponse (List SearchResult) | SetResults (List SearchResult)
| HandleSearchError Http.Error | SetErrorMessage (Maybe String)
| DoNothing
update : Msg -> Model -> ( Model, Cmd Msg ) update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg )
update msg model = update searchFeed msg model =
case msg of case msg of
Search -> Search ->
( model, searchFeed model.query ) ( model, searchFeed (getQueryUrl 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 -> SetQuery query ->
( { model | query = query }, Cmd.none ) ( { model | query = query }, Cmd.none )
SetResults results ->
( { model | results = results }, Cmd.none )
SetErrorMessage errorMessage ->
( { model | errorMessage = errorMessage }, Cmd.none )
DeleteById idToHide -> DeleteById idToHide ->
let let
newResults = newResults =
@@ -139,3 +128,15 @@ update msg model =
{ model | results = newResults } { model | results = newResults }
in in
( newModel, Cmd.none ) ( 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!")

View File

@@ -1,13 +1,31 @@
module Main exposing (..) port module Main exposing (..)
import ElmHub exposing (..) import ElmHub exposing (..)
import Html.App
import Json.Decode
main : Program Never main : Program Never
main = main =
Html.App.program Html.App.program
{ view = view { view = view
, update = update , update = update githubSearch
, init = ( initialModel, searchFeed initialModel.query ) , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) )
, inputs = [] , 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

2
part8/github.js Normal file

File diff suppressed because one or more lines are too long

View File

@@ -4,17 +4,34 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>ElmHub</title> <title>ElmHub</title>
<script type="text/javascript" src="github.js"></script>
<script type="text/javascript" src="elm.js"></script> <script type="text/javascript" src="elm.js"></script>
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<link rel="icon" type="image/png" href="elm-hub.png"> <link rel="icon" type="image/png" href="elm-hub.png">
</head> </head>
<body> <body>
<div id="elm-landing-pad"></div>
</body> </body>
<script type="text/javascript"> <script type="text/javascript">
var app = Elm.Main.fullscreen(); // documentation: https://github.com/michael/github
var github = new Github();
var app = Elm.Main.embed(document.getElementById("elm-landing-pad"));
function searchGithub(query) {
console.log("Searching for", query);
var search = github.getSearch(query);
search.repositories({}, function (err, repositories) {
console.log("Got response", repositories);
app.ports.githubResponse.send(repositories);
});
}
app.ports.githubSearch.subscribe(searchGithub);
</script> </script>
</html> </html>