Add part11
This commit is contained in:
1
part11/.gitignore
vendored
Normal file
1
part11/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
!Auth.elm
|
||||||
331
part11/ElmHub.elm
Normal file
331
part11/ElmHub.elm
Normal file
@@ -0,0 +1,331 @@
|
|||||||
|
port module ElmHub exposing (..)
|
||||||
|
|
||||||
|
import Html exposing (..)
|
||||||
|
import Html.Attributes exposing (class, target, href, defaultValue, type', checked, placeholder, value)
|
||||||
|
import Html.Events exposing (..)
|
||||||
|
import Html.App as Html
|
||||||
|
import Auth
|
||||||
|
import Json.Decode exposing (Decoder)
|
||||||
|
import Json.Decode.Pipeline exposing (..)
|
||||||
|
import String
|
||||||
|
import Table
|
||||||
|
|
||||||
|
|
||||||
|
responseDecoder : Decoder (List SearchResult)
|
||||||
|
responseDecoder =
|
||||||
|
Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
|
||||||
|
|
||||||
|
|
||||||
|
searchResultDecoder : Decoder SearchResult
|
||||||
|
searchResultDecoder =
|
||||||
|
decode SearchResult
|
||||||
|
|> required "id" Json.Decode.int
|
||||||
|
|> required "full_name" Json.Decode.string
|
||||||
|
|> required "stargazers_count" Json.Decode.int
|
||||||
|
|
||||||
|
|
||||||
|
type alias Model =
|
||||||
|
-- TODO add tableState : Table.State to the Model
|
||||||
|
{ query : String
|
||||||
|
, results : List SearchResult
|
||||||
|
, errorMessage : Maybe String
|
||||||
|
, options : SearchOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type alias SearchOptions =
|
||||||
|
{ sort : String
|
||||||
|
, ascending : Bool
|
||||||
|
, searchInDescription : Bool
|
||||||
|
, userFilter : String
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type alias SearchResult =
|
||||||
|
{ id : Int
|
||||||
|
, name : String
|
||||||
|
, stars : Int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
initialModel : Model
|
||||||
|
initialModel =
|
||||||
|
-- TODO initialize the Model's tableState to (Table.initialSort "Stars")
|
||||||
|
{ query = "tutorial"
|
||||||
|
, results = []
|
||||||
|
, errorMessage = Nothing
|
||||||
|
, options =
|
||||||
|
{ sort = "stars"
|
||||||
|
, ascending = False
|
||||||
|
, searchInDescription = True
|
||||||
|
, userFilter = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
init : ( Model, Cmd Msg )
|
||||||
|
init =
|
||||||
|
( initialModel, githubSearch (getQueryString initialModel) )
|
||||||
|
|
||||||
|
|
||||||
|
subscriptions : Model -> Sub Msg
|
||||||
|
subscriptions _ =
|
||||||
|
githubResponse decodeResponse
|
||||||
|
|
||||||
|
|
||||||
|
type Msg
|
||||||
|
= Search
|
||||||
|
| Options OptionsMsg
|
||||||
|
| SetQuery String
|
||||||
|
| DeleteById Int
|
||||||
|
| HandleSearchResponse (List SearchResult)
|
||||||
|
| HandleSearchError (Maybe String)
|
||||||
|
-- TODO add a new constructor: SetTableState Table.State
|
||||||
|
| DoNothing
|
||||||
|
|
||||||
|
|
||||||
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||||
|
update msg model =
|
||||||
|
case msg of
|
||||||
|
Options optionsMsg ->
|
||||||
|
( { model | options = updateOptions optionsMsg model.options }, Cmd.none )
|
||||||
|
|
||||||
|
Search ->
|
||||||
|
( model, githubSearch (getQueryString model) )
|
||||||
|
|
||||||
|
SetQuery query ->
|
||||||
|
( { model | query = query }, Cmd.none )
|
||||||
|
|
||||||
|
HandleSearchResponse results ->
|
||||||
|
( { model | results = results }, Cmd.none )
|
||||||
|
|
||||||
|
HandleSearchError error ->
|
||||||
|
( { model | errorMessage = error }, Cmd.none )
|
||||||
|
|
||||||
|
DeleteById idToHide ->
|
||||||
|
let
|
||||||
|
newResults =
|
||||||
|
model.results
|
||||||
|
|> List.filter (\{ id } -> id /= idToHide)
|
||||||
|
|
||||||
|
newModel =
|
||||||
|
{ model | results = newResults }
|
||||||
|
in
|
||||||
|
( newModel, Cmd.none )
|
||||||
|
|
||||||
|
-- TODO add a new branch for SetTableState
|
||||||
|
-- which records the new tableState in the Model.
|
||||||
|
DoNothing ->
|
||||||
|
( model, Cmd.none )
|
||||||
|
|
||||||
|
|
||||||
|
tableConfig : Table.Config SearchResult Msg
|
||||||
|
tableConfig =
|
||||||
|
Table.config
|
||||||
|
{ toId = .id >> toString
|
||||||
|
, toMsg =
|
||||||
|
-- TODO have the table use SetTableState for its toMsg instead of:
|
||||||
|
(\tableState -> DoNothing)
|
||||||
|
, columns = [ starsColumn, nameColumn ]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
starsColumn : Table.Column SearchResult Msg
|
||||||
|
starsColumn =
|
||||||
|
Table.veryCustomColumn
|
||||||
|
{ name = "Stars"
|
||||||
|
, viewData = viewStars
|
||||||
|
, sorter = Table.increasingOrDecreasingBy (negate << .stars)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
nameColumn : Table.Column SearchResult Msg
|
||||||
|
nameColumn =
|
||||||
|
Table.veryCustomColumn
|
||||||
|
{ name = "Name"
|
||||||
|
, viewData = viewSearchResult
|
||||||
|
, sorter = Table.increasingOrDecreasingBy .name
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
updateOptions : OptionsMsg -> SearchOptions -> SearchOptions
|
||||||
|
updateOptions optionsMsg options =
|
||||||
|
case optionsMsg of
|
||||||
|
SetSort sort ->
|
||||||
|
{ options | sort = sort }
|
||||||
|
|
||||||
|
SetAscending ascending ->
|
||||||
|
{ options | ascending = ascending }
|
||||||
|
|
||||||
|
SetSearchInDescription searchInDescription ->
|
||||||
|
{ options | searchInDescription = searchInDescription }
|
||||||
|
|
||||||
|
SetUserFilter userFilter ->
|
||||||
|
{ options | userFilter = userFilter }
|
||||||
|
|
||||||
|
|
||||||
|
view : Model -> Html Msg
|
||||||
|
view model =
|
||||||
|
let
|
||||||
|
currentTableState : Table.State
|
||||||
|
currentTableState =
|
||||||
|
-- TODO have this use the actual current table state
|
||||||
|
Table.initialSort "Stars"
|
||||||
|
in
|
||||||
|
div [ class "content" ]
|
||||||
|
[ header []
|
||||||
|
[ h1 [] [ text "ElmHub" ]
|
||||||
|
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
|
||||||
|
]
|
||||||
|
, div [ class "search" ]
|
||||||
|
[ Html.map Options (viewOptions model.options)
|
||||||
|
, div [ class "search-input" ]
|
||||||
|
[ input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
|
||||||
|
, button [ class "search-button", onClick Search ] [ text "Search" ]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
, viewErrorMessage model.errorMessage
|
||||||
|
-- TODO have this use model.results instead of []
|
||||||
|
, Table.view tableConfig currentTableState []
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
viewErrorMessage : Maybe String -> Html a
|
||||||
|
viewErrorMessage errorMessage =
|
||||||
|
case errorMessage of
|
||||||
|
Just message ->
|
||||||
|
div [ class "error" ] [ text message ]
|
||||||
|
|
||||||
|
Nothing ->
|
||||||
|
text ""
|
||||||
|
|
||||||
|
|
||||||
|
viewSearchResult : SearchResult -> Html Msg
|
||||||
|
viewSearchResult result =
|
||||||
|
li []
|
||||||
|
[ span [ class "star-count" ] [ text (toString result.stars) ]
|
||||||
|
, a [ href ("https://github.com/" ++ result.name), target "_blank" ]
|
||||||
|
[ text result.name ]
|
||||||
|
, button [ class "hide-result", onClick (DeleteById result.id) ]
|
||||||
|
[ text "X" ]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
type OptionsMsg
|
||||||
|
= SetSort String
|
||||||
|
| SetAscending Bool
|
||||||
|
| SetSearchInDescription Bool
|
||||||
|
| SetUserFilter String
|
||||||
|
|
||||||
|
|
||||||
|
viewOptions : SearchOptions -> Html OptionsMsg
|
||||||
|
viewOptions opts =
|
||||||
|
div [ class "search-options" ]
|
||||||
|
[ div [ class "search-option" ]
|
||||||
|
[ label [ class "top-label" ] [ text "Sort by" ]
|
||||||
|
, select [ onChange SetSort, value opts.sort ]
|
||||||
|
[ option [ value "stars" ] [ text "Stars" ]
|
||||||
|
, option [ value "forks" ] [ text "Forks" ]
|
||||||
|
, option [ value "updated" ] [ text "Updated" ]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
, div [ class "search-option" ]
|
||||||
|
[ label [ class "top-label" ] [ text "Owned by" ]
|
||||||
|
, input
|
||||||
|
[ type' "text"
|
||||||
|
, placeholder "Enter a username"
|
||||||
|
, defaultValue opts.userFilter
|
||||||
|
, onInput SetUserFilter
|
||||||
|
]
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
, label [ class "search-option" ]
|
||||||
|
[ input [ type' "checkbox", checked opts.ascending, onCheck SetAscending ] []
|
||||||
|
, text "Sort ascending"
|
||||||
|
]
|
||||||
|
, label [ class "search-option" ]
|
||||||
|
[ input [ type' "checkbox", checked opts.searchInDescription, onCheck SetSearchInDescription ] []
|
||||||
|
, text "Search in description"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
decodeGithubResponse : Json.Decode.Value -> Msg
|
||||||
|
decodeGithubResponse value =
|
||||||
|
case Json.Decode.decodeValue responseDecoder value of
|
||||||
|
Ok results ->
|
||||||
|
HandleSearchResponse results
|
||||||
|
|
||||||
|
Err err ->
|
||||||
|
HandleSearchError (Just err)
|
||||||
|
|
||||||
|
|
||||||
|
onChange : (String -> msg) -> Attribute msg
|
||||||
|
onChange toMsg =
|
||||||
|
on "change" (Json.Decode.map toMsg Html.Events.targetValue)
|
||||||
|
|
||||||
|
|
||||||
|
decodeResponse : Json.Decode.Value -> Msg
|
||||||
|
decodeResponse json =
|
||||||
|
case Json.Decode.decodeValue responseDecoder json of
|
||||||
|
Err err ->
|
||||||
|
HandleSearchError (Just err)
|
||||||
|
|
||||||
|
Ok results ->
|
||||||
|
HandleSearchResponse results
|
||||||
|
|
||||||
|
|
||||||
|
port githubSearch : String -> Cmd msg
|
||||||
|
|
||||||
|
|
||||||
|
port githubResponse : (Json.Decode.Value -> msg) -> Sub msg
|
||||||
|
|
||||||
|
|
||||||
|
{-| NOTE: The following is not part of the exercise, but is food for thought if
|
||||||
|
you have extra time.
|
||||||
|
|
||||||
|
There are several opportunities to improve this getQueryString implementation.
|
||||||
|
A nice refactor of this would not change the type annotation! It would still be:
|
||||||
|
|
||||||
|
getQueryString : Model -> String
|
||||||
|
|
||||||
|
Try identifying patterns and writing helper functions which are responsible for
|
||||||
|
handling those patterns. Then have this function call them. Things to consider:
|
||||||
|
|
||||||
|
* There's pattern of adding "+foo:bar" - could we write a helper function for this?
|
||||||
|
* In one case, if the "bar" in "+foo:bar" is empty, we want to return "" instead
|
||||||
|
of "+foo:" - is this always true? Should our helper function always do that?
|
||||||
|
* We also join query parameters together with "=" and "&" a lot. Can we give
|
||||||
|
that pattern a similar treatment? Should we also take "?" into account?
|
||||||
|
|
||||||
|
If you have time, give this refactor a shot and see how it turns out!
|
||||||
|
|
||||||
|
Writing something out the long way like this, and then refactoring to something
|
||||||
|
nicer, is generally the preferred way to go about building things in Elm.
|
||||||
|
-}
|
||||||
|
getQueryString : Model -> String
|
||||||
|
getQueryString model =
|
||||||
|
-- See https://developer.github.com/v3/search/#example for how to customize!
|
||||||
|
"access_token="
|
||||||
|
++ Auth.token
|
||||||
|
++ "&q="
|
||||||
|
++ model.query
|
||||||
|
++ (if model.options.searchInDescription then
|
||||||
|
"+in:name,description"
|
||||||
|
else
|
||||||
|
"+in:name"
|
||||||
|
)
|
||||||
|
++ "+language:elm"
|
||||||
|
++ (if String.isEmpty model.options.userFilter then
|
||||||
|
""
|
||||||
|
else
|
||||||
|
"+user:" ++ model.options.userFilter
|
||||||
|
)
|
||||||
|
++ "&sort="
|
||||||
|
++ model.options.sort
|
||||||
|
++ "&order="
|
||||||
|
++ (if model.options.ascending then
|
||||||
|
"asc"
|
||||||
|
else
|
||||||
|
"desc"
|
||||||
|
)
|
||||||
14
part11/Main.elm
Normal file
14
part11/Main.elm
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
module Main exposing (main)
|
||||||
|
|
||||||
|
import ElmHub
|
||||||
|
import Html.App as Html
|
||||||
|
|
||||||
|
|
||||||
|
main : Program Never
|
||||||
|
main =
|
||||||
|
Html.program
|
||||||
|
{ view = ElmHub.view
|
||||||
|
, update = ElmHub.update
|
||||||
|
, init = ElmHub.init
|
||||||
|
, subscriptions = ElmHub.subscriptions
|
||||||
|
}
|
||||||
22
part11/README.md
Normal file
22
part11/README.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
Part 11
|
||||||
|
=======
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
elm-package install
|
||||||
|
```
|
||||||
|
|
||||||
|
(Answer `y` when prompted.)
|
||||||
|
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```bash
|
||||||
|
elm-live Main.elm --open --pushstate --output=elm.js
|
||||||
|
```
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
* [Scaling the Elm Architecture](http://guide.elm-lang.org/reuse/)
|
||||||
|
* [Sortable Table documentation](http://package.elm-lang.org/packages/evancz/elm-sortable-table/latest)
|
||||||
BIN
part11/elm-hub.png
Normal file
BIN
part11/elm-hub.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
22
part11/elm-package.json
Normal file
22
part11/elm-package.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"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": {
|
||||||
|
"Fresheyeball/elm-tuple-extra": "2.1.0 <= v < 3.0.0",
|
||||||
|
"NoRedInk/elm-decode-pipeline": "1.1.2 <= v < 2.0.0",
|
||||||
|
"elm-lang/core": "4.0.1 <= v < 5.0.0",
|
||||||
|
"elm-lang/html": "1.0.0 <= v < 2.0.0",
|
||||||
|
"elm-lang/navigation": "1.0.0 <= v < 2.0.0",
|
||||||
|
"evancz/elm-http": "3.0.1 <= v < 4.0.0",
|
||||||
|
"evancz/elm-sortable-table": "1.0.0 <= v < 2.0.0",
|
||||||
|
"evancz/url-parser": "1.0.0 <= v < 2.0.0"
|
||||||
|
},
|
||||||
|
"elm-version": "0.17.0 <= v < 0.18.0"
|
||||||
|
}
|
||||||
2
part11/github.js
Normal file
2
part11/github.js
Normal file
File diff suppressed because one or more lines are too long
32
part11/index.html
Normal file
32
part11/index.html
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<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) {
|
||||||
|
github.getSearch(query).repositories({}, function (err, repositories) {
|
||||||
|
app.ports.githubResponse.send(repositories);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
app.ports.githubSearch.subscribe(searchGithub);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</html>
|
||||||
139
part11/style.css
Normal file
139
part11/style.css
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
background-color: #FF9632;
|
||||||
|
padding: 20px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
overflow-x: auto;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-input {
|
||||||
|
display: block;
|
||||||
|
float: left;
|
||||||
|
width: 50%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-options {
|
||||||
|
position: relative;
|
||||||
|
float: right;
|
||||||
|
width: 50%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-option {
|
||||||
|
display: block;
|
||||||
|
float: left;
|
||||||
|
width: 50%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-option input[type="text"] {
|
||||||
|
padding: 5px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
width: 90%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search:after {
|
||||||
|
content: "";
|
||||||
|
display: table;
|
||||||
|
clear: both;
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-label {
|
||||||
|
display: block;
|
||||||
|
color: #555;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user