Skip to content

Instantly share code, notes, and snippets.

@SidneyNemzer
Created May 23, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SidneyNemzer/e06c80da67c1bc92bd5460640f27ff15 to your computer and use it in GitHub Desktop.
Save SidneyNemzer/e06c80da67c1bc92bd5460640f27ff15 to your computer and use it in GitHub Desktop.
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