aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/View/LoggedIn/AddPayment.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/elm/View/LoggedIn/AddPayment.elm')
-rw-r--r--src/client/elm/View/LoggedIn/AddPayment.elm123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/client/elm/View/LoggedIn/AddPayment.elm b/src/client/elm/View/LoggedIn/AddPayment.elm
new file mode 100644
index 0000000..0fbe28e
--- /dev/null
+++ b/src/client/elm/View/LoggedIn/AddPayment.elm
@@ -0,0 +1,123 @@
+module View.LoggedIn.AddPayment
+ ( addPayment
+ ) where
+
+import Reads exposing (readInt)
+import Result exposing (..)
+import Signal exposing (Address)
+
+import Html as H exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+
+import Model exposing (Model)
+import Model.Translations exposing (getMessage)
+import Model.Action exposing (..)
+import Model.Action.LoggedInAction exposing (..)
+import Model.Action.AddPaymentAction exposing (..)
+import Model.Communication as Communication
+
+import Model.View.LoggedIn.AddPayment exposing (..)
+import Model.View.LoggedInView exposing (LoggedInView)
+
+import View.Events exposing (onSubmitPrevDefault)
+import View.Icon exposing (renderIcon)
+
+import Utils.Maybe exposing (isJust)
+import Utils.Either exposing (toMaybeError)
+
+addPayment : Address Action -> Model -> LoggedInView -> Html
+addPayment address model loggedInView =
+ H.form
+ [ case (validateName loggedInView.add.name model.translations, validateCost loggedInView.add.cost model.translations) of
+ (Ok name, Ok cost) ->
+ let action =
+ case loggedInView.add.frequency of
+ Punctual -> Communication.AddPayment name cost
+ Monthly -> Communication.AddMonthlyPayment name cost
+ in onSubmitPrevDefault address (ServerCommunication action)
+ (resName, resCost) ->
+ onSubmitPrevDefault address (UpdateLoggedIn <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost))
+ , class "addPayment"
+ ]
+ [ addPaymentName address loggedInView.add
+ , addPaymentCost address model loggedInView.add
+ , paymentFrequency address model loggedInView.add
+ , button
+ [ type' "submit"
+ , class "add" ]
+ [ text (getMessage "Add" model.translations)]
+ ]
+
+addPaymentName : Address Action -> AddPayment -> Html
+addPaymentName address addPayment =
+ div
+ [ classList
+ [ ("name", True)
+ , ("error", isJust addPayment.nameError)
+ ]
+ ]
+ [ input
+ [ id "nameInput"
+ , value addPayment.name
+ , on "input" targetValue (Signal.message address << UpdateLoggedIn << UpdateAdd << UpdateName)
+ , maxlength 20
+ ]
+ []
+ , label
+ [ for "nameInput" ]
+ [ renderIcon "shopping-cart" ]
+ , case addPayment.nameError of
+ Just error ->
+ div [ class "errorMessage" ] [ text error ]
+ Nothing ->
+ text ""
+ ]
+
+addPaymentCost : Address Action -> Model -> AddPayment -> Html
+addPaymentCost address model addPayment =
+ div
+ [ classList
+ [ ("cost", True)
+ , ("error", isJust addPayment.costError)
+ ]
+ ]
+ [ input
+ [ id "costInput"
+ , value addPayment.cost
+ , on "input" targetValue (Signal.message address << UpdateLoggedIn << UpdateAdd << UpdateCost)
+ , maxlength 7
+ ]
+ []
+ , label
+ [ for "costInput" ]
+ [ text model.config.currency ]
+ , case addPayment.costError of
+ Just error ->
+ div [ class "errorMessage" ] [ text error ]
+ Nothing ->
+ text ""
+ ]
+
+paymentFrequency : Address Action -> Model -> AddPayment -> Html
+paymentFrequency address model addPayment =
+ button
+ [ type' "button"
+ , class "frequency"
+ , onClick address (UpdateLoggedIn << UpdateAdd <| ToggleFrequency)
+ ]
+ [ div
+ [ classList
+ [ ("punctual", True)
+ , ("selected", addPayment.frequency == Punctual)
+ ]
+ ]
+ [ text (getMessage "Punctual" model.translations) ]
+ , div
+ [ classList
+ [ ("monthly", True)
+ , ("selected", addPayment.frequency == Monthly)
+ ]
+ ]
+ [ text (getMessage "Monthly" model.translations) ]
+ ]