Skip to content

Instantly share code, notes, and snippets.

@berndca
Last active January 21, 2016 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save berndca/a731835afd33a997b543 to your computer and use it in GitHub Desktop.
Save berndca/a731835afd33a997b543 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