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
|
module LoggedIn.Update exposing
( update
)
import Date exposing (Date)
import Dict
import Form
import Http exposing (Error(..))
import Platform.Cmd exposing (Cmd)
import String
import Task
import LoggedData
import LoggedIn.Home.Model as Home
import LoggedIn.Home.Msg as Home
import LoggedIn.Home.Update as Home
import LoggedIn.Model as LoggedInModel
import LoggedIn.Msg as LoggedIn
import LoggedIn.Stat.Model as Stat
import LoggedIn.Stat.Msg as Stat
import LoggedIn.Stat.Update as Stat
import Model exposing (Model)
import Model.Category exposing (Category)
import Model.Frequency exposing (Frequency(..))
import Model.Income as Income exposing (Income)
import Model.Payment as Payment exposing (Payment)
import Model.PaymentCategory as PaymentCategory
import Server
import Utils.Cmd exposing ((:>))
update : Model -> LoggedIn.Msg -> LoggedInModel.Model -> (LoggedInModel.Model, Cmd LoggedIn.Msg)
update model msg loggedIn =
let loggedData = LoggedData.build model.currentTime model.translations model.conf loggedIn
in case msg of
LoggedIn.NoOp ->
( loggedIn
, Cmd.none
)
LoggedIn.HomeMsg homeMsg ->
case Home.update loggedData homeMsg loggedIn.home of
(home, effects) ->
( { loggedIn | home = home }
, Cmd.map LoggedIn.HomeMsg effects
)
LoggedIn.StatMsg statMsg ->
case Stat.update loggedData statMsg loggedIn.stat of
(stat, effects) ->
( { loggedIn | stat = stat }
, Cmd.map LoggedIn.StatMsg effects
)
LoggedIn.ValidateCreatePayment paymentId name cost date category frequency ->
update model (LoggedIn.HomeMsg <| Home.SearchMsg (Form.Reset (Home.searchInitial frequency))) loggedIn
:> update model (LoggedIn.HomeMsg <| Home.UpdatePage 1)
:> (\loggedIn ->
let newPayment = Payment paymentId name cost date loggedIn.me frequency
in ( { loggedIn
| payments = newPayment :: loggedIn.payments
, paymentCategories = PaymentCategory.save name category loggedIn.paymentCategories
}
, Cmd.none
)
)
LoggedIn.ValidateEditPayment paymentId name cost date category frequency ->
let updatedPayment = Payment paymentId name cost date loggedIn.me frequency
mbOldPayment = Payment.find paymentId loggedIn.payments
in ( { loggedIn
| payments = Payment.edit updatedPayment loggedIn.payments
, paymentCategories =
case mbOldPayment of
Just oldPayment ->
PaymentCategory.save name category loggedIn.paymentCategories
Nothing ->
loggedData.paymentCategories
}
, Cmd.none
)
LoggedIn.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 (LoggedIn.HomeMsg <| Home.SearchMsg (Form.Reset (Home.searchInitial Punctual))) loggedIn
:> (\loggedIn ->
( { loggedIn | payments = payments }
, Cmd.none
)
)
else
( { loggedIn | payments = payments }
, Cmd.none
)
LoggedIn.ValidateCreateIncome incomeId amount date ->
let newIncome = { userId = loggedIn.me, amount = amount, time = Date.toTime date }
in ( { loggedIn | incomes = Dict.insert incomeId newIncome loggedIn.incomes }
, Cmd.none
)
LoggedIn.ValidateEditIncome incomeId amount date ->
let updateIncome _ = Just <| Income loggedIn.me (Date.toTime date) amount
in ( { loggedIn | incomes = Dict.update incomeId updateIncome loggedIn.incomes }
, Cmd.none
)
LoggedIn.ValidateDeleteIncome incomeId ->
( { loggedIn | incomes = Dict.remove incomeId loggedIn.incomes }
, Cmd.none
)
LoggedIn.ValidateCreateCategory categoryId name color ->
let newCategory = { name = name, color = color }
in ( { loggedIn | categories = Dict.insert categoryId newCategory loggedIn.categories }
, Cmd.none
)
LoggedIn.ValidateEditCategory categoryId name color ->
let updateCategory _ = Just <| Category name color
in ( { loggedIn | categories = Dict.update categoryId updateCategory loggedIn.categories } , Cmd.none)
LoggedIn.ValidateDeleteCategory categoryId ->
( { loggedIn | categories = Dict.remove categoryId loggedIn.categories }
, Cmd.none
)
|