Skip to content

Instantly share code, notes, and snippets.

@nicck
Created May 17, 2017 10:42
Show Gist options
  • Save nicck/3f666755f4c3ca09cbfbb6aa0f24f47b to your computer and use it in GitHub Desktop.
Save nicck/3f666755f4c3ca09cbfbb6aa0f24f47b to your computer and use it in GitHub Desktop.

Revisions

  1. nicck created this gist May 17, 2017.
    32 changes: 32 additions & 0 deletions Counter.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    module Counter exposing (view, update, Msg)

    import Data exposing (Model)
    import Html exposing (Html, body, div, button, text)
    import Html.Attributes exposing (attribute, class)
    -- import Html.Events exposing (onClick)

    type Msg
    = Increment Int
    | Decrement Int

    update : Msg -> Model -> Model
    update msg model =
    case msg of
    Increment off ->
    { model | counter = model.counter + off }

    Decrement off ->
    { model | counter = model.counter - off }

    view : String -> Model -> Html msg
    view label model =
    div [ class "counter" ]
    -- [ button [ onClick (Decrement 2) ] [ text "-" ]
    [ button [ ] [ text "-" ]
    , div []
    [ text (label ++ ": ")
    , text (toString model.counter)
    ]
    -- , button [ onClick (Increment 2) ] [ text "+" ]
    , button [ ] [ text "+" ]
    ]
    5 changes: 5 additions & 0 deletions Data.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    module Data exposing (Model)

    type alias Model =
    { counter : Int
    }
    26 changes: 26 additions & 0 deletions Main.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    module Main exposing (..)

    import Html exposing (Html, body, div, button, text)
    import Html.Attributes exposing (attribute, class)
    import Counter
    import Data exposing (Model)

    main : Program Never Model Msg
    main =
    Html.beginnerProgram { model = model, view = view, update = update }

    model : Model
    model = { counter = 10 }

    type Msg = Counter.Msg | Int

    update : Msg -> Model -> Model
    update msg model =
    case msg of
    Counter.Msg ->
    Counter.update msg model


    view : Model -> Html Msg
    view model =
    body [ attribute "id" "main" ] [ Counter.view "Counter" model ]