Skip to content

Instantly share code, notes, and snippets.

@michaelglass
Created October 27, 2015 14:48
Show Gist options
  • Save michaelglass/a9e9f2b42d423811e478 to your computer and use it in GitHub Desktop.
Save michaelglass/a9e9f2b42d423811e478 to your computer and use it in GitHub Desktop.
module CounterList where
import Counter
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- MODEL
type alias Model =
{ counters : List (ID, Counter.Model )
, nextID: ID
}
init =
{ counters = []
, nextID = 0
}
type alias ID = Int
-- UPDATE
type Action
= Insert
| Remove ID
| Modify ID Counter.Action
update action model =
case action of
Insert ->
let newCounter = (model.nextID, Counter.init 0)
newCounters = [newCounter] ++ model.counters
in
{ model |
counters <- newCounters,
nextID <- model.nextID + 1
}
Remove id ->
let removeFilter (counterID, counter) = not (counterID == id)
in
{ model | counters <- List.filter removeFilter model.counters }
Modify id counterAction ->
let updateCounter ( counterID, counterModel) =
if counterID == id
then (counterID, Counter.update counterAction counterModel)
else (counterID, counterModel)
in
{ model | counters <- List.map updateCounter model.counters }
-- VIEW
view address model =
let counters = List.map (viewCounter address) model.counters
insert = button [ onClick address Insert ] [ text "Add"]
in
div [] ([insert] ++ counters)
viewCounter address (id, model) =
div []
[ Counter.view (Signal.forwardTo address (Modify id)) model
, button [ onClick address (Remove id) ] [ text "Remove" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment