Update a ton of things

This commit is contained in:
Richard Feldman
2016-06-24 17:40:54 -07:00
parent 908ca61c77
commit c8e9a117f5
56 changed files with 1023 additions and 1466 deletions

View File

@@ -10,7 +10,7 @@ import Html.App
import Auth import Auth
import Http import Http
import Task exposing (Task) import Task exposing (Task)
import Json.Decode exposing (Decoder, (:=)) import Json.Decode exposing (Decoder)
main : Program Never main : Program Never

View File

@@ -5,7 +5,7 @@ Getting Started
1. Install [Node.js](http://nodejs.org) 4.0.0 or higher 1. Install [Node.js](http://nodejs.org) 4.0.0 or higher
2. Add an [editor plugin](http://elm-lang.org/install#syntax-highlighting) for your editor of choice. 2. Add a plugin for your editor of choice: [Atom](https://atom.io/packages/language-elm), [Sublime Text](https://packagecontrol.io/packages/Elm%20Language%20Support), [VS Code](https://github.com/sbrink/vscode-elm), [Light Table](https://github.com/rundis/elm-light), [Vim](https://github.com/lambdatoast/elm.vim), [Emacs](https://github.com/jcollard/elm-mode), [Brackets](https://github.com/lepinay/elm-brackets)
3. Not required, but **highly** recommended: [install elm-format](https://github.com/avh4/elm-format#installation-) and integrate it into your editor so that it runs on save. 3. Not required, but **highly** recommended: [install elm-format](https://github.com/avh4/elm-format#installation-) and integrate it into your editor so that it runs on save.

View File

@@ -16,7 +16,7 @@ model =
view model = view model =
div [ class "content" ] div [ class "content" ]
[ header [] [ header []
[ -- TODO add the equivalent of <h1>ElmHub</h1> right before the tagline [ -- TODO add the equivalent of <h1>ElmHub</h1> right before this <span>
span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, ul [ class "results" ] , ul [ class "results" ]

View File

@@ -4,19 +4,19 @@ Part 1
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References
* [html-to-elm](http://mbylstra.github.io/html-to-elm/) - paste in HTML, get elm-html code * [html-to-elm](http://mbylstra.github.io/html-to-elm/) - paste in HTML, get elm-html code
* [elm-html documentation](http://package.elm-lang.org/packages/evancz/elm-html/4.0.2/) * [elm-html documentation](http://package.elm-lang.org/packages/evancz/elm-html/4.0.2/)
* [record syntax](http://elm-lang.org/docs/syntax#records) (e.g. `{ foo = 1, bar = 2`) * [record syntax](http://elm-lang.org/docs/syntax#records) (e.g. `{ foo = 1, bar = 2 }`)

View File

@@ -1,36 +1,27 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Html.Lazy exposing (..)
import Http import Http
import Auth import Auth
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Encode
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
import SearchResult import SearchResult
searchFeed : String -> Task x Action searchFeed : String -> Cmd Msg
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 [])) Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url)
responseDecoder : Decoder (List SearchResult.Model) responseDecoder : Decoder (List SearchResult.Model)
@@ -52,54 +43,44 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, ul , ul [ class "results" ] (viewSearchResults model.results)
[ class "results" ]
(viewSearchResults address model.results)
] ]
viewSearchResults : Address Action -> Dict SearchResult.ResultId SearchResult.Model -> List Html viewSearchResults : Dict SearchResult.ResultId SearchResult.Model -> List (Html a)
viewSearchResults address results = viewSearchResults results =
results results
|> Dict.values |> Dict.values
|> List.sortBy (.stars >> negate) |> List.sortBy (.stars >> negate)
|> List.map (\_ -> div [] [ text "TODO replace this line with view logic from SearchResult" ]) |> List.map (\_ -> div [] [ text "TODO replace this line with view logic from SearchResult" ])
onInput address wrap = type Msg
on "input" targetValue (\val -> Signal.message address (wrap val))
defaultValue str =
property "defaultValue" (Json.Encode.string str)
type Action
= Search = Search
| SetQuery String | SetQuery String
| DeleteById SearchResult.ResultId | DeleteById SearchResult.ResultId
| SetResults (List SearchResult.Model) | SetResults (List SearchResult.Model)
| HandleSearchResponse (List SearchResult.Model)
| HandleSearchError Http.Error
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, Effects.task (searchFeed model.query) ) ( model, searchFeed model.query )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
let let
@@ -109,11 +90,11 @@ update action model =
|> List.map (\result -> ( result.id, result )) |> List.map (\result -> ( result.id, result ))
|> Dict.fromList |> Dict.fromList
in in
( { model | results = resultsById }, Effects.none ) ( { model | results = resultsById }, Cmd.none )
DeleteById id -> DeleteById id ->
let let
newModel = newModel =
{ model | results = Dict.remove id model.results } { model | results = Dict.remove id model.results }
in in
( newModel, Effects.none ) ( newModel, Cmd.none )

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed initialModel.query) ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,14 +4,14 @@ Part 10
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```

View File

@@ -1,12 +1,9 @@
module SearchResult (..) where module SearchResult exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Signal exposing (Address)
import Dict exposing (Dict)
type alias ResultId = type alias ResultId =
@@ -28,10 +25,9 @@ decoder =
|> required "stargazers_count" Json.Decode.int |> required "stargazers_count" Json.Decode.int
view : Address a -> Model -> Html view : Model -> Html a
view address result = view result =
li li []
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a
[ href ("https://github.com/" ++ result.name) [ href ("https://github.com/" ++ result.name)

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,13 +9,10 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,4 +1,4 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
@@ -6,30 +6,23 @@ import Html.Events exposing (..)
import Http import Http
import Auth import Auth
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Encode import Json.Encode
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
import SearchResult import SearchResult
searchFeed : String -> Task x Action searchFeed : String -> Cmd Msg
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 [])) Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url)
responseDecoder : Decoder (List SearchResult.Model) responseDecoder : Decoder (List SearchResult.Model)
@@ -50,25 +43,21 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, ul , ul [ class "results" ] (viewSearchResults model.results)
[ class "results" ]
(viewSearchResults address model.results)
] ]
viewSearchResults : Address Action -> Dict SearchResult.ResultId SearchResult.Model -> List Html viewSearchResults : Dict SearchResult.ResultId SearchResult.Model -> List (Html a)
viewSearchResults address results = viewSearchResults results =
results results
|> Dict.values |> Dict.values
|> List.sortBy (.stars >> negate) |> List.sortBy (.stars >> negate)
@@ -83,29 +72,21 @@ filterResults results =
results results
onInput address wrap = type Msg
on "input" targetValue (\val -> Signal.message address (wrap val))
defaultValue str =
property "defaultValue" (Json.Encode.string str)
type Action
= Search = Search
| SetQuery String | SetQuery String
| DeleteById SearchResult.ResultId | DeleteById SearchResult.ResultId
| SetResults (List SearchResult.Model) | SetResults (List SearchResult.Model)
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, Effects.task (searchFeed model.query) ) ( model, searchFeed model.query )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
let let
@@ -115,11 +96,11 @@ update action model =
|> List.map (\result -> ( result.id, result )) |> List.map (\result -> ( result.id, result ))
|> Dict.fromList |> Dict.fromList
in in
( { model | results = resultsById }, Effects.none ) ( { model | results = resultsById }, Cmd.none )
DeleteById id -> DeleteById id ->
let let
newModel = newModel =
{ model | results = Dict.remove id model.results } { model | results = Dict.remove id model.results }
in in
( newModel, Effects.none ) ( newModel, Cmd.none )

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed initialModel.query) ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,14 +4,14 @@ Part 11
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```

View File

@@ -1,10 +1,9 @@
module SearchResult (..) where module SearchResult exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (..) import Html.Events exposing (..)
import Json.Decode exposing (Decoder, (:=)) import Json.Decode exposing (Decoder)
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
@@ -21,8 +20,7 @@ type alias Model =
decoder : Decoder Model decoder : Decoder Model
decoder = decoder =
Json.Decode.object3 Json.Decode.object3 Model
Model
("id" := Json.Decode.int) ("id" := Json.Decode.int)
("full_name" := Json.Decode.string) ("full_name" := Json.Decode.string)
("stargazers_count" := Json.Decode.int) ("stargazers_count" := Json.Decode.int)
@@ -30,21 +28,18 @@ decoder =
view : Address a -> (Int -> a) -> Model -> Html view : Address a -> (Int -> a) -> Model -> Html
view address delete result = view address delete result =
li li []
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a
[ href [ href
("https://github.com/" ("https://github.com/"
++ (Debug.log ++ (Debug.log "TODO we should not see this when typing in the search box!"
"TODO we should not see this when typing in the search box!"
result.name result.name
) )
) )
, target "_blank" , target "_blank"
] ]
[ text result.name ] [ text result.name ]
, button , button [ class "hide-result", onClick (delete result.id) ]
[ class "hide-result", onClick address (delete result.id) ]
[ text "X" ] [ text "X" ]
] ]

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,13 +9,10 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,4 +1,4 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
@@ -7,15 +7,13 @@ import Html.Lazy exposing (..)
import Http import Http
import Auth import Auth
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Encode import Json.Encode
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
import SearchResult exposing (ResultId) import SearchResult exposing (ResultId)
searchFeed : String -> Task x Action searchFeed : String -> Task x Msg
searchFeed query = searchFeed query =
let let
-- See https://developer.github.com/v3/search/#example for how to customize! -- See https://developer.github.com/v3/search/#example for how to customize!
@@ -51,61 +49,48 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, ul , ul [ class "results" ] (viewSearchResults model.results)
[ class "results" ]
(viewSearchResults address model.results)
] ]
viewSearchResults : Address Action -> Dict ResultId SearchResult.Model -> List Html viewSearchResults : Dict ResultId SearchResult.Model -> List Html
viewSearchResults address results = viewSearchResults results =
results results
|> Dict.values |> Dict.values
|> List.sortBy (.stars >> negate) |> List.sortBy (.stars >> negate)
|> List.map (viewSearchResult address) |> List.map (viewSearchResult address)
viewSearchResult : Address Action -> SearchResult.Model -> Html viewSearchResult : SearchResult.Model -> Html Msg
viewSearchResult address result = viewSearchResult result =
SearchResult.view SearchResult.view (Signal.forwardTo address (UpdateSearchResult result.id))
(Signal.forwardTo address (UpdateSearchResult result.id))
result result
onInput address wrap = type Msg
on "input" targetValue (\val -> Signal.message address (wrap val))
defaultValue str =
property "defaultValue" (Json.Encode.string str)
type Action
= Search = Search
| SetQuery String | SetQuery String
| SetResults (List SearchResult.Model) | SetResults (List SearchResult.Model)
| UpdateSearchResult ResultId SearchResult.Action | UpdateSearchResult ResultId SearchResult.Msg
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, Effects.task (searchFeed model.query) ) ( model, searchFeed model.query )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
let let
@@ -115,18 +100,18 @@ update action model =
|> List.map (\result -> ( result.id, result )) |> List.map (\result -> ( result.id, result ))
|> Dict.fromList |> Dict.fromList
in in
( { model | results = resultsById }, Effects.none ) ( { model | results = resultsById }, Cmd.none )
UpdateSearchResult id childAction -> UpdateSearchResult id childMsg ->
let let
updated = updated =
model.results model.results
|> Dict.get id |> Dict.get id
|> Maybe.map (SearchResult.update childAction) |> Maybe.map (SearchResult.update childMsg)
in in
case updated of case updated of
Nothing -> Nothing ->
( model, Effects.none ) ( model, Cmd.none )
Just ( newChildModel, childEffects ) -> Just ( newChildModel, childEffects ) ->
let let

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed initialModel.query) ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,16 +4,16 @@ Part 12
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References

View File

@@ -1,11 +1,9 @@
module SearchResult (..) where module SearchResult exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Signal exposing (Address) import Json.Decode exposing (Decoder)
import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
@@ -21,7 +19,7 @@ type alias ResultId =
Int Int
type Action type Msg
= Expand = Expand
| Collapse | Collapse
@@ -35,20 +33,18 @@ decoder =
|> hardcoded True |> hardcoded True
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
-- TODO implement Expand and Collapse logic -- TODO implement Expand and Collapse logic
( model, Effects.none ) ( model, Cmd.none )
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
li li [] <|
[] if model.expanded then
<| if model.expanded then
[ span [ class "star-count" ] [ text (toString model.stars) ] [ span [ class "star-count" ] [ text (toString model.stars) ]
, a , a [ href ("https://github.com/" ++ model.name), target "_blank" ]
[ href ("https://github.com/" ++ model.name), target "_blank" ]
[ text model.name ] [ text model.name ]
, button , button
-- TODO when the user clicks, send a Collapse action -- TODO when the user clicks, send a Collapse action

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,13 +9,10 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,4 +1,4 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
@@ -7,15 +7,13 @@ import Html.Lazy exposing (..)
import Http import Http
import Auth import Auth
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Encode import Json.Encode
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
import SearchResult exposing (ResultId) import SearchResult exposing (ResultId)
searchFeed : String -> Task x Action searchFeed : String -> Task x Msg
searchFeed query = searchFeed query =
let let
-- See https://developer.github.com/v3/search/#example for how to customize! -- See https://developer.github.com/v3/search/#example for how to customize!
@@ -51,25 +49,21 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, ul , ul [ class "results" ] (viewSearchResults model.results)
[ class "results" ]
(viewSearchResults address model.results)
] ]
viewSearchResults : Address Action -> Dict ResultId SearchResult.Model -> List Html viewSearchResults : Dict ResultId SearchResult.Model -> List Html
viewSearchResults address results = viewSearchResults results =
results results
|> Dict.values |> Dict.values
|> List.sortBy (.stars >> negate) |> List.sortBy (.stars >> negate)
@@ -77,35 +71,26 @@ viewSearchResults address results =
viewSearchResult : Address Action -> SearchResult.Model -> Html viewSearchResult : Address Action -> SearchResult.Model -> Html
viewSearchResult address result = viewSearchResult result =
SearchResult.view SearchResult.view (Signal.forwardTo address (UpdateSearchResult result.id))
(Signal.forwardTo address (UpdateSearchResult result.id))
result result
onInput address wrap = type Msg
on "input" targetValue (\val -> Signal.message address (wrap val))
defaultValue str =
property "defaultValue" (Json.Encode.string str)
type Action
= Search = Search
| SetQuery String | SetQuery String
| SetResults (List SearchResult.Model) | SetResults (List SearchResult.Model)
| UpdateSearchResult ResultId SearchResult.Action | UpdateSearchResult ResultId SearchResult.Action
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, Effects.task (searchFeed model.query) ) ( model, searchFeed model.query )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
let let
@@ -115,7 +100,7 @@ update action model =
|> List.map (\result -> ( result.id, result )) |> List.map (\result -> ( result.id, result ))
|> Dict.fromList |> Dict.fromList
in in
( { model | results = resultsById }, Effects.none ) ( { model | results = resultsById }, Cmd.none )
UpdateSearchResult id childAction -> UpdateSearchResult id childAction ->
let let
@@ -126,7 +111,7 @@ update action model =
in in
case updated of case updated of
Nothing -> Nothing ->
( model, Effects.none ) ( model, Cmd.none )
Just ( newChildModel, childEffects ) -> Just ( newChildModel, childEffects ) ->
let let

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed initialModel.query) ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,16 +4,16 @@ Part 13
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## Compiling CSS ## Compiling CSS

View File

@@ -1,11 +1,9 @@
module SearchResult (..) where module SearchResult exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Signal exposing (Address) import Json.Decode exposing (Decoder)
import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
@@ -21,7 +19,7 @@ type alias ResultId =
Int Int
type Action type Msg
= Expand = Expand
| Collapse | Collapse
@@ -35,31 +33,27 @@ decoder =
|> hardcoded True |> hardcoded True
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Expand -> Expand ->
( { model | expanded = True }, Effects.none ) ( { model | expanded = True }, Cmd.none )
Collapse -> Collapse ->
( { model | expanded = False }, Effects.none ) ( { model | expanded = False }, Cmd.none )
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
li li []
[]
<| if model.expanded then <| if model.expanded then
[ span [ class "star-count" ] [ text (toString model.stars) ] [ span [ class "star-count" ] [ text (toString model.stars) ]
, a , a [ href ("https://github.com/" ++ model.name), target "_blank" ]
[ href ("https://github.com/" ++ model.name), target "_blank" ]
[ text model.name ] [ text model.name ]
, button , button [ class "hide-result", onClick Collapse ]
[ class "hide-result", onClick address Collapse ]
[ text "X" ] [ text "X" ]
] ]
else else
[ button [ button [ class "expand-result", onClick Expand ]
[ class "expand-result", onClick address Expand ]
[ text "Show" ] [ text "Show" ]
] ]

View File

@@ -10,11 +10,10 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "rtfeldman/elm-css": "1.0.0 <= v < 2.0.0",
"evancz/elm-http": "3.0.0 <= v < 4.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -10,12 +10,10 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0",
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"rtfeldman/elm-css": "1.0.0 <= v < 2.0.0", "rtfeldman/elm-css": "1.0.0 <= v < 2.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0" "evancz/elm-http": "3.0.1 <= v < 4.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,13 +9,11 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "rtfeldman/elm-css": "1.0.0 <= v < 2.0.0",
"evancz/elm-http": "3.0.0 <= v < 4.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -50,14 +50,19 @@ model =
} }
elmHubHeader : Html a
elmHubHeader =
header []
[ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
]
{-| TODO add a type annotation to this function {-| TODO add a type annotation to this function
-} -}
view model = view model =
div [ class "content" ] div [ class "content" ]
[ header [] [ elmHubHeader
[ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
]
, ul [ class "results" ] , ul [ class "results" ]
[{- TODO use model.results and viewSearchResults to display results -}] [{- TODO use model.results and viewSearchResults to display results -}]
] ]

View File

@@ -4,16 +4,16 @@ Part 2
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References

View File

@@ -3,6 +3,7 @@ module Main exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.App import Html.App
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
type alias Model = type alias Model =
@@ -50,19 +51,27 @@ initialModel =
} }
view : Model -> Html Msg elmHubHeader : Html a
view model = elmHubHeader =
div [ class "content" ] header []
[ header []
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, ul [ class "results" ]
(List.map viewSearchResult model.results)
{-| TODO revise this type annotation once we add our onClick handler
-}
view : Model -> Html a
view model =
div [ class "content" ]
[ elmHubHeader
, ul [ class "results" ] (List.map viewSearchResult model.results)
] ]
viewSearchResult : SearchResult -> Html Msg {-| TODO revise this type annotation once we add our onClick handler
-}
viewSearchResult : SearchResult -> Html a
viewSearchResult result = viewSearchResult result =
li [] li []
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]

View File

@@ -4,16 +4,16 @@ Part 3
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References

View File

@@ -3,6 +3,7 @@ module Main exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.App import Html.App
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
type alias Model = type alias Model =
@@ -63,13 +64,12 @@ view model =
] ]
, input , input
[ class "search-query" [ class "search-query"
-- TODO when we receive onInput, set the query in the model -- TODO onInput, set the query in the model
, defaultValue model.query , defaultValue model.query
] ]
[] []
, button [ class "search-button" ] [ text "Search" ] , button [ class "search-button" ] [ text "Search" ]
, ul [ class "results" ] , ul [ class "results" ] (List.map viewSearchResult model.results)
(List.map viewSearchResult model.results)
] ]

View File

@@ -4,16 +4,16 @@ Part 4
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References

View File

@@ -69,7 +69,7 @@ responseDecoder =
searchResultDecoder : Decoder SearchResult searchResultDecoder : Decoder SearchResult
searchResultDecoder = searchResultDecoder =
-- See https://developer.github.com/v3/search/#example -- See https://developer.github.com/v3/search/#example
-- TODO replace these `hardcoded` with calls to `require` -- TODO replace these calls to `hardcoded` with calls to `require`
decode SearchResult decode SearchResult
|> hardcoded 0 |> hardcoded 0
|> hardcoded "" |> hardcoded ""

View File

@@ -4,14 +4,14 @@ Part 5
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```

View File

@@ -1,19 +1,16 @@
module ElmHub (..) where module ElmHub exposing (..)
import Auth import Auth
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http import Http
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Json.Encode
import Signal exposing (Address)
searchFeed : String -> Effects Action searchFeed : String -> Cmd Msg
searchFeed query = searchFeed query =
let let
url = url =
@@ -23,11 +20,7 @@ searchFeed query =
++ query ++ query
++ "+language:elm&sort=stars&order=desc" ++ "+language:elm&sort=stars&order=desc"
-- TODO define task as: -- TODO use these ingredients to give Task.perform the arguments it needs:
--
-- task = performAction argument1 argument2 argument3
--
-- Use these "ingredients" to give `performAction` the arguments it needs:
-- --
-- Http.get -- Http.get
-- url -- url
@@ -37,41 +30,15 @@ searchFeed query =
-- --
-- Hint: http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#get -- Hint: http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#get
task = task =
"TODO performAction ..." "TODO call Task.perform ..."
in in
-- TODO replace this `Effects.none` with a call to: -- TODO replace this Cmd.none with a call to Task.perform
-- Cmd.none
-- Effects.task task
Effects.none
{-| Note: this will be a standard function in the next release of Elm.
Example:
type Action =
HandleResponse String | HandleError Http.Error
performAction
(\responseString -> HandleResponse responseString)
(\httpError -> HandleError httpError)
(Http.getString "https://google.com?q=something")
-}
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))
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
responseDecoder = responseDecoder =
"items" := Json.Decode.list searchResultDecoder Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
searchResultDecoder : Decoder SearchResult searchResultDecoder : Decoder SearchResult
@@ -108,25 +75,21 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, viewErrorMessage model.errorMessage , viewErrorMessage model.errorMessage
, ul , ul [ class "results" ] (List.map viewSearchResult model.results)
[ class "results" ]
(List.map (viewSearchResult address) model.results)
] ]
viewErrorMessage : Maybe String -> Html viewErrorMessage : Maybe String -> Html a a
viewErrorMessage errorMessage = viewErrorMessage errorMessage =
case errorMessage of case errorMessage of
Just message -> Just message ->
@@ -136,29 +99,18 @@ viewErrorMessage errorMessage =
text "" text ""
onInput address wrap = viewSearchResult : SearchResult -> Html Msg
on "input" targetValue (\val -> Signal.message address (wrap val)) viewSearchResult result =
li []
defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : Address Action -> SearchResult -> Html
viewSearchResult address result =
li
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a [ href ("https://github.com/" ++ result.name), target "_blank" ]
[ href ("https://github.com/" ++ result.name), target "_blank" ]
[ text result.name ] [ text result.name ]
, button , button [ class "hide-result", onClick (DeleteById result.id) ]
[ class "hide-result", onClick address (DeleteById result.id) ]
[ text "X" ] [ text "X" ]
] ]
type Action type Msg
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
@@ -166,27 +118,27 @@ type Action
| HandleSearchError Http.Error | HandleSearchError Http.Error
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, searchFeed model.query ) ( model, searchFeed model.query )
HandleSearchResponse results -> HandleSearchResponse results ->
( { model | results = results }, Effects.none ) ( { model | results = results }, Cmd.none )
HandleSearchError error -> HandleSearchError error ->
-- TODO if decoding failed, store the message in model.errorMessage -- TODO if decoding failed, store the message in model.errorMessage
-- --
-- Hint 1: look for "decode" in the documentation for this union type: -- Hint 1: look for "decode" in the documentation for this union type:
-- http://package.elm-lang.org/packages/evancz/elm-http/3.0.0/Http#Error -- http://package.elm-lang.org/packages/evancz/elm-http/3.0.1/Http#Error
-- --
-- Hint 2: to check if this is working, break responseDecoder -- Hint 2: to check if this is working, break responseDecoder
-- by changing "stargazers_count" to "description" -- by changing "stargazers_count" to "description"
( model, Effects.none ) ( model, Cmd.none )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
DeleteById idToHide -> DeleteById idToHide ->
let let
@@ -197,4 +149,4 @@ update action model =
newModel = newModel =
{ model | results = newResults } { model | results = newResults }
in in
( newModel, Effects.none ) ( newModel, Cmd.none )

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, searchFeed initialModel.query ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,16 +4,16 @@ Part 6
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## References ## References

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,19 +1,16 @@
module ElmHub (..) where module ElmHub exposing (..)
import Auth import Auth
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http import Http
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Json.Encode
import Signal exposing (Address)
searchFeed : String -> Effects Action searchFeed : String -> Cmd Msg
searchFeed query = searchFeed query =
let let
url = url =
@@ -22,19 +19,13 @@ searchFeed query =
++ "&q=" ++ "&q="
++ query ++ query
++ "+language:elm&sort=stars&order=desc" ++ "+language:elm&sort=stars&order=desc"
task =
performAction
(\response -> HandleSearchResponse response)
(\error -> HandleSearchError error)
(Http.get responseDecoder url)
in in
Effects.task task Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url)
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
responseDecoder = responseDecoder =
"items" := Json.Decode.list searchResultDecoder Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
searchResultDecoder : Decoder SearchResult searchResultDecoder : Decoder SearchResult
@@ -45,30 +36,6 @@ searchResultDecoder =
|> required "stargazers_count" Json.Decode.int |> required "stargazers_count" Json.Decode.int
{-| Note: this will be a standard function in the next release of Elm.
Example:
type Action =
HandleResponse String | HandleError Http.Error
performAction
(\responseString -> HandleResponse responseString)
(\httpError -> HandleError httpError)
(Http.getString "https://google.com?q=something")
-}
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
@@ -95,25 +62,21 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, viewErrorMessage model.errorMessage , viewErrorMessage model.errorMessage
, ul , ul [ class "results" ] (List.map viewSearchResult model.results)
[ class "results" ]
(List.map (viewSearchResult address) model.results)
] ]
viewErrorMessage : Maybe String -> Html viewErrorMessage : Maybe String -> Html a
viewErrorMessage errorMessage = viewErrorMessage errorMessage =
case errorMessage of case errorMessage of
Just message -> Just message ->
@@ -123,29 +86,18 @@ viewErrorMessage errorMessage =
text "" text ""
onInput address wrap = viewSearchResult : SearchResult -> Html Msg
on "input" targetValue (\val -> Signal.message address (wrap val)) viewSearchResult result =
li []
defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : Address Action -> SearchResult -> Html
viewSearchResult address result =
li
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a [ href ("https://github.com/" ++ result.name), target "_blank" ]
[ href ("https://github.com/" ++ result.name), target "_blank" ]
[ text result.name ] [ text result.name ]
, button , button [ class "hide-result", onClick (DeleteById result.id) ]
[ class "hide-result", onClick address (DeleteById result.id) ]
[ text "X" ] [ text "X" ]
] ]
type Action type Msg
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
@@ -153,14 +105,14 @@ type Action
| HandleSearchError Http.Error | HandleSearchError Http.Error
update : Action -> Model -> ( Model, Effects Action ) update : Msg -> Model -> ( Model, Cmd Msg )
update action model = update msg model =
case action of case msg of
Search -> Search ->
( model, searchFeed model.query ) ( model, searchFeed model.query )
HandleSearchResponse results -> HandleSearchResponse results ->
( { model | results = results }, Effects.none ) ( { model | results = results }, Cmd.none )
HandleSearchError error -> HandleSearchError error ->
let let
@@ -172,10 +124,10 @@ update action model =
_ -> _ ->
Nothing Nothing
in in
( { model | errorMessage = errorMessage }, Effects.none ) ( { model | errorMessage = errorMessage }, Cmd.none )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
DeleteById idToHide -> DeleteById idToHide ->
let let
@@ -186,4 +138,4 @@ update action model =
newModel = newModel =
{ model | results = newResults } { model | results = newResults }
in in
( newModel, Effects.none ) ( newModel, Cmd.none )

View File

@@ -1,27 +1,13 @@
module Main (..) where module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, searchFeed initialModel.query ) , init = ( initialModel, searchFeed initialModel.query )
, inputs = [] , inputs = []
} }
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks

View File

@@ -4,23 +4,23 @@ Part 7
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## Running Tests ## Running Tests
```bash ```bash
cd test cd test
elm package install elm-package install
elm test TestRunner.elm elm test TestRunner.elm
``` ```

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,14 +9,10 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0",
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,43 +1,28 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http
import Auth import Auth
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Json.Decode exposing (Decoder)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Json.Encode import Json.Encode
import Signal exposing (Address)
searchFeed : Address String -> String -> Effects Action getQueryUrl : String -> String
searchFeed address query = getQueryUrl query =
let
-- See https://developer.github.com/v3/search/#example for how to customize! -- 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"
-- These only talk to JavaScript ports now. They never result in Actions
-- actually do any actions themselves.
task =
performAction
(\_ -> DoNothing)
(\_ -> DoNothing)
(Signal.send address query)
in
Effects.task task
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
responseDecoder = responseDecoder =
"items" := Json.Decode.list searchResultDecoder Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
searchResultDecoder : Decoder SearchResult searchResultDecoder : Decoder SearchResult
@@ -48,30 +33,6 @@ searchResultDecoder =
|> required "stargazers_count" Json.Decode.int |> required "stargazers_count" Json.Decode.int
{-| Note: this will be a standard function in the next release of Elm.
Example:
type Action =
HandleResponse String | HandleError Http.Error
performAction
(\responseString -> HandleResponse responseString)
(\httpError -> HandleError httpError)
(Http.getString "https://google.com?q=something")
-}
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
@@ -98,25 +59,21 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, viewErrorMessage model.errorMessage , viewErrorMessage model.errorMessage
, ul , ul [ class "results" ] (List.map viewSearchResult model.results)
[ class "results" ]
(List.map (viewSearchResult address) model.results)
] ]
viewErrorMessage : Maybe String -> Html viewErrorMessage : Maybe String -> Html a
viewErrorMessage errorMessage = viewErrorMessage errorMessage =
case errorMessage of case errorMessage of
Just message -> Just message ->
@@ -126,29 +83,18 @@ viewErrorMessage errorMessage =
text "" text ""
onInput address wrap = viewSearchResult : SearchResult -> Html Msg
on "input" targetValue (\val -> Signal.message address (wrap val)) viewSearchResult result =
li []
defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : Address Action -> SearchResult -> Html
viewSearchResult address result =
li
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a [ href ("https://github.com/" ++ result.name), target "_blank" ]
[ href ("https://github.com/" ++ result.name), target "_blank" ]
[ text result.name ] [ text result.name ]
, button , button [ class "hide-result", onClick (DeleteById result.id) ]
[ class "hide-result", onClick address (DeleteById result.id) ]
[ text "X" ] [ text "X" ]
] ]
type Action type Msg
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
@@ -157,20 +103,20 @@ type Action
| DoNothing | DoNothing
update : Address String -> Action -> Model -> ( Model, Effects Action ) update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg )
update searchAddress action model = update searchFeed msg model =
case action of case msg of
Search -> Search ->
( model, searchFeed searchAddress model.query ) ( model, searchFeed (getQueryUrl model.query) )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
( { model | results = results }, Effects.none ) ( { model | results = results }, Cmd.none )
SetErrorMessage errorMessage -> SetErrorMessage errorMessage ->
( { model | errorMessage = errorMessage }, Effects.none ) ( { model | errorMessage = errorMessage }, Cmd.none )
DeleteById idToHide -> DeleteById idToHide ->
let let
@@ -181,7 +127,16 @@ update searchAddress action model =
newModel = newModel =
{ model | results = newResults } { model | results = newResults }
in in
( newModel, Effects.none ) ( newModel, Cmd.none )
DoNothing -> DoNothing ->
( model, Effects.none ) ( 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,57 +1,31 @@
module Main (..) where port module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects) import Html.App
import Task exposing (Task) import Json.Decode exposing (Value)
import Html exposing (Html)
import Signal
import Json.Encode
import Json.Decode
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update search.address , update = update githubSearch
, init = ( initialModel, searchFeed search.address initialModel.query ) , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) )
, inputs = [ responseActions ] , subscriptions = \_ -> githubResponse decodeResponse
} }
port tasks : Signal (Task Effects.Never ()) decodeResponse : Json.Decode.Value -> Msg
port tasks = decodeResponse json =
app.tasks case Json.Decode.decodeValue responseDecoder json of
Err err ->
SetErrorMessage (Just err)
Ok results ->
SetResults results
search : Signal.Mailbox String port githubSearch : String -> Cmd msg
search =
Signal.mailbox ""
port githubSearch : Signal String port githubResponse : (Json.Decode.Value -> msg) -> Sub msg
port githubSearch =
search.signal
responseActions : Signal Action
responseActions =
Signal.map decodeGithubResponse githubResponse
decodeGithubResponse : Json.Encode.Value -> Action
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!")
port githubResponse : Signal Json.Encode.Value

View File

@@ -4,22 +4,22 @@ Part 8
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```
## Running Tests ## Running Tests
```bash ```bash
cd test cd test
elm package install elm-package install
elm test TestRunner.elm elm test TestRunner.elm
``` ```

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -9,14 +9,10 @@
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0",
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.3 <= v < 2.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }

View File

@@ -1,44 +1,27 @@
module ElmHub (..) where module ElmHub exposing (..)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (class, target, href, property) import Html.Attributes exposing (class, target, href, property, defaultValue)
import Html.Events exposing (..) import Html.Events exposing (..)
import Http
import Auth import Auth
import Task exposing (Task) import Json.Decode exposing (Decoder)
import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..) import Json.Decode.Pipeline exposing (..)
import Json.Encode
import Signal exposing (Address)
import Dict exposing (Dict) import Dict exposing (Dict)
searchFeed : Address String -> String -> Effects Action getQueryUrl : String -> String
searchFeed address query = getQueryUrl query =
let
-- See https://developer.github.com/v3/search/#example for how to customize! -- 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"
-- These only talk to JavaScript ports now. They never result in Actions
-- actually do any actions themselves.
task =
performAction
(\_ -> DoNothing)
(\_ -> DoNothing)
(Signal.send address query)
in
Effects.task task
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
responseDecoder = responseDecoder =
"items" := Json.Decode.list searchResultDecoder Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
searchResultDecoder : Decoder SearchResult searchResultDecoder : Decoder SearchResult
@@ -49,30 +32,6 @@ searchResultDecoder =
|> required "stargazers_count" Json.Decode.int |> required "stargazers_count" Json.Decode.int
{-| Note: this will be a standard function in the next release of Elm.
Example:
type Action =
HandleResponse String | HandleError Http.Error
performAction
(\responseString -> HandleResponse responseString)
(\httpError -> HandleError httpError)
(Http.getString "https://google.com?q=something")
-}
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 : Dict ResultId SearchResult , results : Dict ResultId SearchResult
@@ -99,52 +58,37 @@ initialModel =
} }
view : Address Action -> Model -> Html view : Model -> Html Msg
view address model = view model =
div div [ class "content" ]
[ class "content" ] [ header []
[ header
[]
[ h1 [] [ text "ElmHub" ] [ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick address Search ] [ text "Search" ] , button [ class "search-button", onClick Search ] [ text "Search" ]
, ul , ul [ class "results" ] (viewSearchResults model.results)
[ class "results" ]
(viewSearchResults address model.results)
] ]
viewSearchResults : Address Action -> Dict ResultId SearchResult -> List Html viewSearchResults : Dict ResultId SearchResult -> List (Html Msg)
viewSearchResults address results = viewSearchResults results =
-- TODO sort by star count and render -- TODO sort by star count and render
[] []
onInput address wrap = viewSearchResult : SearchResult -> Html Msg
on "input" targetValue (\val -> Signal.message address (wrap val)) viewSearchResult result =
li []
defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : Address Action -> SearchResult -> Html
viewSearchResult address result =
li
[]
[ span [ class "star-count" ] [ text (toString result.stars) ] [ span [ class "star-count" ] [ text (toString result.stars) ]
, a , a [ href ("https://github.com/" ++ result.name), target "_blank" ]
[ href ("https://github.com/" ++ result.name), target "_blank" ]
[ text result.name ] [ text result.name ]
, button , button [ class "hide-result", onClick (DeleteById result.id) ]
[ class "hide-result", onClick address (DeleteById result.id) ]
[ text "X" ] [ text "X" ]
] ]
type Action type Msg
= Search = Search
| SetQuery String | SetQuery String
| DeleteById ResultId | DeleteById ResultId
@@ -153,14 +97,14 @@ type Action
| DoNothing | DoNothing
update : Address String -> Action -> Model -> ( Model, Effects Action ) update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg )
update searchAddress action model = update searchFeed msg model =
case action of case msg of
Search -> Search ->
( model, searchFeed searchAddress model.query ) ( model, searchFeed (getQueryUrl model.query) )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) ( { model | query = query }, Cmd.none )
SetResults results -> SetResults results ->
let let
@@ -169,14 +113,14 @@ update searchAddress action model =
-- TODO convert results list into a Dict -- TODO convert results list into a Dict
Dict.empty Dict.empty
in in
( { model | results = resultsById }, Effects.none ) ( { model | results = resultsById }, Cmd.none )
DeleteById id -> DeleteById id ->
-- TODO delete the result with the given id -- TODO delete the result with the given id
( model, Effects.none ) ( model, Cmd.none )
SetErrorMessage errorMessage -> SetErrorMessage errorMessage ->
( { model | errorMessage = errorMessage }, Effects.none ) ( { model | errorMessage = errorMessage }, Cmd.none )
DoNothing -> DoNothing ->
( model, Effects.none ) ( model, Cmd.none )

View File

@@ -1,58 +1,31 @@
module Main (..) where port module Main exposing (..)
import StartApp
import ElmHub exposing (..) import ElmHub exposing (..)
import Effects exposing (Effects) import Html.App
import Task exposing (Task) import Json.Decode exposing (Value)
import Html exposing (Html)
import Signal
import Json.Encode
import Json.Decode
main : Signal Html main : Program Never
main = main =
app.html Html.App.program
app : StartApp.App Model
app =
StartApp.start
{ view = view { view = view
, update = update search.address , update = update githubSearch
, init = ( initialModel, searchFeed search.address initialModel.query ) , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) )
, inputs = [ responseActions ] , subscriptions = \_ -> githubResponse decodeResponse
} }
port tasks : Signal (Task Effects.Never ()) decodeResponse : Json.Decode.Value -> Msg
port tasks = decodeResponse json =
app.tasks case Json.Decode.decodeValue responseDecoder json of
Err err ->
SetErrorMessage (Just err)
search : Signal.Mailbox String
search =
Signal.mailbox ""
port githubSearch : Signal String
port githubSearch =
search.signal
responseActions : Signal Action
responseActions =
Signal.map decodeGithubResponse githubResponse
decodeGithubResponse : Json.Encode.Value -> Action
decodeGithubResponse value =
case Json.Decode.decodeValue responseDecoder value of
Ok results -> Ok results ->
SetResults results SetResults results
Err message ->
SetErrorMessage (Just message) port githubSearch : String -> Cmd msg
port githubResponse : Signal Json.Encode.Value port githubResponse : (Json.Decode.Value -> msg) -> Sub msg

View File

@@ -4,14 +4,14 @@ Part 9
## Installation ## Installation
```bash ```bash
elm package install elm-package install
``` ```
(Answer `y` at the prompt. In rare cases a known issue can cause the download (Answer `y` at the prompt. In rare cases a known issue can cause the download
to fail; in that case, just run `elm package install` again.) to fail; in that case, just run `elm-package install` again.)
## Building ## Building
```bash ```bash
elm live Main.elm --open -- --output=elm.js elm-live Main.elm --open -- --output=elm.js
``` ```

View File

@@ -10,11 +10,9 @@
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0", "NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0", "elm-lang/core": "4.0.1 <= v < 5.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0", "elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0", "evancz/elm-http": "3.0.1 <= v < 4.0.0"
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.0 <= v < 3.0.0"
}, },
"elm-version": "0.16.0 <= v < 0.17.0" "elm-version": "0.17.0 <= v < 0.18.0"
} }