From 1cac3e33fd8ae1635522502dff9d7f9be60ae4cb Mon Sep 17 00:00:00 2001 From: Artus Date: Tue, 19 Nov 2019 14:37:46 +0100 Subject: [PATCH] impls basic Wealth editing --- src/Api.elm | 9 ++++++ src/Page/Chest.elm | 74 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/Api.elm b/src/Api.elm index e1f036d..47bdeef 100644 --- a/src/Api.elm +++ b/src/Api.elm @@ -334,6 +334,7 @@ type RequestData | BuyPayload Loot (Maybe Float) (List (Maybe Float)) | GrabPayload Loot | AddPayload String Loot + | WealthPayload Float zip xs ys = @@ -396,6 +397,9 @@ buildPayload data = , ( "source_name", E.string sourceName ) ] + WealthPayload amount -> + E.float amount + confirmAction : String -> RequestData -> Cmd Msg confirmAction id data = @@ -421,6 +425,11 @@ confirmAction id data = ( "http://localhost:8088/api/players/" ++ id ++ "/claims" , "POST" ) + + WealthPayload _ -> + ( "http://localhost:8088/api/players/" ++ id ++ "/wealth" + , "PUT" + ) in Http.request { method = method diff --git a/src/Page/Chest.elm b/src/Page/Chest.elm index 117c4b4..3877325 100644 --- a/src/Page/Chest.elm +++ b/src/Page/Chest.elm @@ -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 =