From b76a1f17e9f3bc20fae2cbe705eaf7d7d3edd963 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sat, 11 Aug 2018 17:29:07 -0400 Subject: [PATCH] Update README --- README.md | 16 +++- intro/part5/src/Article/Feed.elm | 126 +++++++++++-------------------- 2 files changed, 56 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 70d0d59..e43c57f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,19 @@ Getting Started 1. Install [Node.js](http://nodejs.org) 7.0.0 or higher -2. Clone this repository +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: enable "[`elm-format`](https://github.com/avh4/elm-format) on save" in your editor. + +4. Run the following command to install all the other Elm tools: + +> **Note:** Make sure not to run this command with `sudo`! If it gives you an `EACCESS` error, apply [**this fix**](https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-two-change-npms-default-directory) and then re-run the command (still without `sudo`). + +```shell +npm install -g elm-test@beta elm-format@rc +``` + +5. Clone this repository Run this at the terminal: @@ -14,4 +26,4 @@ git clone https://github.com/rtfeldman/elm-0.19-workshop.git cd elm-0.19-workshop ``` -3. Continue with either the [`intro`](https://github.com/rtfeldman/elm-0.19-workshop/blob/master/intro/README.md) or [`advanced`](https://github.com/rtfeldman/elm-0.19-workshop/blob/master/advanced/README.md) instructions, depending on which workshop you're doing! +6. Continue with either the [`intro`](https://github.com/rtfeldman/elm-0.19-workshop/blob/master/intro/README.md) or [`advanced`](https://github.com/rtfeldman/elm-0.19-workshop/blob/master/advanced/README.md) instructions, depending on which workshop you're doing! diff --git a/intro/part5/src/Article/Feed.elm b/intro/part5/src/Article/Feed.elm index 5cf73bf..a5a482a 100644 --- a/intro/part5/src/Article/Feed.elm +++ b/intro/part5/src/Article/Feed.elm @@ -1,11 +1,7 @@ module Article.Feed exposing - ( FeedConfig - , ListConfig - , Model + ( Model , Msg - , defaultFeedConfig - , defaultListConfig , init , selectTag , update @@ -41,6 +37,24 @@ import Viewer exposing (Viewer) import Viewer.Cred as Cred exposing (Cred) +{-| NOTE: This module has its own Model, view, and update. This is not normal! +If you find yourself doing this often, please watch + +This is the reusable Article Feed that appears on both the Home page as well as +on the Profile page. There's a lot of logic here, so it's more convenient to use +the heavyweight approach of giving this its own Model, view, and update. + +This means callers must use Html.map and Cmd.map to use this thing, but in +this case that's totally worth it because of the amount of logic wrapped up +in this thing. + +For every other reusable view in this application, this API would be totally +overkill, so we use simpler APIs instead. + +-} + + + -- MODEL @@ -325,34 +339,42 @@ fetch maybeCred page feedSource = offset = (page - 1) * articlesPerPage - listConfig = - { defaultListConfig | offset = offset, limit = articlesPerPage } + params = + [ ( "limit", String.fromInt articlesPerPage ) + , ( "offset", String.fromInt offset ) + ] in Task.map (PaginatedList.mapPage (\_ -> page)) <| case feedSource of YourFeed cred -> - let - feedConfig = - { defaultFeedConfig | offset = offset, limit = articlesPerPage } - in - feed feedConfig cred + params + |> buildFromQueryParams (Just cred) (Api.url [ "articles", "feed" ]) + |> Cred.addHeader cred + |> HttpBuilder.toRequest |> Http.toTask GlobalFeed -> - list listConfig maybeCred - |> Http.toTask + list maybeCred params TagFeed tagName -> - list { listConfig | tag = Just tagName } maybeCred - |> Http.toTask + list maybeCred (( "tag", Tag.toString tagName ) :: params) FavoritedFeed username -> - list { listConfig | favorited = Just username } maybeCred - |> Http.toTask + list maybeCred (( "favorited", Username.toString username ) :: params) AuthorFeed username -> - list { listConfig | author = Just username } maybeCred - |> Http.toTask + list maybeCred (( "author", Username.toString username ) :: params) + + +list : + Maybe Cred + -> List ( String, String ) + -> Task Http.Error (PaginatedList (Article Preview)) +list maybeCred params = + buildFromQueryParams maybeCred (Api.url [ "articles" ]) params + |> Cred.addHeaderIfAvailable maybeCred + |> HttpBuilder.toRequest + |> Http.toTask replaceArticle : Article a -> Article a -> Article a @@ -365,70 +387,6 @@ replaceArticle newArticle oldArticle = --- LIST - - -type alias ListConfig = - { tag : Maybe Tag - , author : Maybe Username - , favorited : Maybe Username - , limit : Int - , offset : Int - } - - -defaultListConfig : ListConfig -defaultListConfig = - { tag = Nothing - , author = Nothing - , favorited = Nothing - , limit = 20 - , offset = 0 - } - - -list : ListConfig -> Maybe Cred -> Http.Request (PaginatedList (Article Preview)) -list config maybeCred = - [ Maybe.map (\tag -> ( "tag", Tag.toString tag )) config.tag - , Maybe.map (\author -> ( "author", Username.toString author )) config.author - , Maybe.map (\favorited -> ( "favorited", Username.toString favorited )) config.favorited - , Just ( "limit", String.fromInt config.limit ) - , Just ( "offset", String.fromInt config.offset ) - ] - |> List.filterMap identity - |> buildFromQueryParams maybeCred (Api.url [ "articles" ]) - |> Cred.addHeaderIfAvailable maybeCred - |> HttpBuilder.toRequest - - - --- FEED - - -type alias FeedConfig = - { limit : Int - , offset : Int - } - - -defaultFeedConfig : FeedConfig -defaultFeedConfig = - { limit = 10 - , offset = 0 - } - - -feed : FeedConfig -> Cred -> Http.Request (PaginatedList (Article Preview)) -feed config cred = - [ ( "limit", String.fromInt config.limit ) - , ( "offset", String.fromInt config.offset ) - ] - |> buildFromQueryParams (Just cred) (Api.url [ "articles", "feed" ]) - |> Cred.addHeader cred - |> HttpBuilder.toRequest - - - -- SERIALIZATION