-
Notifications
You must be signed in to change notification settings - Fork 0
/
CounterListFancy.elm
81 lines (55 loc) · 1.8 KB
/
CounterListFancy.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module CounterListFancy (update, view, init) where
import Counter exposing(Model, viewWithRemoveButton, init, Context)
import Html exposing (div, button, text, Html)
import Html.Events exposing (onClick)
-- Model
type alias Model = {
counters : List (ID, Counter.Model)
, nextID : ID
}
type alias ID = Int
init : Model
init =
{
counters = []
, nextID = 0
}
-- Action
type Action =
Insert
| Remove ID
| Modify ID Counter.Action
-- Update
update : Action -> Model -> Model
update action model =
case action of
Insert ->
{ model |
counters <- (model.nextID, Counter.init 0) :: model.counters
, nextID <- model.nextID + 1
}
Remove id ->
{ model | counters <- List.filter (\(currentId, _) -> currentId /= id) 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 : Signal.Address Action -> Model -> Html
view address model =
let
insert = button [onClick address Insert] [text "Insert"]
in
div [] (insert :: List.map (viewCounter address) model.counters)
viewCounter : Signal.Address Action -> (ID, Counter.Model) -> Html
viewCounter address (counterID, counterModel) =
let context =
Counter.Context
(Signal.forwardTo address (Modify counterID))
(Signal.forwardTo address (always (Remove counterID) ))
in
Counter.viewWithRemoveButton context counterModel