aboutsummaryrefslogtreecommitdiff
path: root/src/client/Update/LoggedIn.elm
blob: 07f34260083327360dd2c2de5121254874806479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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.View.LoggedInView exposing (..)
import Model.View.LoggedIn.Add exposing (..)

import Update.LoggedIn.Add exposing (..)
import Update.LoggedIn.Monthly as UM
import Update.LoggedIn.Account as UA

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
  | UpdateAccount UA.AccountAction

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
      , account <- UA.updateAccount (UA.UpdatePayer userId cost) loggedInView.account
      , 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
      , account <- UA.updateAccount (UA.UpdatePayer userId -cost) loggedInView.account
      , paymentsCount <- loggedInView.paymentsCount - 1
      }
    UpdatePage page payments ->
      { loggedInView
      | currentPage <- page
      , payments <- payments
      }
    UpdateMonthly monthlyAction ->
      { loggedInView | monthly <- UM.updateMonthly monthlyAction loggedInView.monthly }
    UpdateAccount accountAction ->
      { loggedInView | account <- UA.updateAccount accountAction loggedInView.account }