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

import Maybe
import Dict
import Task

import Effects exposing (Effects)

import Server

import LoggedIn.Account.Action as AccountAction
import LoggedIn.Account.Model as AccountModel

import Utils.Maybe exposing (isJust)

update : AccountAction.Action -> AccountModel.Model -> (AccountModel.Model, Effects AccountAction.Action)
update action account =
  case action of

    AccountAction.NoOp ->
      (account, Effects.none)

    AccountAction.ToggleDetail ->
      ( { account | visibleDetail = not account.visibleDetail }
      , Effects.none
      )

    AccountAction.ToggleIncomeEdition ->
      ( { account | incomeEdition =
          if isJust account.incomeEdition
            then Nothing
            else Just (AccountModel.initIncomeEdition (Maybe.withDefault 0 (AccountModel.getCurrentIncome account)))
      }
      , Effects.none
      )

    AccountAction.UpdateIncomeEdition income ->
      case account.incomeEdition of
        Just incomeEdition ->
          ( { account | incomeEdition = Just { incomeEdition | income = income } }
          , Effects.none
          )
        Nothing ->
          ( account
          , Effects.none
          )

    AccountAction.UpdateEditionError error ->
      case account.incomeEdition of
        Just incomeEdition ->
          ( { account | incomeEdition = Just { incomeEdition | error = Just error } }
          , Effects.none
          )
        Nothing ->
          ( account
          , Effects.none
          )

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

    AccountAction.ValidateUpdateIncome incomeId currentTime amount ->
      ( { account
        | incomes = Dict.insert incomeId { userId = account.me, creation = currentTime, amount = amount } account.incomes
        , incomeEdition = Nothing
        }
      , Effects.none
      )