Skip to content

Instantly share code, notes, and snippets.

@newtriks
Forked from berndca/HttpAction.elm
Created November 19, 2015 09:27
Show Gist options
  • Save newtriks/813d9d42054b1611dfd3 to your computer and use it in GitHub Desktop.
Save newtriks/813d9d42054b1611dfd3 to your computer and use it in GitHub Desktop.
Http.getString at startup
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