-
-
Save newtriks/813d9d42054b1611dfd3 to your computer and use it in GitHub Desktop.
Http.getString at startup
This file contains 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 StartApp | |
import Http | |
import Markdown | |
import Html exposing (Html, div, button, text) | |
import Html.Events exposing (onClick) | |
import Task exposing (Task) | |
import Effects exposing (Effects) | |
-- the URL of the README.md that we desire | |
readmeUrl : String | |
readmeUrl = | |
"https://raw.githubusercontent.com/elm-lang/core/master/README.md" | |
initialModel = div [] [] | |
type Action = FetchReadme String | |
| ReceiveReadme String | |
| FetchError Http.Error | |
fetchTask model urlString = | |
let | |
request = Task.map | |
(\resp -> ReceiveReadme resp) | |
(Http.getString urlString) | |
neverFailingRequest = Task.onError | |
request | |
(\err -> Task.succeed (FetchError err)) | |
in | |
( model, Effects.task neverFailingRequest ) | |
update action model = | |
case action of | |
FetchReadme urlString -> | |
fetchTask model urlString | |
ReceiveReadme str -> | |
( Markdown.toHtml str, Effects.none ) | |
FetchError err -> | |
( (div [] [text (toString err)]), Effects.none ) | |
view actionDispatcher model = | |
div [] | |
[ button [onClick actionDispatcher (FetchReadme readmeUrl)] [text "Fetch Readme"] | |
, button [onClick actionDispatcher (FetchReadme "readmeUrl")] [text "Fetch Error"] | |
, model | |
] | |
app = | |
StartApp.start | |
{ init = fetchTask initialModel readmeUrl | |
, update = update | |
, view = view | |
, inputs = [] | |
} | |
main = | |
app.html | |
port tasks : Signal (Task Effects.Never ()) | |
port tasks = | |
app.tasks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment