aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Dialog/AddPayment
diff options
context:
space:
mode:
authorJoris2016-08-08 20:58:17 +0200
committerJoris2016-08-08 20:58:17 +0200
commit8816cf758119a6a2073e561c8df297a833630986 (patch)
tree20e63f3c0de15945b818a6d7a78359f9134b5e82 /src/client/elm/Dialog/AddPayment
parentb54d8e45fc8784d8fa6eaa03f58536b7a19cf70b (diff)
Show incomes in a table and update like payments are updated
Diffstat (limited to 'src/client/elm/Dialog/AddPayment')
-rw-r--r--src/client/elm/Dialog/AddPayment/Model.elm61
-rw-r--r--src/client/elm/Dialog/AddPayment/View.elm82
2 files changed, 143 insertions, 0 deletions
diff --git a/src/client/elm/Dialog/AddPayment/Model.elm b/src/client/elm/Dialog/AddPayment/Model.elm
new file mode 100644
index 0000000..19326f0
--- /dev/null
+++ b/src/client/elm/Dialog/AddPayment/Model.elm
@@ -0,0 +1,61 @@
+module Dialog.AddPayment.Model exposing
+ ( Model
+ , init
+ , initialAdd
+ , initialClone
+ , initialEdit
+ )
+
+import Date exposing (Date)
+import View.Date as Date
+
+import Form exposing (Form)
+import Form.Field as Field exposing (Field)
+import Form.Validate as Validate exposing (Validation)
+import Validation
+
+import Model.Payment as Payment exposing (Payment, Frequency, PaymentId)
+import Model.Translations exposing (Translations)
+
+type alias Model =
+ { id : Maybe PaymentId
+ , name : String
+ , cost : Int
+ , date : Date
+ , frequency : Frequency
+ }
+
+init : Form String Model
+init = Form.initial [] validation
+
+initialAdd : Translations -> Date -> Frequency -> List (String, Field)
+initialAdd translations date frequency =
+ [ ("date", Field.Text (Date.shortView date translations))
+ , ("frequency", Field.Radio (toString frequency))
+ ]
+
+initialClone : Translations -> Date -> Payment -> List (String, Field)
+initialClone translations date payment =
+ [ ("name", Field.Text payment.name)
+ , ("cost", Field.Text (toString payment.cost))
+ , ("date", Field.Text (Date.shortView date translations))
+ , ("frequency", Field.Radio (toString payment.frequency))
+ ]
+
+initialEdit : Translations -> Payment -> List (String, Field)
+initialEdit translations payment =
+ [ ("id", Field.Text (toString payment.id))
+ , ("name", Field.Text payment.name)
+ , ("cost", Field.Text (toString payment.cost))
+ , ("date", Field.Text (Date.shortView payment.date translations))
+ , ("frequency", Field.Radio (toString payment.frequency))
+ ]
+
+validation : Validation String Model
+validation =
+ Validate.form5 Model
+ (Validate.get "id" (Validate.maybe Validate.int))
+ (Validate.get "name" (Validate.string `Validate.andThen` (Validate.nonEmpty)))
+ (Validate.get "cost" (Validate.int `Validate.andThen` (Validate.minInt 1)))
+ (Validate.get "date" Validation.date)
+ (Validate.get "frequency" Payment.validateFrequency)
diff --git a/src/client/elm/Dialog/AddPayment/View.elm b/src/client/elm/Dialog/AddPayment/View.elm
new file mode 100644
index 0000000..3ffe200
--- /dev/null
+++ b/src/client/elm/Dialog/AddPayment/View.elm
@@ -0,0 +1,82 @@
+module Dialog.AddPayment.View exposing
+ ( button
+ )
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Html.App as Html
+import Task
+
+import Form exposing (Form)
+import Form.Field as Field exposing (Field)
+import Utils.Form as Form
+
+import Dialog
+import Dialog.AddPayment.Model as AddPayment
+import Dialog.Msg as DialogMsg
+
+import Tooltip
+
+import View.Form as Form
+import View.Events exposing (onSubmitPrevDefault)
+
+import Msg exposing (Msg)
+import LoggedIn.Msg as LoggedInMsg
+import LoggedIn.Home.Msg as HomeMsg
+
+import Model.Translations exposing (getMessage)
+import Model.Payment as Payment exposing (Frequency(..))
+import Model.View exposing (View(LoggedInView))
+
+import LoggedData exposing (LoggedData)
+import LoggedIn.Home.Model as HomeModel
+
+button : LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg
+button loggedData initialForm title buttonContent tooltip =
+ let dialogConfig =
+ { className = "paymentDialog"
+ , title = getMessage title loggedData.translations
+ , body = \model -> addPaymentForm loggedData model.addPayment
+ , confirm = getMessage "Confirm" loggedData.translations
+ , confirmMsg = submitForm << .addPayment
+ , undo = getMessage "Undo" loggedData.translations
+ }
+ in Html.button
+ ( ( case tooltip of
+ Just message -> Tooltip.show Msg.Tooltip message
+ Nothing -> []
+ )
+ ++ [ class "addPayment"
+ , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset initialForm))
+ ]
+ )
+ [ buttonContent ]
+
+addPaymentForm : LoggedData -> Form String AddPayment.Model -> Html Msg
+addPaymentForm loggedData addPayment =
+ let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg)
+ in Html.form
+ [ class "addPayment"
+ , onSubmitPrevDefault Msg.NoOp
+ ]
+ [ htmlMap <| Form.textInput loggedData.translations addPayment "payment" "name"
+ , htmlMap <| Form.textInput loggedData.translations addPayment "payment" "cost"
+ , if (Form.getFieldAsString "frequency" addPayment).value == Just (toString Punctual)
+ then htmlMap <| Form.textInput loggedData.translations addPayment "payment" "date"
+ else text ""
+ , htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ]
+ , Form.hiddenSubmit (submitForm addPayment)
+ ]
+
+submitForm : Form String AddPayment.Model -> Msg
+submitForm addPayment =
+ case Form.getOutput addPayment of
+ Just data ->
+ case data.id of
+ Just paymentId ->
+ Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.EditPayment paymentId data.name data.cost data.date data.frequency
+ Nothing ->
+ Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.CreatePayment data.name data.cost data.date data.frequency
+ Nothing ->
+ Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg <| Form.Submit