Reorganize stages into parts
This commit is contained in:
116
part9/ElmHub.elm
Normal file
116
part9/ElmHub.elm
Normal file
@@ -0,0 +1,116 @@
|
||||
module ElmHub (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Html.Lazy exposing (..)
|
||||
import Http
|
||||
import Task exposing (Task)
|
||||
import Effects exposing (Effects)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Json.Encode
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
import SearchResult
|
||||
|
||||
|
||||
searchFeed : String -> Task x Action
|
||||
searchFeed query =
|
||||
let
|
||||
-- See https://developer.github.com/v3/search/#example for how to customize!
|
||||
url =
|
||||
"https://api.github.com/search/repositories?q="
|
||||
++ query
|
||||
++ "+language:elm"
|
||||
|
||||
task =
|
||||
Http.get responseDecoder url
|
||||
|> Task.map SetResults
|
||||
in
|
||||
Task.onError task (\_ -> Task.succeed (SetResults []))
|
||||
|
||||
|
||||
responseDecoder : Decoder (List SearchResult.Model)
|
||||
responseDecoder =
|
||||
-- TODO make use of SearchResult's decoder
|
||||
Json.Decode.succeed []
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ query : String
|
||||
, results : Dict SearchResult.ResultId SearchResult.Model
|
||||
}
|
||||
|
||||
|
||||
initialModel : Model
|
||||
initialModel =
|
||||
{ query = "tutorial"
|
||||
, results = Dict.empty
|
||||
}
|
||||
|
||||
|
||||
view : Address Action -> Model -> Html
|
||||
view address model =
|
||||
div
|
||||
[ class "content" ]
|
||||
[ header
|
||||
[]
|
||||
[ h1 [] [ text "ElmHub" ]
|
||||
, span [ class "tagline" ] [ text "“Like GitHub, but for Elm things.”" ]
|
||||
]
|
||||
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] []
|
||||
, button [ class "search-button", onClick address Search ] [ text "Search" ]
|
||||
, ul
|
||||
[ class "results" ]
|
||||
(viewSearchResults address model.results)
|
||||
]
|
||||
|
||||
|
||||
viewSearchResults : Address Action -> Dict SearchResult.ResultId SearchResult.Model -> List Html
|
||||
viewSearchResults address results =
|
||||
results
|
||||
|> Dict.values
|
||||
|> List.sortBy (.stars >> negate)
|
||||
|> List.map (\_ -> div [] [ text "TODO replace this line with view logic from SearchResult" ])
|
||||
|
||||
|
||||
onInput address wrap =
|
||||
on "input" targetValue (\val -> Signal.message address (wrap val))
|
||||
|
||||
|
||||
defaultValue str =
|
||||
property "defaultValue" (Json.Encode.string str)
|
||||
|
||||
|
||||
type Action
|
||||
= Search
|
||||
| SetQuery String
|
||||
| DeleteById SearchResult.ResultId
|
||||
| SetResults (List SearchResult.Model)
|
||||
|
||||
|
||||
update : Action -> Model -> ( Model, Effects Action )
|
||||
update action model =
|
||||
case action of
|
||||
Search ->
|
||||
( model, Effects.task (searchFeed model.query) )
|
||||
|
||||
SetQuery query ->
|
||||
( { model | query = query }, Effects.none )
|
||||
|
||||
SetResults results ->
|
||||
let
|
||||
resultsById : Dict SearchResult.ResultId SearchResult.Model
|
||||
resultsById =
|
||||
results
|
||||
|> List.map (\result -> ( result.id, result ))
|
||||
|> Dict.fromList
|
||||
in
|
||||
( { model | results = resultsById }, Effects.none )
|
||||
|
||||
DeleteById id ->
|
||||
let
|
||||
newModel =
|
||||
{ model | results = Dict.remove id model.results }
|
||||
in
|
||||
( newModel, Effects.none )
|
||||
27
part9/Main.elm
Normal file
27
part9/Main.elm
Normal file
@@ -0,0 +1,27 @@
|
||||
module Main (..) where
|
||||
|
||||
import StartApp
|
||||
import ElmHub exposing (..)
|
||||
import Effects exposing (Effects)
|
||||
import Task exposing (Task)
|
||||
import Html exposing (Html)
|
||||
|
||||
|
||||
main : Signal Html
|
||||
main =
|
||||
app.html
|
||||
|
||||
|
||||
app : StartApp.App Model
|
||||
app =
|
||||
StartApp.start
|
||||
{ view = view
|
||||
, update = update
|
||||
, init = ( initialModel, Effects.task (searchFeed initialModel.query) )
|
||||
, inputs = []
|
||||
}
|
||||
|
||||
|
||||
port tasks : Signal (Task Effects.Never ())
|
||||
port tasks =
|
||||
app.tasks
|
||||
17
part9/README.md
Normal file
17
part9/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
Stage 9
|
||||
=======
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
elm package install
|
||||
```
|
||||
|
||||
(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.)
|
||||
|
||||
## Building
|
||||
|
||||
```bash
|
||||
elm live Main.elm --open -- --output=elm.js
|
||||
```
|
||||
45
part9/SearchResult.elm
Normal file
45
part9/SearchResult.elm
Normal file
@@ -0,0 +1,45 @@
|
||||
module SearchResult (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
|
||||
|
||||
type alias ResultId =
|
||||
Int
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ id : ResultId
|
||||
, name : String
|
||||
, stars : Int
|
||||
}
|
||||
|
||||
|
||||
decoder : Decoder Model
|
||||
decoder =
|
||||
Json.Decode.object3
|
||||
Model
|
||||
("id" := Json.Decode.int)
|
||||
("full_name" := Json.Decode.string)
|
||||
("stargazers_count" := Json.Decode.int)
|
||||
|
||||
|
||||
view : Address a -> Model -> Html
|
||||
view address result =
|
||||
li
|
||||
[]
|
||||
[ span [ class "star-count" ] [ text (toString result.stars) ]
|
||||
, a
|
||||
[ href ("https://github.com/" ++ result.name)
|
||||
, target "_blank"
|
||||
]
|
||||
[ text result.name ]
|
||||
, button
|
||||
-- TODO onClick, send a delete action to the address
|
||||
[ class "hide-result" ]
|
||||
[ text "X" ]
|
||||
]
|
||||
BIN
part9/elm-hub.png
Normal file
BIN
part9/elm-hub.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
18
part9/elm-package.json
Normal file
18
part9/elm-package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Like GitHub, but for Elm stuff.",
|
||||
"repository": "https://github.com/rtfeldman/elm-workshop.git",
|
||||
"license": "BSD-3-Clause",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"elm-lang/core": "3.0.0 <= v < 4.0.0",
|
||||
"evancz/elm-effects": "2.0.0 <= v < 3.0.0",
|
||||
"evancz/elm-html": "4.0.0 <= v < 5.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"
|
||||
}
|
||||
26
part9/index.html
Normal file
26
part9/index.html
Normal file
@@ -0,0 +1,26 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>ElmHub</title>
|
||||
<script type="text/javascript" src="elm.js"></script>
|
||||
|
||||
<!-- Uncomment the below line to enable elm-reactor support. -->
|
||||
<!-- <script type="text/javascript" src="/_reactor/debug.js"></script> -->
|
||||
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="icon" type="image/png" href="elm-hub.png">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
</body>
|
||||
|
||||
<script type="text/javascript">
|
||||
var app = Elm.fullscreen(Elm.Main, {});
|
||||
|
||||
// Uncomment this line and comment out the above to enable elm-reactor support.
|
||||
// var app = Elm.fullscreenDebug("ElmHub", "Main.elm");
|
||||
</script>
|
||||
|
||||
</html>
|
||||
92
part9/style.css
Normal file
92
part9/style.css
Normal file
@@ -0,0 +1,92 @@
|
||||
|
||||
.content {
|
||||
width: 960px;
|
||||
margin: 0 auto;
|
||||
padding: 30px;
|
||||
font-family: Helvetica, Arial, serif;
|
||||
}
|
||||
|
||||
header {
|
||||
position: relative;
|
||||
padding: 6px 12px;
|
||||
height: 36px;
|
||||
background-color: rgb(96, 181, 204);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: white;
|
||||
font-weight: normal;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.tagline {
|
||||
color: #eee;
|
||||
position: absolute;
|
||||
right: 16px;
|
||||
top: 12px;
|
||||
font-size: 24px;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.results {
|
||||
list-style-image: url('http://img-cache.cdn.gaiaonline.com/76bd5c99d8f2236e9d3672510e933fdf/http://i278.photobucket.com/albums/kk81/d3m3nt3dpr3p/Tiny-Star-Icon.png');
|
||||
list-style-position: inside;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.results li {
|
||||
font-size: 18px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.star-count {
|
||||
font-weight: bold;
|
||||
margin-right: 16px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: rgb(96, 181, 204);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.search-query {
|
||||
padding: 8px;
|
||||
font-size: 24px;
|
||||
margin-bottom: 18px;
|
||||
margin-top: 36px;
|
||||
}
|
||||
|
||||
.search-button {
|
||||
padding: 8px 16px;
|
||||
font-size: 24px;
|
||||
color: white;
|
||||
border: 1px solid #ccc;
|
||||
background-color: rgb(96, 181, 204);
|
||||
margin-left: 12px
|
||||
}
|
||||
|
||||
.search-button:hover {
|
||||
color: rgb(96, 181, 204);
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
.hide-result {
|
||||
background-color: transparent;
|
||||
border: 0;
|
||||
font-weight: bold;
|
||||
font-size: 18px;
|
||||
margin-left: 18px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.hide-result:hover {
|
||||
color: rgb(96, 181, 204);
|
||||
}
|
||||
|
||||
button:focus, input:focus {
|
||||
outline: none;
|
||||
}
|
||||
15
part9/test/TestRunner.elm
Normal file
15
part9/test/TestRunner.elm
Normal file
@@ -0,0 +1,15 @@
|
||||
module Main where
|
||||
|
||||
import Signal exposing (Signal)
|
||||
|
||||
import ElmTest exposing (consoleRunner)
|
||||
import Console exposing (IO, run)
|
||||
import Task
|
||||
|
||||
import Tests
|
||||
|
||||
console : IO ()
|
||||
console = consoleRunner Tests.all
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner = run console
|
||||
35
part9/test/Tests.elm
Normal file
35
part9/test/Tests.elm
Normal file
@@ -0,0 +1,35 @@
|
||||
module Tests (..) where
|
||||
|
||||
import ElmTest exposing (..)
|
||||
import ElmHub exposing (responseDecoder)
|
||||
import Json.Decode exposing (decodeString)
|
||||
|
||||
|
||||
all : Test
|
||||
all =
|
||||
suite
|
||||
"Decoding responses from GitHub"
|
||||
[ test "they can decode empty responses"
|
||||
<| let
|
||||
emptyResponse =
|
||||
"""{ "items": [] }"""
|
||||
in
|
||||
assertEqual
|
||||
(decodeString responseDecoder emptyResponse)
|
||||
(Ok [])
|
||||
, test "they can decode responses with results in them"
|
||||
<| let
|
||||
response =
|
||||
"""{ "items": [
|
||||
{ "id": 5, "full_name": "foo", "stargazers_count": 42 },
|
||||
{ "id": 3, "full_name": "bar", "stargazers_count": 77 }
|
||||
] }"""
|
||||
in
|
||||
assertEqual
|
||||
(decodeString responseDecoder response)
|
||||
(Ok
|
||||
[ { id = 5, name = "foo", stars = 42 }
|
||||
, { id = 3, name = "bar", stars = 77 }
|
||||
]
|
||||
)
|
||||
]
|
||||
21
part9/test/elm-package.json
Normal file
21
part9/test/elm-package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Like GitHub, but for Elm stuff.",
|
||||
"repository": "https://github.com/rtfeldman/elm-workshop.git",
|
||||
"license": "BSD-3-Clause",
|
||||
"source-directories": [
|
||||
".",
|
||||
".."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.1.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "3.0.0 <= v < 4.0.0",
|
||||
"evancz/elm-effects": "2.0.0 <= v < 3.0.0",
|
||||
"evancz/elm-html": "4.0.0 <= v < 5.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"
|
||||
}
|
||||
Reference in New Issue
Block a user