aboutsummaryrefslogtreecommitdiff
path: root/src/client/Update/Payment.elm
blob: 798cdb4ed46c880192116d9d60718c58fdd2215d (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
module Update.Payment
  ( PaymentAction(..)
  , updatePayment
  ) where

import Date
import Dict

import Model exposing (Model)
import Model.Payment exposing (..)
import Model.Payers exposing (..)
import Model.View.PaymentView exposing (..)
import Model.View.Payment.Add exposing (..)

import Update.Payment.Add exposing (..)

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

updatePayment : Model -> PaymentAction -> PaymentView -> PaymentView
updatePayment model action paymentView =
  case action of
    UpdateAdd addPaymentAction ->
      { paymentView | add <- updateAddPayment addPaymentAction paymentView.add }
    UpdatePayments payments ->
      { paymentView | payments <- payments }
    AddPayment id name cost ->
      let payment =
            { creation = Date.fromTime model.currentTime
            , name = name
            , cost = cost
            , userName = paymentView.userName
            }
      in  { paymentView
          | payments <- addPayment paymentView.payments (id, payment)
          , add <- initAddPayment
          , payers <- updatePayers paymentView.payers payment.userName payment.cost
          }
    ToggleEdit id ->
      { paymentView | edition <- if paymentView.edition == Just id then Nothing else Just id }
    Remove id ->
      case Dict.get id paymentView.payments of
        Just payment ->
          { paymentView
          | payments <- removePayment paymentView.payments id
          , payers <- updatePayers paymentView.payers payment.userName -payment.cost
          }
        Nothing ->
          paymentView
    UpdatePage page payments ->
      { paymentView
      | currentPage <- page
      , payments <- payments
      }