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/Server.elm | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/client/elm/Server.elm') diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm index 3a6c86a..e50de7e 100644 --- a/src/client/elm/Server.elm +++ b/src/client/elm/Server.elm @@ -14,16 +14,19 @@ import Json.Decode as Json exposing ((:=)) import Date import Time exposing (Time) import Debug +import String import SimpleHTTP exposing (..) import Model.Action as U exposing (Action) +import Model.Action.AddPaymentAction as AddPayment import Model.Action.LoggedInAction as UL exposing (LoggedInAction) import Model.Action.MonthlyAction as UM exposing (MonthlyAction) import Model.Action.AccountAction as UA exposing (AccountAction) import Model.Payment exposing (..) import Model.Payer exposing (Payers, payersDecoder) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) +import Model.Translations exposing (Translations, getMessage) import Update.SignIn exposing (updateSignIn) @@ -32,11 +35,17 @@ signIn assertion = post ("/signIn?assertion=" ++ assertion) |> flip Task.andThen (always initViewAction) -addPayment : String -> Int -> PaymentFrequency -> Task Http.Error LoggedInAction -addPayment name cost frequency = - post ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)) +addPayment : Translations -> String -> String -> PaymentFrequency -> Task Http.Error LoggedInAction +addPayment translations name cost frequency = + post ("/payment/add?name=" ++ name ++ "&cost=" ++ cost ++ "&frequency=" ++ (toString frequency)) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) - |> Task.map (\paymentId -> (UL.ValidateAddPayment paymentId name cost frequency)) + |> Task.map (\paymentId -> + case String.toInt cost of + Err _ -> + UL.UpdateAdd (AddPayment.AddError Nothing (Just (getMessage "CostRequired" translations))) + Ok costNumber -> + UL.ValidateAddPayment paymentId name costNumber frequency + ) deletePayment : Payment -> PaymentFrequency -> Task Http.Error LoggedInAction deletePayment payment frequency = -- cgit v1.2.3