aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Home/AddPayment/View.elm
blob: d97f3ca2e5b11e8418165d80b79a6abf15eda78d (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
module LoggedIn.Home.AddPayment.View exposing
  ( view
  )

import Result exposing (..)
import Json.Decode as Json

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)

import Msg exposing (Msg)

import LoggedIn.Msg as LoggedInMsg

import LoggedIn.Home.Msg as HomeMsg
import LoggedIn.Home.Model as HomeModel

import LoggedIn.Home.AddPayment.Msg as AddPaymentMsg
import LoggedIn.Home.AddPayment.Model as AddPaymentModel

import Model.Payment exposing (Frequency(..))
import Model.Translations exposing (getMessage)
import LoggedData exposing (LoggedData)

import View.Events exposing (onSubmitPrevDefault)
import View.Icon exposing (..)

import Utils.Maybe exposing (isJust)
import Utils.Either exposing (toMaybeError)

view : LoggedData -> HomeModel.Model -> Html Msg
view loggedData homeModel =
  Html.form
    [ let update =
            if homeModel.add.waitingServer
              then
                Msg.NoOp
              else
                Msg.UpdateLoggedIn <| LoggedInMsg.AddPayment homeModel.add.name homeModel.add.cost homeModel.add.frequency
      in  onSubmitPrevDefault update
    , class "addPayment"
    ]
    [ addPaymentName loggedData homeModel.add
    , addPaymentCost loggedData homeModel.add
    , paymentFrequency loggedData homeModel.add
    , button
        [ type' "submit"
        , classList
            [ ("add", True)
            , ("waitingServer", homeModel.add.waitingServer)
            ]
        ]
        [ text (getMessage "Add" loggedData.translations)
        , if homeModel.add.waitingServer then renderSpinIcon else text ""
        ]
    ]

addPaymentName : LoggedData -> AddPaymentModel.Model -> Html Msg
addPaymentName loggedData addPayment =
  div
    [ classList
        [ ("name", True)
        , ("error", isJust addPayment.nameError)
        ]
    ]
    [ input
        [ id "nameInput"
        , value addPayment.name
        , on "input" (targetValue |> (Json.map <| Msg.UpdateLoggedIn << LoggedInMsg.HomeMsg << HomeMsg.UpdateAdd << AddPaymentMsg.UpdateName))
        , maxlength 20
        ]
        []
    , label
        [ for "nameInput" ]
        [ renderIcon "shopping-cart" ]
    , case addPayment.nameError of
        Just error ->
          div [ class "errorMessage" ] [ text error ]
        Nothing ->
          text ""
    ]

addPaymentCost : LoggedData -> AddPaymentModel.Model -> Html Msg
addPaymentCost loggedData addPayment =
  div
    [ classList
        [ ("cost", True)
        , ("error", isJust addPayment.costError)
        ]
    ]
    [ input
        [ id "costInput"
        , value addPayment.cost
        , on "input" (targetValue |> (Json.map <| Msg.UpdateLoggedIn << LoggedInMsg.HomeMsg << HomeMsg.UpdateAdd << AddPaymentMsg.UpdateCost))
        , maxlength 7
        ]
        []
    , label
        [ for "costInput" ]
        [ text loggedData.conf.currency ]
    , case addPayment.costError of
        Just error ->
          div [ class "errorMessage" ] [ text error ]
        Nothing ->
          text ""
    ]

paymentFrequency : LoggedData -> AddPaymentModel.Model -> Html Msg
paymentFrequency loggedData addPayment =
  button
    [ type' "button"
    , class "frequency"
    , onClick (Msg.UpdateLoggedIn << LoggedInMsg.HomeMsg << HomeMsg.UpdateAdd <| AddPaymentMsg.ToggleFrequency)
    ]
    [ div
        [ classList
            [ ("punctual", True)
            , ("selected", addPayment.frequency == Punctual)
            ]
        ]
        [ text (getMessage "Punctual" loggedData.translations) ]
    , div
        [ classList
            [ ("monthly", True)
            , ("selected", addPayment.frequency == Monthly)
            ]
        ]
        [ text (getMessage "Monthly" loggedData.translations) ]
    ]