Commit 3bb3f31c authored by Stefan Siegl's avatar Stefan Siegl

another example: use time in decoder

parent cf27cc77
......@@ -22,7 +22,13 @@ main =
type alias Model =
{ message : Maybe String
{ info : Maybe InfoMessage
}
type alias InfoMessage =
{ message : String
, time : Time.Time
}
......@@ -37,7 +43,7 @@ init =
doRequest : Cmd Msg
doRequest =
Task.map2 buildTheUrl fetchToken Time.now
Task.map2 (,) fetchToken Time.now
|> Task.andThen secondRequest
|> Task.attempt HandleResult
......@@ -49,15 +55,11 @@ fetchToken =
|> Http.toTask
buildTheUrl : String -> Time.Time -> String
buildTheUrl token time =
token ++ ".json?timestamp=" ++ (toString time)
secondRequest : String -> Task.Task Http.Error String
secondRequest url =
secondRequest : ( String, Time.Time ) -> Task.Task Http.Error InfoMessage
secondRequest ( token, time ) =
Decode.field "message" Decode.string
|> Http.get url
|> Decode.map (\x -> InfoMessage x time)
|> Http.get (token ++ ".json")
|> Http.toTask
......@@ -66,7 +68,7 @@ secondRequest url =
type Msg
= HandleResult (Result Http.Error String)
= HandleResult (Result Http.Error InfoMessage)
update : Msg -> Model -> ( Model, Cmd Msg )
......@@ -76,7 +78,7 @@ update msg model =
( model, Cmd.none )
HandleResult (Ok x) ->
( { model | message = Just x }, Cmd.none )
( { model | info = Just x }, Cmd.none )
......@@ -88,11 +90,11 @@ view model =
div []
[ span []
[ text <|
case model.message of
case model.info of
Nothing ->
"no result yet"
Just x ->
x
Just { message, time } ->
message ++ " at " ++ (toString time)
]
]
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