aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Update/LoggedIn/AddPayment.elm
blob: 4c9c4848eaa3aa212e929095f81e4412c82ffaa2 (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
module Update.LoggedIn.AddPayment
  ( updateAddPayment
  , addPaymentError
  ) where

import Maybe
import Json.Decode as Json exposing ((:=))

import Model.Action.AddPaymentAction exposing (..)
import Model.View.LoggedIn.AddPayment exposing (..)
import Model.Translations exposing (Translations, getMessage)
import Model.Payment exposing (PaymentFrequency(..))

updateAddPayment : AddPaymentAction -> AddPayment -> AddPayment
updateAddPayment action addPayment =
  case action of

    NoOp ->
      addPayment

    UpdateName name ->
      { addPayment | name = name }

    UpdateCost cost ->
      { addPayment | cost = cost }

    AddError nameError costError ->
      { addPayment
      | nameError = nameError
      , costError = costError
      , waitingServer = False
      }

    ToggleFrequency ->
      { addPayment
      | frequency = if addPayment.frequency == Punctual then Monthly else Punctual
      }

    WaitingServer ->
      { addPayment | waitingServer = True }

addPaymentError : Translations -> String -> Maybe AddPaymentAction
addPaymentError translations jsonErr =
  let decoder =
        Json.object2 (,)
          (Json.maybe <| "name" := Json.string)
          (Json.maybe <| "cost" := Json.string)
  in  case Json.decodeString decoder jsonErr of
        Err _ ->
          Nothing
        Ok (mbNameKey, mbCostKey) ->
          Just <| AddError
            (Maybe.map (flip getMessage translations) mbNameKey)
            (Maybe.map (flip getMessage translations) mbCostKey)