module Update.LoggedIn ( LoggedAction(..) , updateLoggedIn ) where import Date import Dict import Model exposing (Model) import Model.User exposing (UserId) import Model.Payment exposing (..) import Model.Payers exposing (..) import Model.View.LoggedInView exposing (..) import Model.View.LoggedIn.Add exposing (..) import Update.LoggedIn.Add exposing (..) import Update.LoggedIn.Monthly as UM type LoggedAction = UpdateAdd AddPaymentAction | UpdatePayments Payments | AddPayment UserId String Int Payments | AddMonthlyPayment PaymentId String Int | ToggleEdit PaymentId | DeletePayment UserId Int Payments | UpdatePage Int Payments | UpdateMonthly UM.MonthlyAction updateLoggedIn : Model -> LoggedAction -> LoggedInView -> LoggedInView updateLoggedIn model action loggedInView = case action of UpdateAdd addPaymentAction -> { loggedInView | add <- updateAddPayment addPaymentAction loggedInView.add } UpdatePayments payments -> { loggedInView | payments <- payments } AddPayment userId name cost payments -> { loggedInView | payments <- payments , currentPage <- 1 , add <- initAddPayment Punctual , payers <- updatePayers loggedInView.payers userId cost , paymentsCount <- loggedInView.paymentsCount + 1 } AddMonthlyPayment id name cost -> { loggedInView | add <- initAddPayment Monthly , monthly <- let payment = Payment id (Date.fromTime model.currentTime) name cost loggedInView.me in UM.updateMonthly (UM.AddPayment payment) loggedInView.monthly } ToggleEdit id -> { loggedInView | paymentEdition <- if loggedInView.paymentEdition == Just id then Nothing else Just id } DeletePayment userId cost payments -> { loggedInView | payments <- payments , payers <- updatePayers loggedInView.payers userId -cost , paymentsCount <- loggedInView.paymentsCount - 1 } UpdatePage page payments -> { loggedInView | currentPage <- page , payments <- payments } UpdateMonthly monthlyAction -> { loggedInView | monthly <- UM.updateMonthly monthlyAction loggedInView.monthly }