Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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