aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/elm')
-rw-r--r--src/client/elm/Model/Payment.elm4
-rw-r--r--src/client/elm/ServerCommunication.elm7
-rw-r--r--src/client/elm/Update/LoggedIn.elm19
-rw-r--r--src/client/elm/View/LoggedIn/Add.elm2
4 files changed, 20 insertions, 12 deletions
diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm
index c4a8963..cabdd43 100644
--- a/src/client/elm/Model/Payment.elm
+++ b/src/client/elm/Model/Payment.elm
@@ -5,6 +5,7 @@ module Model.Payment
, PaymentId
, paymentsDecoder
, paymentIdDecoder
+ , deletePayment
) where
import Date exposing (..)
@@ -42,3 +43,6 @@ paymentDecoder =
paymentIdDecoder : Json.Decoder PaymentId
paymentIdDecoder = Json.int
+
+deletePayment : PaymentId -> Payments -> Payments
+deletePayment paymentId = List.filter (((/=) paymentId) << .id)
diff --git a/src/client/elm/ServerCommunication.elm b/src/client/elm/ServerCommunication.elm
index 390bcfd..74b45e8 100644
--- a/src/client/elm/ServerCommunication.elm
+++ b/src/client/elm/ServerCommunication.elm
@@ -31,7 +31,7 @@ import InitViewAction exposing (initViewAction)
type Communication =
NoCommunication
| SignIn String
- | AddPayment UserId String Int
+ | AddPayment String Int
| AddMonthlyPayment String Int
| SetIncome Time Int
| DeletePayment Payment Int
@@ -56,9 +56,10 @@ sendRequest communication =
|> flip Task.andThen (always <| Task.fail err)
)
- AddPayment userId name cost ->
+ AddPayment name cost ->
post (addPaymentURL name cost Punctual)
- |> Task.map (always (U.UpdateLoggedIn (UL.AddPayment userId name cost)))
+ |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder)
+ |> Task.map (\paymentId -> (U.UpdateLoggedIn (UL.AddPayment paymentId name cost)))
AddMonthlyPayment name cost ->
post (addPaymentURL name cost Monthly)
diff --git a/src/client/elm/Update/LoggedIn.elm b/src/client/elm/Update/LoggedIn.elm
index 2dc65c3..38901b2 100644
--- a/src/client/elm/Update/LoggedIn.elm
+++ b/src/client/elm/Update/LoggedIn.elm
@@ -19,7 +19,7 @@ import Update.LoggedIn.Account as UA
type LoggedAction =
UpdateAdd AddPaymentAction
| UpdatePayments Payments
- | AddPayment UserId String Int
+ | AddPayment PaymentId String Int
| AddMonthlyPayment PaymentId String Int
| ToggleEdit PaymentId
| DeletePayment Payment
@@ -34,13 +34,15 @@ updateLoggedIn model action loggedInView =
{ loggedInView | add = updateAddPayment addPaymentAction loggedInView.add }
UpdatePayments payments ->
{ loggedInView | payments = payments }
- AddPayment userId name cost ->
- { loggedInView
- | currentPage = 1
- , add = initAddPayment Punctual
- , account = UA.updateAccount (UA.UpdatePayer userId model.currentTime cost) loggedInView.account
- , paymentsCount = loggedInView.paymentsCount + 1
- }
+ AddPayment paymentId name cost ->
+ let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedInView.account.me
+ in { loggedInView
+ | currentPage = 1
+ , add = initAddPayment Punctual
+ , account = UA.updateAccount (UA.UpdatePayer loggedInView.account.me model.currentTime cost) loggedInView.account
+ , payments = newPayment :: loggedInView.payments
+ , paymentsCount = loggedInView.paymentsCount + 1
+ }
AddMonthlyPayment id name cost ->
{ loggedInView
| add = initAddPayment Monthly
@@ -53,6 +55,7 @@ updateLoggedIn model action loggedInView =
DeletePayment payment ->
{ loggedInView
| account = UA.updateAccount (UA.UpdatePayer payment.userId (Date.toTime payment.creation) -payment.cost) loggedInView.account
+ , payments = deletePayment payment.id loggedInView.payments
, paymentsCount = loggedInView.paymentsCount - 1
}
UpdatePage page ->
diff --git a/src/client/elm/View/LoggedIn/Add.elm b/src/client/elm/View/LoggedIn/Add.elm
index 572bdf6..2f580f9 100644
--- a/src/client/elm/View/LoggedIn/Add.elm
+++ b/src/client/elm/View/LoggedIn/Add.elm
@@ -32,7 +32,7 @@ addPayment model loggedInView =
(Ok name, Ok cost) ->
let action =
case loggedInView.add.frequency of
- Punctual -> SC.AddPayment loggedInView.account.me name cost
+ Punctual -> SC.AddPayment name cost
Monthly -> SC.AddMonthlyPayment name cost
in onSubmitPrevDefault serverCommunications.address action
(resName, resCost) ->