Created
November 17, 2016 14:21
-
-
Save garbas/b83731fddfac9d80f8c96c814abb0b53 to your computer and use it in GitHub Desktop.
something
This file contains hidden or 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 (main) | |
import Http | |
import Html.App as App | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
-- MODEL | |
type alias Counter = Int | |
type alias Response = | |
{ id : Int | |
, title : String | |
} | |
type alias Model = | |
{ counter : Counter | |
, response : Response | |
} | |
init : (Model, Cmd Msg) | |
init = | |
({ counter = 0, }, Cmd.none) | |
type Msg | |
= IncCounter | |
| DecCounter | |
| SetCounter Counter | |
| GetRequest String | |
decodeFetchData : JsonDecode.Decoder Response | |
decodeResponse = | |
( JsonDecode.object2 Response | |
( "id" := JsonDecode.int) | |
( "title" := JsonDecode.string ) | |
-- | |
-- UPDATE | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
IncCounter -> | |
( { model | counter = new_value } | |
, Cmd.none | |
) | |
DecCounter -> | |
( { model | counter = model.counter - 1 } | |
, Cmd.none | |
) | |
SetCounter newCounter-> | |
( { model | counter = newCounter } | |
, Cmd.none | |
) | |
GetRequest url -> | |
( model | |
, Http.get GetRequestDone decodeResponse url | |
) | |
GetRequestDone response -> | |
( model | response | response | |
, Cmd.none | |
) | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
ul | |
[ ] | |
[ div [] [ text (toString model.counter) ] | |
, button [ onClick IncCounter ] | |
[ text "Up" ] | |
, button [ onClick DecCounter ] | |
[ text "Down" ] | |
, button [ SetCounter 10 |> onClick ] | |
[ text "Set to 10" ] | |
] | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- PROGRAM | |
main = | |
App.program | |
{ init = init | |
, update = update | |
, view = view | |
, subscriptions = subscriptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment