aboutsummaryrefslogtreecommitdiff
path: root/src/client/Update/Payment.elm
blob: f063b4c5b719de3c1dc3e222429033050051fae0 (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
module Update.Payment
  ( PaymentAction(..)
  , updatePayment
  ) 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.LoggedView exposing (..)
import Model.View.Payment.Add exposing (..)

import Update.Payment.Add exposing (..)

type PaymentAction =
  UpdateAdd AddPaymentAction
  | UpdatePayments Payments
  | AddPayment UserId Int Payments
  | ToggleEdit PaymentId
  | Remove UserId Int Payments
  | UpdatePage Int Payments

updatePayment : Model -> PaymentAction -> LoggedView -> LoggedView
updatePayment model action loggedView =
  case action of
    UpdateAdd addPaymentAction ->
      { loggedView | add <- updateAddPayment addPaymentAction loggedView.add }
    UpdatePayments payments ->
      { loggedView | payments <- payments }
    AddPayment userId cost payments ->
      { loggedView
      | payments <- payments
      , currentPage <- 1
      , add <- initAddPayment
      , payers <- updatePayers loggedView.payers userId cost
      , paymentsCount <- loggedView.paymentsCount + 1
      }
    ToggleEdit id ->
      { loggedView | edition <- if loggedView.edition == Just id then Nothing else Just id }
    Remove userId cost payments ->
      { loggedView
      | payments <- payments
      , payers <- updatePayers loggedView.payers userId -cost
      , paymentsCount <- loggedView.paymentsCount - 1
      }
    UpdatePage page payments ->
      { loggedView
      | currentPage <- page
      , payments <- payments
      }