diff options
Diffstat (limited to 'src/client/elm/Update')
-rw-r--r-- | src/client/elm/Update/LoggedIn.elm | 18 | ||||
-rw-r--r-- | src/client/elm/Update/LoggedIn/AddPayment.elm | 28 |
2 files changed, 42 insertions, 4 deletions
diff --git a/src/client/elm/Update/LoggedIn.elm b/src/client/elm/Update/LoggedIn.elm index 74e213a..134aec2 100644 --- a/src/client/elm/Update/LoggedIn.elm +++ b/src/client/elm/Update/LoggedIn.elm @@ -4,9 +4,11 @@ module Update.LoggedIn import Date import Dict +import Debug +import Task import Effects exposing (Effects) -import Task +import Http exposing (Error(..)) import Server @@ -20,7 +22,7 @@ import Model.Action.AddPaymentAction as AddPayment import Model.View.LoggedInView exposing (..) import Model.View.LoggedIn.AddPayment exposing (..) -import Update.LoggedIn.AddPayment exposing (updateAddPayment) +import Update.LoggedIn.AddPayment exposing (updateAddPayment, addPaymentError) import Update.LoggedIn.Monthly exposing (updateMonthly) import Update.LoggedIn.Account exposing (updateAccount) @@ -42,8 +44,16 @@ updateLoggedIn model action loggedInView = AddPayment name cost frequency -> ( { loggedInView | add = updateAddPayment AddPayment.WaitingServer loggedInView.add } - , Server.addPayment name cost frequency - |> flip Task.onError (always <| Task.succeed NoOp) + , Server.addPayment model.translations name cost frequency + |> flip Task.onError (\err -> + case err of + BadResponse 400 jsonErr -> + case addPaymentError model.translations jsonErr of + Just addPaymentAction -> Task.succeed (UpdateAdd addPaymentAction) + Nothing -> Task.succeed NoOp + _ -> + Task.succeed NoOp + ) |> Effects.task ) diff --git a/src/client/elm/Update/LoggedIn/AddPayment.elm b/src/client/elm/Update/LoggedIn/AddPayment.elm index 3eb2ea4..4c9c484 100644 --- a/src/client/elm/Update/LoggedIn/AddPayment.elm +++ b/src/client/elm/Update/LoggedIn/AddPayment.elm @@ -1,26 +1,54 @@ module Update.LoggedIn.AddPayment ( updateAddPayment + , addPaymentError ) where +import Maybe +import Json.Decode as Json exposing ((:=)) + import Model.Action.AddPaymentAction exposing (..) import Model.View.LoggedIn.AddPayment exposing (..) +import Model.Translations exposing (Translations, getMessage) import Model.Payment exposing (PaymentFrequency(..)) updateAddPayment : AddPaymentAction -> AddPayment -> AddPayment updateAddPayment action addPayment = case action of + + NoOp -> + addPayment + UpdateName name -> { addPayment | name = name } + UpdateCost cost -> { addPayment | cost = cost } + AddError nameError costError -> { addPayment | nameError = nameError , costError = costError + , waitingServer = False } + ToggleFrequency -> { addPayment | frequency = if addPayment.frequency == Punctual then Monthly else Punctual } + WaitingServer -> { addPayment | waitingServer = True } + +addPaymentError : Translations -> String -> Maybe AddPaymentAction +addPaymentError translations jsonErr = + let decoder = + Json.object2 (,) + (Json.maybe <| "name" := Json.string) + (Json.maybe <| "cost" := Json.string) + in case Json.decodeString decoder jsonErr of + Err _ -> + Nothing + Ok (mbNameKey, mbCostKey) -> + Just <| AddError + (Maybe.map (flip getMessage translations) mbNameKey) + (Maybe.map (flip getMessage translations) mbCostKey) |