aboutsummaryrefslogtreecommitdiff
path: root/src/client/Update.elm
blob: 7006d5af1b2bb6498c4e922542529d42a5626341 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
module Update exposing
  ( update
  )

import Task
import Platform.Cmd exposing (Cmd)
import Navigation exposing (Location)

import Page exposing (Page)

import Server

import Msg exposing (..)

import Model exposing (Model)
import Model.Translations exposing (getMessage)
import Model.View as V

import LoggedIn.Model as LoggedInModel
import LoggedIn.Msg as LoggedInMsg
import LoggedIn.Update as LoggedInUpdate

import SignIn.Model as SignInModel
import SignIn.Msg as SignInMsg
import SignIn.Update as SignInUpdate

import Dialog
import Dialog.Update as DialogUpdate

import Tooltip

import Utils.Http exposing (errorKey)
import Utils.Cmd exposing ((:>))

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of

    NoOp ->
      (model, Cmd.none)

    UpdatePage page ->
      ({ model | page = page }, Cmd.none)

    SignIn email ->
      ( applySignIn model (SignInMsg.WaitingServer)
      , Server.signIn email (\result -> case result of
          Ok _ -> UpdateSignIn SignInMsg.ValidLogin
          Err error -> UpdateSignIn (SignInMsg.ErrorLogin (errorKey error))
        )
      )

    GoLoggedInView init ->
      ( { model | view = V.LoggedInView (LoggedInModel.init init) }
      , Cmd.none
      )

    UpdateTime time ->
      ({ model | currentTime = time }, Cmd.none)

    GoSignInView ->
      ({ model | view = V.SignInView (SignInModel.init Nothing) }, Cmd.none)

    UpdateSignIn signInMsg ->
      (applySignIn model signInMsg, Cmd.none)

    UpdateLoggedIn loggedInMsg ->
      applyLoggedIn model loggedInMsg

    SignOut ->
      ( model
      , Server.signOut (\result -> case result of
          Ok _ -> GoSignInView
          Err _ -> Error "SignOutError"
        )
      )

    Error error ->
      ({ model | errors = model.errors ++ [ error ] }, Cmd.none)

    Dialog dialogMsg ->
      Dialog.update DialogUpdate.update dialogMsg model.dialog.model model.dialog
        |> Tuple.mapFirst (\dialog -> { model | dialog = dialog })
        :> update (Tooltip Tooltip.HideMessage)

    Tooltip tooltipMsg ->
      let (newTooltip, command) = Tooltip.update tooltipMsg model.tooltip
      in  ( { model | tooltip = newTooltip }
          , Cmd.map Tooltip command
          )

    CreatePayment name cost date category frequency ->
      ( model
      , Server.createPayment name cost date category frequency (\result -> case result of
          Ok paymentId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreatePayment paymentId name cost date category frequency
          Err _ -> Error "CreatePaymentError"
        )
      )

    EditPayment paymentId name cost date category frequency ->
      ( model
      , Server.editPayment paymentId name cost date category frequency (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditPayment paymentId name cost date category frequency
          Err _ -> Error "EditPaymentError"
        )
      )

    DeletePayment paymentId ->
      ( model
      , Server.deletePayment paymentId (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeletePayment paymentId
          Err _ -> Error "DeletePaymentError"
        )
      )

    CreateIncome amount date ->
      ( model
      , Server.createIncome amount date (\result -> case result of
          Ok incomeId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateIncome incomeId amount date
          Err _ -> Error "CreateIncomeError"
        )
      )

    EditIncome incomeId amount date ->
      ( model
      , Server.editIncome incomeId amount date (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditIncome incomeId amount date
          Err _ -> Error "EditIncomeError"
        )
      )

    DeleteIncome incomeId ->
      ( model
      , Server.deleteIncome incomeId (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeleteIncome incomeId
          Err _ -> Error "DeleteIncomeError"
        )
      )

    CreateCategory name color ->
      ( model
      , Server.createCategory name color (\result -> case result of
          Ok categoryId -> UpdateLoggedIn <| LoggedInMsg.ValidateCreateCategory categoryId name color
          Err _ -> Error "CreateCategoryError"
        )
      )

    EditCategory categoryId name color ->
      ( model
      , Server.editCategory categoryId name color (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateEditCategory categoryId name color
          Err _ -> Error "EditCategoryError"
        )
      )

    DeleteCategory categoryId ->
      ( model
      , Server.deleteCategory categoryId (\result -> case result of
          Ok _ -> UpdateLoggedIn <| LoggedInMsg.ValidateDeleteCategory categoryId
          Err _ -> Error "DeleteCategoryError"
        )
      )


applySignIn : Model -> SignInMsg.Msg -> Model
applySignIn model signInMsg =
  case model.view of
    V.SignInView signInView ->
      { model | view = V.SignInView (SignInUpdate.update model.translations signInMsg signInView) }
    _ ->
      model

applyLoggedIn : Model -> LoggedInMsg.Msg -> (Model, Cmd Msg)
applyLoggedIn model loggedInMsg =
  case model.view of
    V.LoggedInView loggedInView ->
      let (view, cmd) = LoggedInUpdate.update model loggedInMsg loggedInView
      in  ( { model | view = V.LoggedInView view }
          , Cmd.map UpdateLoggedIn cmd
          )
    _ ->
      (model, Cmd.none)