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

another example: use time in decoder

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