diff --git a/part10/ElmHub.elm b/part10/ElmHub.elm index 38aead5..6300c50 100644 --- a/part10/ElmHub.elm +++ b/part10/ElmHub.elm @@ -3,6 +3,7 @@ module ElmHub (..) where import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) +import Html.Lazy exposing (..) import Http import Auth import Task exposing (Task) @@ -34,7 +35,8 @@ searchFeed query = responseDecoder : Decoder (List SearchResult.Model) responseDecoder = - "items" := Json.Decode.list SearchResult.decoder + -- TODO make use of SearchResult's decoder + Json.Decode.succeed [] type alias Model = @@ -72,15 +74,7 @@ viewSearchResults address results = results |> Dict.values |> List.sortBy (.stars >> negate) - |> filterResults - |> List.map (SearchResult.view address DeleteById) - - -filterResults : List SearchResult.Model -> List SearchResult.Model -filterResults results = - -- TODO filter out repos with 0 stars - -- using a case-expression rather than List.filter - results + |> List.map (\_ -> div [] [ text "TODO replace this line with view logic from SearchResult" ]) onInput address wrap = diff --git a/part10/README.md b/part10/README.md index 50a317c..f08ee4f 100644 --- a/part10/README.md +++ b/part10/README.md @@ -1,5 +1,5 @@ -Part 10 -======= +Part 9 +====== ## Installation diff --git a/part10/SearchResult.elm b/part10/SearchResult.elm index 95a843c..e93bd0c 100644 --- a/part10/SearchResult.elm +++ b/part10/SearchResult.elm @@ -28,23 +28,18 @@ decoder = ("stargazers_count" := Json.Decode.int) -view : Address a -> (Int -> a) -> Model -> Html -view address delete result = +view : Address a -> Model -> Html +view address result = li [] [ span [ class "star-count" ] [ text (toString result.stars) ] , a - [ href - ("https://github.com/" - ++ (Debug.log - "TODO we should not see this when typing in the search box!" - result.name - ) - ) + [ href ("https://github.com/" ++ result.name) , target "_blank" ] [ text result.name ] , button - [ class "hide-result", onClick address (delete result.id) ] + -- TODO onClick, send a delete action to the address + [ class "hide-result" ] [ text "X" ] ] diff --git a/part11/ElmHub.elm b/part11/ElmHub.elm index 8476dbf..38aead5 100644 --- a/part11/ElmHub.elm +++ b/part11/ElmHub.elm @@ -3,7 +3,6 @@ module ElmHub (..) where import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) -import Html.Lazy exposing (..) import Http import Auth import Task exposing (Task) @@ -74,20 +73,14 @@ viewSearchResults address results = |> Dict.values |> List.sortBy (.stars >> negate) |> filterResults - |> List.map (lazy3 SearchResult.view address DeleteById) + |> List.map (SearchResult.view address DeleteById) filterResults : List SearchResult.Model -> List SearchResult.Model filterResults results = - case results of - [] -> - [] - - result :: rest -> - if result.stars > 0 then - result :: (filterResults rest) - else - filterResults rest + -- TODO filter out repos with 0 stars + -- using a case-expression rather than List.filter + results onInput address wrap = diff --git a/part11/README.md b/part11/README.md index f01b7a8..50a317c 100644 --- a/part11/README.md +++ b/part11/README.md @@ -1,4 +1,4 @@ -Part 11 +Part 10 ======= ## Installation @@ -15,9 +15,3 @@ to fail; in that case, just run `elm package install` again.) ```bash elm live Main.elm --open -- --output=elm.js ``` - -## Compiling CSS - -```bash -elm css Stylesheets.elm -``` diff --git a/part11/SearchResult.elm b/part11/SearchResult.elm index daf012a..95a843c 100644 --- a/part11/SearchResult.elm +++ b/part11/SearchResult.elm @@ -34,7 +34,13 @@ view address delete result = [] [ span [ class "star-count" ] [ text (toString result.stars) ] , a - [ href ("https://github.com/" ++ result.name) + [ href + ("https://github.com/" + ++ (Debug.log + "TODO we should not see this when typing in the search box!" + result.name + ) + ) , target "_blank" ] [ text result.name ] diff --git a/part11/elm-package.json b/part11/elm-package.json index 7f72d5a..ba40466 100644 --- a/part11/elm-package.json +++ b/part11/elm-package.json @@ -12,8 +12,7 @@ "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", - "rtfeldman/elm-css": "1.0.0 <= v < 2.0.0" + "evancz/start-app": "2.0.0 <= v < 3.0.0" }, "elm-version": "0.16.0 <= v < 0.17.0" } diff --git a/part11/style.css b/part11/style.css index 9d13aae..64c74d7 100644 --- a/part11/style.css +++ b/part11/style.css @@ -1,6 +1,92 @@ + .content { - width: 960px; - margin: 0 auto; - padding: 30px; - font-family: Helvetica, Arial, serif; + 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; } diff --git a/part12/ElmHub.elm b/part12/ElmHub.elm new file mode 100644 index 0000000..8476dbf --- /dev/null +++ b/part12/ElmHub.elm @@ -0,0 +1,132 @@ +module ElmHub (..) where + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Html.Lazy exposing (..) +import Http +import Auth +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?access_token=" + ++ Auth.token + ++ "&q=" + ++ query + ++ "+language:elm&sort=stars&order=desc" + + task = + Http.get responseDecoder url + |> Task.map SetResults + in + Task.onError task (\_ -> Task.succeed (SetResults [])) + + +responseDecoder : Decoder (List SearchResult.Model) +responseDecoder = + "items" := Json.Decode.list SearchResult.decoder + + +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) + |> filterResults + |> List.map (lazy3 SearchResult.view address DeleteById) + + +filterResults : List SearchResult.Model -> List SearchResult.Model +filterResults results = + case results of + [] -> + [] + + result :: rest -> + if result.stars > 0 then + result :: (filterResults rest) + else + filterResults rest + + +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 ) diff --git a/part11/ElmHub/Css.elm b/part12/ElmHub/Css.elm similarity index 100% rename from part11/ElmHub/Css.elm rename to part12/ElmHub/Css.elm diff --git a/part12/Main.elm b/part12/Main.elm index 5b8e88e..fa4bada 100644 --- a/part12/Main.elm +++ b/part12/Main.elm @@ -1,7 +1,7 @@ module Main (..) where import StartApp -import Component.ElmHub exposing (..) +import ElmHub exposing (..) import Effects exposing (Effects) import Task exposing (Task) import Html exposing (Html) diff --git a/part12/README.md b/part12/README.md index deddbd0..f01b7a8 100644 --- a/part12/README.md +++ b/part12/README.md @@ -1,4 +1,4 @@ -Part 12 +Part 11 ======= ## Installation @@ -15,3 +15,9 @@ to fail; in that case, just run `elm package install` again.) ```bash elm live Main.elm --open -- --output=elm.js ``` + +## Compiling CSS + +```bash +elm css Stylesheets.elm +``` diff --git a/part9/SearchResult.elm b/part12/SearchResult.elm similarity index 83% rename from part9/SearchResult.elm rename to part12/SearchResult.elm index e93bd0c..daf012a 100644 --- a/part9/SearchResult.elm +++ b/part12/SearchResult.elm @@ -28,8 +28,8 @@ decoder = ("stargazers_count" := Json.Decode.int) -view : Address a -> Model -> Html -view address result = +view : Address a -> (Int -> a) -> Model -> Html +view address delete result = li [] [ span [ class "star-count" ] [ text (toString result.stars) ] @@ -39,7 +39,6 @@ view address result = ] [ text result.name ] , button - -- TODO onClick, send a delete action to the address - [ class "hide-result" ] + [ class "hide-result", onClick address (delete result.id) ] [ text "X" ] ] diff --git a/part11/Stylesheets.elm b/part12/Stylesheets.elm similarity index 100% rename from part11/Stylesheets.elm rename to part12/Stylesheets.elm diff --git a/part12/elm-package.json b/part12/elm-package.json index ba40466..7f72d5a 100644 --- a/part12/elm-package.json +++ b/part12/elm-package.json @@ -12,7 +12,8 @@ "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" + "evancz/start-app": "2.0.0 <= v < 3.0.0", + "rtfeldman/elm-css": "1.0.0 <= v < 2.0.0" }, "elm-version": "0.16.0 <= v < 0.17.0" } diff --git a/part12/style.css b/part12/style.css index 64c74d7..9d13aae 100644 --- a/part12/style.css +++ b/part12/style.css @@ -1,92 +1,6 @@ - .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; + width: 960px; + margin: 0 auto; + padding: 30px; + font-family: Helvetica, Arial, serif; } diff --git a/part12/Component/ElmHub.elm b/part13/part12/Component/ElmHub.elm similarity index 100% rename from part12/Component/ElmHub.elm rename to part13/part12/Component/ElmHub.elm diff --git a/part12/Component/SearchResult.elm b/part13/part12/Component/SearchResult.elm similarity index 100% rename from part12/Component/SearchResult.elm rename to part13/part12/Component/SearchResult.elm diff --git a/part13/part12/Main.elm b/part13/part12/Main.elm new file mode 100644 index 0000000..5b8e88e --- /dev/null +++ b/part13/part12/Main.elm @@ -0,0 +1,27 @@ +module Main (..) where + +import StartApp +import Component.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 diff --git a/part13/part12/README.md b/part13/part12/README.md new file mode 100644 index 0000000..deddbd0 --- /dev/null +++ b/part13/part12/README.md @@ -0,0 +1,17 @@ +Part 12 +======= + +## 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 +``` diff --git a/part13/part12/elm-hub.png b/part13/part12/elm-hub.png new file mode 100644 index 0000000..ba32816 Binary files /dev/null and b/part13/part12/elm-hub.png differ diff --git a/part13/part12/elm-package.json b/part13/part12/elm-package.json new file mode 100644 index 0000000..ba40466 --- /dev/null +++ b/part13/part12/elm-package.json @@ -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" +} diff --git a/part13/part12/index.html b/part13/part12/index.html new file mode 100644 index 0000000..5db9b93 --- /dev/null +++ b/part13/part12/index.html @@ -0,0 +1,26 @@ + + + +
+ +