-
-
Save rootscript/098b843a6c0e2f6c8e2e145fccbbd12c to your computer and use it in GitHub Desktop.
How to extract the results of Http Requests in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.App exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import Html.Attributes exposing (..) | |
import Http | |
import Task exposing (Task) | |
import Json.Decode as Json exposing ((:=)) | |
type Msg | |
= NoOp | |
| FetchData | |
| ErrorOccurred String | |
| DataFetched (List RepoInfo) | |
type alias RepoInfo = | |
{ id : Int | |
, name : String | |
} | |
type alias Model = | |
{ message : String | |
, repos : List RepoInfo | |
} | |
main = Html.App.program | |
{ init = init | |
, update = update | |
, view = view | |
, subscriptions = \_ -> Sub.none | |
} | |
init = | |
let | |
model = | |
{ message = "Hello, Elm!" | |
, repos = [] | |
} | |
in | |
model ! [] | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
NoOp -> | |
model ! [] | |
FetchData -> | |
{ model | message = "Initiating data fetch!" } ! [fetchData] | |
ErrorOccurred errorMessage -> | |
{ model | message = "Oops! An error occurred: " ++ errorMessage } ! [] | |
DataFetched repos -> | |
{ model | repos = repos, message = "The data has been fetched!" } ! [] | |
view : Model -> Html Msg | |
view model = | |
let | |
showRepo repo = | |
li [] | |
[ text ("Repository ID: " ++ (toString repo.id) ++ "; ") | |
, text ("Repository Name: " ++ repo.name) | |
] | |
in | |
div [] | |
[ div [] [ text model.message ] | |
, button [ onClick FetchData ] [ text "Click to load nytimes repositories" ] | |
, ul [] (List.map showRepo model.repos) | |
] | |
repoInfoDecoder : Json.Decoder RepoInfo | |
repoInfoDecoder = | |
Json.object2 | |
RepoInfo | |
("id" := Json.int) | |
("name" := Json.string) | |
repoInfoListDecoder : Json.Decoder (List RepoInfo) | |
repoInfoListDecoder = | |
Json.list repoInfoDecoder | |
fetchData : Cmd Msg | |
fetchData = | |
Http.get repoInfoListDecoder "https://api.github.com/users/nytimes/repos" | |
|> Task.mapError toString | |
|> Task.perform ErrorOccurred DataFetched |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add anything useful a new user might need to understand ALL this code:
Type aliases, recursive types documentation is here: https://github.com/elm-lang/elm-compiler/blob/master/hints/recursive-alias.md
Json.Decode documentation is here: http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode
Json.Decode.Extra tutorial video is here: https://elmseeds.thaterikperson.com/json-decode-extra
Json.Decode.Extra documentation is here: http://package.elm-lang.org/packages/elm-community/json-extra/1.1.0/Json-Decode-Extra
elm-http documentation is here: https://github.com/evancz/elm-http
elm-task documentation is here: http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Task
Task tutorial is here: https://www.elm-tutorial.org/en/03-subs-cmds/03-tasks.html
Task example code is here: http://stackoverflow.com/questions/37746578/how-to-convert-task-http-rawerror-http-response-to-task-string-int-int
elm-cmd commands tutorial is here: https://www.elm-tutorial.org/en/03-subs-cmds/02-commands.html
elm-cheat-sheet is here: https://github.com/izdi/elm-cheat-sheet