impls basic Wealth editing

This commit is contained in:
2019-11-19 14:37:46 +01:00
parent 44e44c70bf
commit 1cac3e33fd
2 changed files with 74 additions and 9 deletions

View File

@@ -43,6 +43,9 @@ type alias State =
, itemList : Maybe (List String)
-- , inventoryItems : Loot
, editWealth : Bool
, wealthAmount : String
-- Fetched on init
, player : Api.Player
, playerLoot : Loot
@@ -80,6 +83,8 @@ init (Player navKey playerId) =
Nothing
Nothing
Nothing
False
"0.0"
Api.blankPlayer
[]
[]
@@ -225,8 +230,8 @@ viewHeaderBar title model =
-- PLAYER BAR
viewPlayerBar : Api.Player -> List (Html Msg) -> Html Msg
viewPlayerBar player actionControls =
viewPlayerBar : Api.Player -> List (Html Msg) -> ( Bool, String ) -> Html Msg
viewPlayerBar player actionControls ( editing, amount ) =
section [ class "hero is-dark is-bold" ]
[ div [ class "hero-body" ]
[ div [ class "level container is-mobile" ]
@@ -235,10 +240,15 @@ viewPlayerBar player actionControls =
[ p [ class "title is-3" ] [ text player.name ] ]
, div [ class "level-item" ]
[ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ]
, span [ class "icon" ] [ i [ class "fas fa-tools" ] [] ]
, span [ class "icon", onClick EditWealth ] [ i [ class "fas fa-tools" ] [] ]
]
]
++ viewWealth player.wealth
++ (if editing then
viewUpdateWealth amount
else
viewWealth player.wealth
)
++ (if player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
@@ -256,9 +266,18 @@ viewPlayerBar player actionControls =
]
viewUpdateWealth =
[ input [ class "level-item" ] []
, button [ class "level-item button" ] [ text "Ok" ]
viewUpdateWealth amount =
let
isAmountValid =
case String.toFloat amount of
Just _ ->
True
Nothing ->
False
in
[ input [ class "level-item", class "input", classList [ ( "is-danger", not isAmountValid ) ], value amount, onInput AmountChanged ] []
, button [ class "level-item button", onClick ConfirmEditWealth ] [ text "Ok" ]
]
@@ -307,7 +326,7 @@ view model =
"Marchand"
NewLoot ->
"Nouveau trésor :)"
"Nouveau trésor"
shownItems =
selectContent model
@@ -420,7 +439,7 @@ view model =
(\i -> String.toLower i.name |> String.contains (String.toLower model.searchText))
in
[ viewHeaderBar "Mon coffre" model
, viewPlayerBar model.state.player renderControls
, viewPlayerBar model.state.player renderControls ( model.state.editWealth, model.state.wealthAmount )
, main_
[ class "container" ]
[ viewNotification model
@@ -860,6 +879,10 @@ type Msg
| AddMsg AddMsg
-- Buy/Sell modes
| PriceModifierChanged Int String
-- Edit wealth
| EditWealth
| AmountChanged String
| ConfirmEditWealth
insensitiveContains : String -> String -> Bool
@@ -867,9 +890,42 @@ insensitiveContains substring string =
String.contains (String.toLower substring) (String.toLower string)
switchEditWealth state =
{ state | editWealth = not state.editWealth }
setWealthAmount state amount =
{ state
| wealthAmount = amount
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
EditWealth ->
( { model | state = switchEditWealth model.state }, Cmd.none )
AmountChanged amount ->
( { model | state = setWealthAmount model.state amount }, Cmd.none )
ConfirmEditWealth ->
let
amount =
case String.toFloat model.state.wealthAmount of
Just a ->
a
Nothing ->
0.0
in
( { model | state = setWealthAmount model.state "0" |> switchEditWealth }
, Cmd.map ApiMsg <|
Api.confirmAction
(String.fromInt model.state.player.id)
(Api.WealthPayload amount)
)
PriceModifierChanged id value ->
let
state =