Created
May 23, 2018 16:44
-
-
Save SidneyNemzer/e06c80da67c1bc92bd5460640f27ff15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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