Created
September 3, 2017 01:35
-
-
Save ksunair/11e4ca96e2296cf811e46c8d155d4dc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (..) | |
import Html.Events exposing (..) | |
import Http | |
type alias Model = | |
{ rate : String | |
} | |
initData : Model | |
initData = | |
{ rate = "0" | |
} | |
dummyData : Model | |
dummyData = | |
{ rate = "dummy" | |
} | |
type Msg | |
= GetData | |
| HttpDataComplete (Result Http.Error String) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
GetData -> | |
( model, httpGetData ) | |
HttpDataComplete (Ok val) -> | |
( { model | rate = val }, Cmd.none ) | |
HttpDataComplete (Err _) -> | |
( initData, Cmd.none ) | |
httpGetData : Cmd Msg | |
httpGetData = | |
let | |
uri = | |
"https://api.cryptonator.com/api/ticker/ltc-usd" | |
request = | |
Http.getString uri | |
in | |
Http.send HttpDataComplete request | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ h3 [] [ text ("Current rate = " ++ model.rate) ] | |
, button [ onClick GetData ] [ text "Get Data" ] | |
] | |
subscription : Model -> Sub Msg | |
subscription model = | |
Sub.none | |
main = | |
Html.program | |
{ init = ( initData, Cmd.none ) | |
, view = view | |
, update = update | |
, subscriptions = subscription | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment