Permalink
Browse files

let the children show off their candy stashes

  • Loading branch information...
BrianHicks committed Jun 27, 2016
1 parent 8867e6b commit 816e0d04b4479f04129da2edbfb4e4ada56803bb
Showing with 67 additions and 16 deletions.
  1. +35 −6 Child.elm
  2. +32 −10 Parent.elm
@@ -5,40 +5,69 @@ import Html.Events exposing (onClick)
type alias Model =
{ money : Float }
{ money : Float
, jealousy : Float
}
init : Model
init =
{ money = 0 }
{ money = 0
, jealousy = 0
}
type Msg
= Allowance Float
| Candy
| SeeOthersCandy
| ShowOffCandy
update : Msg -> Model -> ( Model, Float )
type OutMsg
= NeedMoney Float
| BragAboutCandy
update : Msg -> Model -> ( Model, Maybe OutMsg )
update msg model =
case msg of
Allowance amount ->
( { model | money = model.money + amount }
, 0
, Nothing
)
SeeOthersCandy ->
( { model | jealousy = model.jealousy + 1 }
, Nothing
)
ShowOffCandy ->
( model
, Just BragAboutCandy
)
Candy ->
let
money =
model.money - 5
moneyMessage =
if money < 0 then
Just (NeedMoney (abs money))
else
Nothing
in
( { model | money = money }
, min money 0 |> abs
( { model | money = money, jealousy = 0 }
, moneyMessage
)
view : Model -> Html Msg
view model =
div []
[ p [] [ "Allowance money: $" ++ (toString model.money) |> text ]
, p [] [ "Jealousy: " ++ (toString model.jealousy) |> text ]
, button [ onClick Candy ] [ text "Blow $5 on candy" ]
, button [ onClick ShowOffCandy ] [ text "Show off sweet candy stash" ]
]
@@ -65,21 +65,43 @@ update msg model =
Just child ->
let
( updated, amount ) =
( updated, childMsg ) =
Child.update msg' child
( child', money ) =
if amount > 0 then
( Child.update (Child.Allowance amount) updated |> fst
, model.money - amount
)
else
( updated, model.money )
( child', model' ) =
updateFromChild childMsg name updated model
children =
Dict.insert name child' model.children
Dict.insert name child' model'.children
in
{ model | children = children, money = money }
{ model' | children = children }
updateFromChild : Maybe Child.OutMsg -> String -> Child.Model -> Model -> ( Child.Model, Model )
updateFromChild msg name child model =
case msg of
Nothing ->
( child, model )
Just (Child.NeedMoney amount) ->
if amount > 0 then
( Child.update (Child.Allowance amount) child |> fst
, { model | money = model.money - amount }
)
else
( child, model )
Just Child.BragAboutCandy ->
let
showOff =
\name' child ->
if name' == name then
child
else
Child.update Child.SeeOthersCandy child |> fst
in
( child
, { model | children = Dict.map showOff model.children })
view : Model -> Html Msg

0 comments on commit 816e0d0

Please sign in to comment.