aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Update.elm
blob: fd141c8fae315a05f3c7c4c83226896569b72f4e (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
module LoggedIn.Update
  ( update
  ) where

import Dict
import String
import Task

import Effects exposing (Effects)
import Http exposing (Error(..))
import Date

import Model exposing (Model)
import Model.Translations exposing (getMessage)
import Model.Payment exposing (Payment, Frequency(..), deletePayment)

import Server
import LoggedData

import LoggedIn.Action as LoggedInAction
import LoggedIn.Model as LoggedInModel

import LoggedIn.Home.Action as HomeAction
import LoggedIn.Home.Update as HomeUpdate

import LoggedIn.Home.Account.Action as AccountAction

import LoggedIn.Home.AddPayment.Action as AddPaymentAction
import LoggedIn.Home.AddPayment.Update as AddPaymentUpdate

import Utils.Tuple as Tuple
import Utils.Effects as Effects

update : Model -> LoggedInAction.Action -> LoggedInModel.Model -> (LoggedInModel.Model, Effects LoggedInAction.Action)
update model action loggedIn =
  let loggedData = LoggedData.build model loggedIn
  in  case action of

        LoggedInAction.NoOp ->
          (loggedIn, Effects.none)

        LoggedInAction.HomeAction homeAction ->
          case HomeUpdate.update loggedData homeAction loggedIn.home of
            (home, effects) ->
              ( { loggedIn | home = home }
              , Effects.map LoggedInAction.HomeAction effects
              )

        LoggedInAction.AddPayment name cost frequency ->
          update model (LoggedInAction.HomeAction <| HomeAction.UpdateAdd <| AddPaymentAction.WaitingServer) loggedIn
            |> Tuple.mapSnd (\effect ->
                 Server.addPayment name cost frequency
                   |> Task.map (\paymentId ->
                        case String.toInt cost of
                          Err _ ->
                            LoggedInAction.HomeAction <| HomeAction.UpdateAdd (AddPaymentAction.AddError Nothing (Just (getMessage "CostRequired" loggedData.translations)))
                          Ok costNumber ->
                            LoggedInAction.ValidateAddPayment paymentId name costNumber frequency
                      )
                   |> flip Task.onError (\err ->
                       case err of
                         BadResponse 400 jsonErr ->
                           case AddPaymentUpdate.addPaymentError model.translations jsonErr of
                             Just addPaymentAction -> Task.succeed (LoggedInAction.HomeAction <| HomeAction.UpdateAdd addPaymentAction)
                             Nothing -> Task.succeed LoggedInAction.NoOp
                         _ ->
                           Task.succeed LoggedInAction.NoOp
                      )
                   |> Effects.task
                   |> \effect2 -> [effect, effect2]
                   |> Effects.batch
               )

        LoggedInAction.ValidateAddPayment paymentId name cost frequency ->
          update model (LoggedInAction.HomeAction <| HomeAction.UpdateAdd <| AddPaymentAction.Init frequency) loggedIn
            |> flip Effects.andThen (\loggedIn ->
                 case frequency of
                   Punctual ->
                     update model (LoggedInAction.HomeAction <| HomeAction.UpdatePage 1) loggedIn
                   Monthly ->
                     update model (LoggedInAction.HomeAction <| HomeAction.ShowMonthlyDetail) loggedIn
               )
            |> Tuple.mapFst (\loggedIn ->
                 let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedIn.me frequency
                 in  { loggedIn | payments = newPayment :: loggedIn.payments }
               )

        LoggedInAction.DeletePayment payment frequency ->
          ( loggedIn
          , 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 ->
          ( { loggedIn | payments = deletePayment payment.id loggedIn.payments }
          , Effects.none
          )

        LoggedInAction.UpdateIncome amount ->
          ( loggedIn
          , Server.setIncome amount
              |> Task.map (\incomeId -> (LoggedInAction.ValidateUpdateIncome incomeId amount))
              |> flip Task.onError (always <| Task.succeed LoggedInAction.NoOp)
              |> Effects.task
          )

        LoggedInAction.ValidateUpdateIncome incomeId amount ->
          update model (LoggedInAction.HomeAction <| HomeAction.UpdateAccount <| AccountAction.ToggleIncomeEdition) loggedIn
            |> Tuple.mapFst (\loggedIn ->
                 let newIncome = { userId = loggedIn.me, creation = model.currentTime, amount = amount }
                 in  { loggedIn | incomes = Dict.insert incomeId newIncome loggedIn.incomes }
               )