Shift everything forward a partg
This commit is contained in:
110
part7/Main.elm
110
part7/Main.elm
@@ -1,11 +1,12 @@
|
||||
port module Main exposing (..)
|
||||
module Main exposing (..)
|
||||
|
||||
import Html.App as Html
|
||||
import Json.Decode exposing (..)
|
||||
import Auth
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (class, target, href, property, defaultValue)
|
||||
import Html.Events exposing (..)
|
||||
import Auth
|
||||
import Http
|
||||
import Html.App as Html
|
||||
import Task exposing (Task)
|
||||
import Json.Decode exposing (Decoder)
|
||||
import Json.Decode.Pipeline exposing (..)
|
||||
|
||||
@@ -15,19 +16,34 @@ main =
|
||||
Html.program
|
||||
{ view = view
|
||||
, update = update
|
||||
, init = ( initialModel, githubSearch (getQueryString initialModel.query) )
|
||||
, subscriptions = \_ -> githubResponse decodeResponse
|
||||
, init = ( initialModel, searchFeed initialModel.query )
|
||||
, subscriptions = \_ -> Sub.none
|
||||
}
|
||||
|
||||
|
||||
getQueryString : String -> String
|
||||
getQueryString query =
|
||||
-- See https://developer.github.com/v3/search/#example for how to customize!
|
||||
"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"
|
||||
|
||||
-- Hint: responseDecoder may be useful here
|
||||
task =
|
||||
"TODO replace this String with a Task using http://package.elm-lang.org/packages/evancz/elm-http/latest/Http#get"
|
||||
in
|
||||
-- TODO replace this Cmd.none with a call to Task.perform
|
||||
-- http://package.elm-lang.org/packages/elm-lang/core/4.0.1/Task#perform
|
||||
--
|
||||
-- Hint: pass these to Task.perform, but in a different order than this!
|
||||
--
|
||||
-- task
|
||||
-- HandleSearchResponse
|
||||
-- HandleSearchError
|
||||
Cmd.none
|
||||
|
||||
|
||||
responseDecoder : Decoder (List SearchResult)
|
||||
@@ -100,51 +116,43 @@ viewSearchResult result =
|
||||
]
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
Search ->
|
||||
( model, githubSearch (getQueryString model.query) )
|
||||
|
||||
SetQuery query ->
|
||||
( { model | query = query }, Cmd.none )
|
||||
|
||||
HandleSearchResponse results ->
|
||||
( { model | results = results }, Cmd.none )
|
||||
|
||||
HandleSearchError error ->
|
||||
( { model | errorMessage = error }, Cmd.none )
|
||||
|
||||
DeleteById idToDelete ->
|
||||
let
|
||||
newResults =
|
||||
model.results
|
||||
|> List.filter (\{ id } -> id /= idToDelete)
|
||||
|
||||
newModel =
|
||||
{ model | results = newResults }
|
||||
in
|
||||
( newModel, Cmd.none )
|
||||
|
||||
|
||||
type Msg
|
||||
= Search
|
||||
| SetQuery String
|
||||
| DeleteById Int
|
||||
| HandleSearchResponse (List SearchResult)
|
||||
| HandleSearchError (Maybe String)
|
||||
| HandleSearchError Http.Error
|
||||
|
||||
|
||||
decodeResponse : Value -> Msg
|
||||
decodeResponse json =
|
||||
-- TODO use decodeValue to decode the response into a Msg.
|
||||
--
|
||||
-- Hint: look at the definition of Msg and
|
||||
-- the definition of responseDecoder
|
||||
HandleSearchError (Just "TODO decode the response!")
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
Search ->
|
||||
( model, searchFeed model.query )
|
||||
|
||||
HandleSearchResponse results ->
|
||||
( { model | results = results }, Cmd.none )
|
||||
|
||||
port githubSearch : String -> Cmd msg
|
||||
HandleSearchError error ->
|
||||
-- TODO if decoding failed, store the message in model.errorMessage
|
||||
--
|
||||
-- Hint 1: look for "decode" in the documentation for this union type:
|
||||
-- http://package.elm-lang.org/packages/evancz/elm-http/latest/Http#Error
|
||||
--
|
||||
-- Hint 2: to check if this is working, break responseDecoder
|
||||
-- by changing "stargazers_count" to "description"
|
||||
( model, Cmd.none )
|
||||
|
||||
SetQuery query ->
|
||||
( { model | query = query }, Cmd.none )
|
||||
|
||||
port githubResponse : (Value -> msg) -> Sub msg
|
||||
DeleteById idToHide ->
|
||||
let
|
||||
newResults =
|
||||
model.results
|
||||
|> List.filter (\{ id } -> id /= idToHide)
|
||||
|
||||
newModel =
|
||||
{ model | results = newResults }
|
||||
in
|
||||
( newModel, Cmd.none )
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Part 7
|
||||
Part 6
|
||||
======
|
||||
|
||||
The instructor will paste notes from the lesson, including code examples from
|
||||
@@ -18,3 +18,11 @@ elm-package install
|
||||
```bash
|
||||
elm-live Main.elm --open --output=elm.js
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
* [**let-expressions**](http://elm-lang.org/docs/syntax#let-expressions)
|
||||
* [**case-expressions** and **if-expressions**](http://elm-lang.org/docs/syntax#conditionals)
|
||||
* [HTTP Tasks tutorial](http://elm-lang.org/guide/reactivity#http-tasks)
|
||||
* [HTTP Error documentation](http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#Error)
|
||||
* [Modules syntax reference](http://elm-lang.org/docs/syntax#modules)
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -4,33 +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"));
|
||||
|
||||
function searchGithub(query) {
|
||||
console.log("Searching for", query);
|
||||
|
||||
github.getSearch(query).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>
|
||||
|
||||
Reference in New Issue
Block a user