From d9df5c3fcffe12aac239b58ccf2fd82c19c3be62 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 3 Jan 2016 23:37:14 +0100 Subject: Validate add payment server side --- src/client/elm/Update/LoggedIn/AddPayment.elm | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/client/elm/Update/LoggedIn/AddPayment.elm') 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) -- cgit v1.2.3