Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Signal exposing (Address)
import Html exposing (Html, button, text, div)
import Html.Events exposing (onClick)
import StartApp.Simple as StartApp exposing (start)
type alias State =
{ counter : Int }
type Action
= Increment
| Decrement
update : Action -> State -> State
update action state =
case action of
Increment ->
{ state | counter = state.counter + 1 }
Decrement ->
{ state | counter = state.counter - 1 }
type alias ViewState =
{ textLabel : String
, button1Label : String
, button2Label : String
}
type Event
= Button1Click
| Button2Click
view : Address Event -> ViewState -> Html
view address state =
div []
[ button
[ onClick address Button1Click ]
[ text state.button1Label ]
, button
[ onClick address Button2Click ]
[ text state.button2Label ]
, text state.textLabel
]
eventToAction : Event -> Action
eventToAction event =
case event of
Button1Click ->
Increment
Button2Click ->
Decrement
stateToViewState : State -> ViewState
stateToViewState state =
{ textLabel = toString state.counter
, button1Label = "Increment"
, button2Label = "Decrement"
}
render : Address Action -> State -> Html
render address state =
view (Signal.forwardTo address eventToAction) (stateToViewState state)
main =
start
{ model = State 0
, update = update
, view = render
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment