Skip to content

Instantly share code, notes, and snippets.

@xarvh
Created March 11, 2016 10:18
Show Gist options
  • Save xarvh/98ac3620c5525f3d1730 to your computer and use it in GitHub Desktop.
Save xarvh/98ac3620c5525f3d1730 to your computer and use it in GitHub Desktop.
module Counter where
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
import Effects
import Task
-- MODEL
type alias Model = Int
initModel : Model
initModel = 0
-- Update
type Action = Increment | Decrement | Change Int
effectDelayedChange : Int -> Effects.Effects Action
effectDelayedChange delta =
Effects.task <| Task.sleep 1000 `Task.andThen` \_ -> Task.succeed <| Change <| delta * 2
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case action of
Increment -> (model, effectDelayedChange (0+1))
Decrement -> (model, effectDelayedChange (0-1))
Change delta -> (model + delta, Effects.none)
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div [] [
button [ onClick address Decrement ] [ text "-" ],
div [ countStyle ] [ text (toString model)],
button [ onClick address Increment ] [ text "+" ]
]
countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment