Commit f1c70faa authored by Stefan Siegl's avatar Stefan Siegl

initial version

parents
elm-stuff
{
"message": "hello world"
}
\ No newline at end of file
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
{
"token": "5a0c95597156f"
}
\ No newline at end of file
module Main exposing (main)
import Html exposing (..)
import Http
import Json.Decode as Decode
import Task
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
-- MODEL
type alias Model =
{ message : Maybe String
}
init : ( Model, Cmd Msg )
init =
( Model Nothing, doRequest )
doRequest : Cmd Msg
doRequest =
Decode.field "token" Decode.string
|> Http.get "first.json"
|> Http.toTask
|> Task.andThen secondRequest
|> Task.attempt HandleResult
secondRequest : String -> Task.Task Http.Error String
secondRequest token =
Decode.field "message" Decode.string
|> Http.get (token ++ ".json")
|> Http.toTask
-- UPDATE
type Msg
= HandleResult (Result Http.Error String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
HandleResult (Err _) ->
( model, Cmd.none )
HandleResult (Ok x) ->
( { model | message = Just x }, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
div []
[ span []
[ text <|
case model.message of
Nothing ->
"no result yet"
Just x ->
x
]
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment