Skip to content

Instantly share code, notes, and snippets.

@pdamoc pdamoc/Main.elm
Last active Jun 17, 2016

Embed
What would you like to do?
SAM in Elm
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Events exposing (onClick)
import Time
-- MODEL
type Model
= Ready
| Countdown Int
| Aborted
| Launched
init : Model
init =
Ready
-- UPDATE
type Msg
= StartCountdown
| Tick
| Abort
update : Msg -> Model -> Model
update msg model =
case ( msg, model ) of
( StartCountdown, Ready ) ->
Countdown 10
( Tick, Countdown c ) ->
if c == 1 then
Launched
else
Countdown (c - 1)
( Abort, Countdown c ) ->
Aborted
( _, _ ) ->
model
-- VIEW
view : Model -> Html Msg
view model =
case model of
Ready ->
div []
[ button [ onClick StartCountdown ] [ text "Start Countdown" ] ]
Countdown count ->
div []
[ p [] [ text ("Countdown: " ++ (toString count)) ]
, button [ onClick Abort ] [ text "Abort" ]
]
Aborted ->
div [] [ text "Launch Aborted!" ]
Launched ->
div [] [ text "Rocket Launched!" ]
-- WIRING
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Countdown _ ->
Time.every Time.second (\_ -> Tick)
_ ->
Sub.none
main : Program Never
main =
App.program
{ init = init ! []
, update = \msg model -> update msg model ! []
, view = view
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.