From 702d60cbcdf85216a1b18416f4480afb77384e8a Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 27 Mar 2016 20:20:10 +0200 Subject: Regroup loggedIn modules --- src/client/elm/LoggedIn/Action.elm | 21 ++++++ src/client/elm/LoggedIn/Model.elm | 37 ++++++++++ src/client/elm/LoggedIn/Update.elm | 136 +++++++++++++++++++++++++++++++++++++ src/client/elm/LoggedIn/View.elm | 34 ++++++++++ 4 files changed, 228 insertions(+) (limited to 'src/client/elm/LoggedIn') diff --git a/src/client/elm/LoggedIn/Action.elm b/src/client/elm/LoggedIn/Action.elm index e69de29..db69e2b 100644 --- a/src/client/elm/LoggedIn/Action.elm +++ b/src/client/elm/LoggedIn/Action.elm @@ -0,0 +1,21 @@ +module LoggedIn.Action + ( Action(..) + ) where + +import Model.Payment exposing (Payments, Payment, PaymentId, PaymentFrequency) +import Model.Action.MonthlyAction exposing (MonthlyAction) +import Model.Action.AccountAction exposing (AccountAction) +import Model.Action.AddPaymentAction exposing (AddPaymentAction) + +type Action = + NoOp + | UpdateAdd AddPaymentAction + | UpdatePayments Payments + | AddPayment String String PaymentFrequency + | ValidateAddPayment PaymentId String Int PaymentFrequency + | DeletePayment Payment PaymentFrequency + | ValidateDeletePayment Payment PaymentFrequency + | ToggleEdit PaymentId + | UpdatePage Int + | UpdateMonthly MonthlyAction + | UpdateAccount AccountAction diff --git a/src/client/elm/LoggedIn/Model.elm b/src/client/elm/LoggedIn/Model.elm index e69de29..5ab5e01 100644 --- a/src/client/elm/LoggedIn/Model.elm +++ b/src/client/elm/LoggedIn/Model.elm @@ -0,0 +1,37 @@ +module LoggedIn.Model + ( Model + , init + ) where + +import Model.User exposing (Users, UserId) +import Model.Payment exposing (Payments, PaymentFrequency(..)) +import Model.Payer exposing (Payers) +import Model.Init exposing (..) + +import Model.View.LoggedIn.AddPayment exposing (..) +import Model.View.LoggedIn.Edition exposing (..) +import Model.View.LoggedIn.Monthly exposing (..) +import Model.View.LoggedIn.Account exposing (..) + +type alias Model = + { users : Users + , add : AddPayment + , monthly : Monthly + , account : Account + , payments : Payments + , paymentsCount : Int + , paymentEdition : Maybe Edition + , currentPage : Int + } + +init : Init -> Model +init initData = + { users = initData.users + , add = initAddPayment Punctual + , monthly = initMonthly initData.monthlyPayments + , account = initAccount initData.me initData.incomes + , payments = initData.payments + , paymentsCount = initData.paymentsCount + , paymentEdition = Nothing + , currentPage = 1 + } diff --git a/src/client/elm/LoggedIn/Update.elm b/src/client/elm/LoggedIn/Update.elm index e69de29..aac046d 100644 --- a/src/client/elm/LoggedIn/Update.elm +++ b/src/client/elm/LoggedIn/Update.elm @@ -0,0 +1,136 @@ +module LoggedIn.Update + ( update + ) where + +import Date +import Dict +import Debug +import Task +import String + +import Effects exposing (Effects) +import Http exposing (Error(..)) + +import Server + +import LoggedIn.Action as LoggedInAction +import LoggedIn.Model as LoggedInModel + +import Model exposing (Model) +import Model.User exposing (UserId) +import Model.Payment exposing (..) +import Model.Action.AccountAction as Account +import Model.Action.MonthlyAction as Monthly +import Model.Action.AddPaymentAction as AddPayment +import Model.View.LoggedIn.AddPayment exposing (..) +import Model.Translations exposing (Translations, getMessage) + +import Update.LoggedIn.AddPayment exposing (updateAddPayment, addPaymentError) +import Update.LoggedIn.Monthly exposing (updateMonthly) +import Update.LoggedIn.Account exposing (updateAccount) + +update : Model -> LoggedInAction.Action -> LoggedInModel.Model -> (LoggedInModel.Model, Effects LoggedInAction.Action) +update model action loggedInView = + case action of + + LoggedInAction.NoOp -> (loggedInView, Effects.none) + + LoggedInAction.UpdateAdd addPaymentAction -> + ( { loggedInView | add = updateAddPayment addPaymentAction loggedInView.add } + , Effects.none + ) + + LoggedInAction.UpdatePayments payments -> + ( { loggedInView | payments = payments } + , Effects.none + ) + + LoggedInAction.AddPayment name cost frequency -> + ( { loggedInView | add = updateAddPayment AddPayment.WaitingServer loggedInView.add } + , Server.addPayment name cost frequency + |> Task.map (\paymentId -> + case String.toInt cost of + Err _ -> + LoggedInAction.UpdateAdd (AddPayment.AddError Nothing (Just (getMessage "CostRequired" model.translations))) + Ok costNumber -> + LoggedInAction.ValidateAddPayment paymentId name costNumber frequency + ) + |> flip Task.onError (\err -> + case err of + BadResponse 400 jsonErr -> + case addPaymentError model.translations jsonErr of + Just addPaymentAction -> Task.succeed (LoggedInAction.UpdateAdd addPaymentAction) + Nothing -> Task.succeed LoggedInAction.NoOp + _ -> + Task.succeed LoggedInAction.NoOp + ) + |> Effects.task + ) + + LoggedInAction.ValidateAddPayment paymentId name cost frequency -> + let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedInView.account.me + newAdd = initAddPayment frequency + in case frequency of + Punctual -> + ( { loggedInView + | currentPage = 1 + , add = newAdd + , account = loggedInView.account + , payments = newPayment :: loggedInView.payments + , paymentsCount = loggedInView.paymentsCount + 1 + } + , Effects.none + ) + Monthly -> + ( { loggedInView + | add = newAdd + , monthly = updateMonthly (Monthly.AddPayment newPayment) loggedInView.monthly + } + , Effects.none + ) + + LoggedInAction.ToggleEdit id -> + ( { loggedInView | paymentEdition = if loggedInView.paymentEdition == Just id then Nothing else Just id } + , Effects.none + ) + + LoggedInAction.DeletePayment payment frequency -> + ( loggedInView + , Server.deletePayment payment frequency + |> Task.map (always (LoggedInAction.ValidateDeletePayment payment frequency)) + |> flip Task.onError (always <| Task.succeed LoggedInAction.NoOp) + |> Effects.task + ) + + LoggedInAction.ValidateDeletePayment payment frequency -> + case frequency of + Monthly -> + ( { loggedInView + | monthly = updateMonthly (Monthly.DeletePayment payment) loggedInView.monthly + } + , Effects.none + ) + Punctual -> + ( { loggedInView + | account = loggedInView.account + , payments = deletePayment payment.id loggedInView.payments + , paymentsCount = loggedInView.paymentsCount - 1 + } + , Effects.none + ) + + LoggedInAction.UpdatePage page -> + ( { loggedInView | currentPage = page } + , Effects.none + ) + + LoggedInAction.UpdateMonthly monthlyAction -> + ( { loggedInView | monthly = updateMonthly monthlyAction loggedInView.monthly } + , Effects.none + ) + + LoggedInAction.UpdateAccount accountAction -> + let (newAccount, accountEffects) = updateAccount accountAction loggedInView.account + in ( { loggedInView | account = newAccount } + , Effects.map LoggedInAction.UpdateAccount accountEffects + ) diff --git a/src/client/elm/LoggedIn/View.elm b/src/client/elm/LoggedIn/View.elm index e69de29..8d4bdbb 100644 --- a/src/client/elm/LoggedIn/View.elm +++ b/src/client/elm/LoggedIn/View.elm @@ -0,0 +1,34 @@ +module LoggedIn.View + ( view + ) where + +import Signal exposing (Address) + +import Html exposing (..) +import Html.Attributes exposing (..) + +import LoggedIn.Model as LoggedInModel + +import Model exposing (Model) +import Model.Payment exposing (Payments) +import Model.Action exposing (Action) + +import View.LoggedIn.AddPayment exposing (addPayment) +import View.LoggedIn.Monthly exposing (monthlyPayments) +import View.LoggedIn.Account exposing (account) +import View.LoggedIn.Table exposing (paymentsTable) +import View.LoggedIn.Paging exposing (paymentsPaging) + +view : Address Action -> Model -> LoggedInModel.Model -> Html +view address model loggedInModel = + div + [ class "loggedIn" ] + [ addPayment address model loggedInModel + , div + [ class "expandables" ] + [ account address model loggedInModel + , monthlyPayments address model loggedInModel + ] + , paymentsTable address model loggedInModel + , paymentsPaging address loggedInModel + ] -- cgit v1.2.3