From 0ae7d068263dffbc1cc2dc92c7829dd0037c97e7 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 6 Sep 2015 15:54:38 +0200 Subject: The user can remove a monthly payment --- src/client/ServerCommunication.elm | 93 +++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 46 deletions(-) (limited to 'src/client/ServerCommunication.elm') diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm index 30bd2bf..20e2b14 100644 --- a/src/client/ServerCommunication.elm +++ b/src/client/ServerCommunication.elm @@ -13,17 +13,20 @@ import Date import Model.Message exposing (messageDecoder) import Model.User exposing (UserId) import Model.Payment exposing (..) -import Model.View.Payment.Add exposing (Frequency) +import Model.View.Payment.Add exposing (Frequency(..)) import Update as U import Update.SignIn exposing (..) import Update.LoggedView as UL +import Update.LoggedView.Monthly as UM type Communication = NoCommunication | SignIn String - | AddPayment UserId String Int Frequency + | AddPayment UserId String Int + | AddMonthlyPayment String Int | DeletePayment PaymentId UserId Int Int + | DeleteMonthlyPayment PaymentId | UpdatePage Int | SignOut @@ -42,18 +45,22 @@ sendRequest communication = getRequest : Communication -> Maybe Http.Request getRequest communication = case communication of - NoCommunication -> - Nothing - SignIn login -> - Just (simple "post" ("/signIn?login=" ++ login)) - AddPayment userId name cost frequency -> - Just (simple "post" ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency))) - DeletePayment paymentId _ _ _ -> - Just (simple "post" ("payment/delete?id=" ++ (toString paymentId))) - UpdatePage page -> - Just (updatePageRequest page) - SignOut -> - Just (simple "post" "/signOut") + NoCommunication -> Nothing + SignIn login -> Just (simple "post" ("/signIn?login=" ++ login)) + AddPayment userId name cost -> Just (addPaymentRequest name cost Punctual) + AddMonthlyPayment name cost -> Just (addPaymentRequest name cost Monthly) + DeletePayment paymentId _ _ _ -> Just (deletePaymentRequest paymentId) + DeleteMonthlyPayment paymentId -> Just (deletePaymentRequest paymentId) + UpdatePage page -> Just (updatePageRequest page) + SignOut -> Just (simple "post" "/signOut") + +addPaymentRequest : String -> Int -> Frequency -> Http.Request +addPaymentRequest name cost frequency = + simple "post" ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)) + +deletePaymentRequest : PaymentId -> Http.Request +deletePaymentRequest id = + simple "post" ("payment/delete?id=" ++ (toString id)) updatePageRequest : Int -> Http.Request updatePageRequest page = @@ -75,46 +82,33 @@ serverResult communication response = NoCommunication -> Task.succeed U.NoOp SignIn login -> - Task.succeed (U.UpdateSignIn (ValidLogin login)) - AddPayment userId name cost frequency -> - decodeResponse - response + Task.succeed << U.UpdateSignIn <| ValidLogin login + AddPayment userId name cost -> + Http.send Http.defaultSettings (updatePageRequest 1) + |> flip Task.andThen (decodeOkResponse paymentsDecoder (\payments -> + Task.succeed <| U.UpdateLoggedView (UL.AddPayment userId name cost payments) + )) + AddMonthlyPayment name cost -> + decodeOkResponse ("id" := paymentIdDecoder) - (\paymentId -> - Http.send Http.defaultSettings (updatePageRequest 1) - |> flip Task.andThen (\response2 -> - if response2.status == 200 - then - decodeResponse - response2 - paymentsDecoder - (\payments -> Task.succeed <| U.UpdateLoggedView (UL.AddPayment userId paymentId name cost frequency payments)) - else - Task.succeed U.NoOp - ) - ) + (\id -> Task.succeed <| U.UpdateLoggedView (UL.AddMonthlyPayment id name cost)) + response DeletePayment id userId cost currentPage -> Http.send Http.defaultSettings (updatePageRequest currentPage) - |> flip Task.andThen (\response -> - if response.status == 200 - then - decodeResponse - response - paymentsDecoder - (\payments -> Task.succeed <| U.UpdateLoggedView (UL.Remove userId cost payments)) - else - Task.succeed U.NoOp - ) + |> flip Task.andThen (decodeOkResponse paymentsDecoder (\payments -> + Task.succeed <| U.UpdateLoggedView (UL.DeletePayment userId cost payments) + )) + DeleteMonthlyPayment id -> + Task.succeed <| U.UpdateLoggedView (UL.UpdateMonthly (UM.DeletePayment id)) UpdatePage page -> - decodeResponse - response + decodeOkResponse paymentsDecoder (\payments -> Task.succeed <| U.UpdateLoggedView (UL.UpdatePage page payments)) + response SignOut -> Task.succeed (U.GoSignInView) else decodeResponse - response messageDecoder (\error -> case communication of @@ -123,9 +117,16 @@ serverResult communication response = _ -> Task.succeed <| U.NoOp ) + response + +decodeOkResponse : Decoder a -> (a -> Task b U.Action) -> Http.Response -> Task b U.Action +decodeOkResponse decoder responseToAction response = + if response.status == 200 + then decodeResponse decoder responseToAction response + else Task.succeed U.NoOp -decodeResponse : Http.Response -> Decoder a -> (a -> Task b U.Action) -> Task b U.Action -decodeResponse response decoder responseToAction = +decodeResponse : Decoder a -> (a -> Task b U.Action) -> Http.Response -> Task b U.Action +decodeResponse decoder responseToAction response = case response.value of Http.Text text -> case decodeString decoder text of -- cgit v1.2.3