Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module CounterList exposing (..)
import Counter
import Html exposing (..)
import Html.Events exposing (..)
import Html.App
-- MODEL
type alias ID = Int
type alias Model =
{ counters : Dict ID Counter.Model
, nextID : ID
}
init : Model
init =
{ counters = Dict.empty
, nextID = 0
}
-- UPDATE
type Msg
= Insert
| Remove ID
| Modify ID Counter.Msg
update : Msg -> Model -> Model
update msg model =
case msg of
Insert ->
{ model |
counters = Dict.insert model.nextID (Counter.init 0) model.counters,
nextID = model.nextID + 1
}
Remove id ->
{ model |
counters = Dict.remove id model.counters
}
Modify id counterMsg ->
case counterMsg of
Counter.Remove -> update (Remove id) model
_ ->
{ model |
counters =
Dict.update
id
(Maybe.map (Counter.update counterMsg))
model.counters
}
-- VIEW
view : Model -> Html Msg
view model =
let
insert = button [ onClick Insert ] [ text "Add" ]
counters = Dict.toList model.counters |> List.map viewCounter
in
div [] (insert :: counters)
viewCounter : (ID, Counter.Model) -> Html Msg
viewCounter (id, model) =
Html.App.map (Modify id) (Counter.viewWithRemoveButton model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment