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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
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 Form
import Model exposing (Model)
import Model.Translations exposing (getMessage)
import Model.Payment as Payment exposing (Payment, Frequency(..))
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.Home.Model as HomeModel
import LoggedIn.Income.Msg as IncomeMsg
import LoggedIn.Income.Update as IncomeUpdate
import LoggedIn.Income.Model as IncomeModel
import Utils.Tuple as Tuple
import Utils.Cmd exposing ((:>))
update : Model -> LoggedInMsg.Msg -> LoggedInModel.Model -> (LoggedInModel.Model, Cmd LoggedInMsg.Msg)
update model msg loggedIn =
let loggedData = LoggedData.build model loggedIn
in case msg 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.CreatePayment name cost date frequency ->
( loggedIn
, Server.createPayment name cost date frequency
|> Task.perform
(always LoggedInMsg.NoOp)
(\paymentId -> LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency)
)
LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg (Form.Reset (HomeModel.searchInitial frequency))) loggedIn
:> update model (LoggedInMsg.HomeMsg <| HomeMsg.UpdatePage 1)
:> (\loggedIn ->
let newPayment = Payment paymentId name cost date loggedIn.me frequency
in ( { loggedIn | payments = newPayment :: loggedIn.payments }
, Cmd.none
)
)
LoggedInMsg.EditPayment paymentId name cost date frequency ->
( loggedIn
, Server.editPayment paymentId name cost date frequency
|> Task.perform
(always LoggedInMsg.NoOp)
(always <| LoggedInMsg.ValidateEditPayment paymentId name cost date frequency)
)
LoggedInMsg.ValidateEditPayment paymentId name cost date frequency ->
let updatedPayment = Payment paymentId name cost date loggedIn.me frequency
in ( { loggedIn | payments = Payment.edit updatedPayment loggedIn.payments }
, Cmd.none
)
LoggedInMsg.DeletePayment paymentId ->
( loggedIn
, Server.deletePayment paymentId
|> Task.perform
(always LoggedInMsg.NoOp)
(always (LoggedInMsg.ValidateDeletePayment paymentId))
)
LoggedInMsg.ValidateDeletePayment paymentId ->
let payments = Payment.delete paymentId loggedIn.payments
frequency =
case Form.getOutput loggedIn.home.search of
Just data -> data.frequency
Nothing -> Punctual
switchToPunctual =
( frequency == Monthly
&& List.isEmpty (Payment.monthly payments)
)
in if switchToPunctual
then
update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg (Form.Reset (HomeModel.searchInitial Punctual))) loggedIn
:> (\loggedIn ->
( { loggedIn | payments = payments }
, Cmd.none
)
)
else
( { loggedIn | payments = payments }
, Cmd.none
)
LoggedInMsg.CreateIncome amount date ->
( loggedIn
, Server.createIncome amount date
|> Task.perform
(always LoggedInMsg.NoOp)
(\incomeId -> (LoggedInMsg.ValidateCreateIncome incomeId amount date))
)
LoggedInMsg.ValidateCreateIncome incomeId amount date ->
let newIncome = { userId = loggedIn.me, amount = amount, time = Date.toTime date }
loggedInIncome = loggedIn.income
in ( { loggedIn
| incomes = Dict.insert incomeId newIncome loggedIn.incomes
, income = { loggedInIncome | addIncome = IncomeModel.initForm }
}
, 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
)
|