aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Update.elm
blob: aac046d47803f1463a78882aaae2a26a09991952 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
          )