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
|
module LoggedIn.Update exposing
( update
)
import Dict
import String
import Task
import Http exposing (Error(..))
import Date exposing (Date)
import Platform.Cmd exposing (Cmd)
import Model exposing (Model)
import Model.Translations exposing (getMessage)
import Model.Payment exposing (Payment, Frequency(..), deletePayment)
import Server
import LoggedData
import LoggedIn.Msg as LoggedInMsg
import LoggedIn.Model as LoggedInModel
import LoggedIn.Home.Msg as HomeMsg
import LoggedIn.Home.Update as HomeUpdate
import LoggedIn.Income.Msg as IncomeMsg
import LoggedIn.Income.Update as IncomeUpdate
import LoggedIn.Home.AddPayment.Msg as AddPaymentMsg
import LoggedIn.Home.AddPayment.Update as AddPaymentUpdate
import Utils.Tuple as Tuple
import Utils.Cmd exposing ((:>))
update : Model -> LoggedInMsg.Msg -> LoggedInModel.Model -> (LoggedInModel.Model, Cmd LoggedInMsg.Msg)
update model action loggedIn =
let loggedData = LoggedData.build model loggedIn
in case action of
LoggedInMsg.NoOp ->
(loggedIn, Cmd.none)
LoggedInMsg.HomeMsg homeMsg ->
case HomeUpdate.update loggedData homeMsg loggedIn.home of
(home, effects) ->
( { loggedIn | home = home }
, Cmd.map LoggedInMsg.HomeMsg effects
)
LoggedInMsg.IncomeMsg incomeMsg ->
case IncomeUpdate.update loggedData incomeMsg loggedIn.income of
(income, cmd) ->
( { loggedIn | income = income }
, Cmd.map LoggedInMsg.IncomeMsg cmd
)
LoggedInMsg.AddPayment name cost frequency ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.UpdateAdd <| AddPaymentMsg.WaitingServer) loggedIn
:> \loggedIn ->
Server.addPayment name cost frequency
|> Task.perform
(\err ->
case err of
BadResponse 400 jsonErr ->
case AddPaymentUpdate.addPaymentError model.translations jsonErr of
Just addPaymentMsg -> (LoggedInMsg.HomeMsg <| HomeMsg.UpdateAdd addPaymentMsg)
Nothing -> LoggedInMsg.NoOp
_ ->
LoggedInMsg.NoOp
)
(\paymentId ->
case String.toInt cost of
Err _ ->
LoggedInMsg.HomeMsg <| HomeMsg.UpdateAdd (AddPaymentMsg.AddError Nothing (Just (getMessage "CostRequired" loggedData.translations)))
Ok costNumber ->
LoggedInMsg.ValidateAddPayment paymentId name costNumber frequency
)
|> \cmd -> (loggedIn, cmd)
LoggedInMsg.ValidateAddPayment paymentId name cost frequency ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.UpdateAdd <| AddPaymentMsg.Init frequency) loggedIn
:> (\loggedIn ->
case frequency of
Punctual ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.UpdatePage 1) loggedIn
Monthly ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.ShowMonthlyDetail) loggedIn
)
:> (\loggedIn ->
let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedIn.me frequency
in ( { loggedIn | payments = newPayment :: loggedIn.payments }
, Cmd.none
)
)
LoggedInMsg.DeletePayment paymentId ->
( loggedIn
, Server.deletePayment paymentId
|> Task.perform
(always LoggedInMsg.NoOp)
(always (LoggedInMsg.ValidateDeletePayment paymentId))
)
LoggedInMsg.ValidateDeletePayment paymentId ->
( { loggedIn | payments = deletePayment paymentId loggedIn.payments }
, Cmd.none
)
LoggedInMsg.AddIncome creation amount ->
( loggedIn
, Server.addIncome creation amount
|> Task.perform
(always LoggedInMsg.NoOp)
(\incomeId -> (LoggedInMsg.ValidateAddIncome incomeId creation amount))
)
LoggedInMsg.ValidateAddIncome incomeId creation amount ->
let newIncome = { userId = loggedIn.me, creation = (Date.toTime creation), amount = amount }
in ( { loggedIn | incomes = Dict.insert incomeId newIncome loggedIn.incomes }
, Cmd.none
)
LoggedInMsg.DeleteIncome incomeId ->
( loggedIn
, Server.deleteIncome incomeId
|> Task.perform
(always LoggedInMsg.NoOp)
(always <| LoggedInMsg.ValidateDeleteIncome incomeId)
)
LoggedInMsg.ValidateDeleteIncome incomeId ->
( { loggedIn | incomes = Dict.remove incomeId loggedIn.incomes }
, Cmd.none
)
|