aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Dialog
diff options
context:
space:
mode:
authorJoris2016-06-25 15:10:03 +0200
committerJoris2016-06-25 22:09:04 +0200
commit70720548c9af024dbb6080638ac8e5470c2213eb (patch)
treea149bd6d8f1448de11ac4b0b41c52bc82052035a /src/client/elm/Dialog
parent4ce4de89a5400b0d8b9cddaa2922901a081fdaaa (diff)
Use the search to view either punctual or monthly payments
Diffstat (limited to 'src/client/elm/Dialog')
-rw-r--r--src/client/elm/Dialog/AddPayment/View.elm64
-rw-r--r--src/client/elm/Dialog/Model.elm37
-rw-r--r--src/client/elm/Dialog/Msg.elm9
-rw-r--r--src/client/elm/Dialog/Update.elm24
4 files changed, 134 insertions, 0 deletions
diff --git a/src/client/elm/Dialog/AddPayment/View.elm b/src/client/elm/Dialog/AddPayment/View.elm
new file mode 100644
index 0000000..8915b1d
--- /dev/null
+++ b/src/client/elm/Dialog/AddPayment/View.elm
@@ -0,0 +1,64 @@
+module Dialog.AddPayment.View exposing
+ ( view
+ )
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Html.App as Html
+import Task
+
+import Form exposing (Form)
+
+import Dialog
+
+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 Dialog.Model as DialogModel
+import Dialog.Msg as DialogMsg
+
+import LoggedData exposing (LoggedData)
+import LoggedIn.Home.Model as HomeModel
+
+view : LoggedData -> Frequency -> Html Msg
+view loggedData frequency =
+ let dialogConfig =
+ { className = "paymentDialog"
+ , title = getMessage "AddPayment" loggedData.translations
+ , body = \model -> addPaymentForm loggedData model.addPayment
+ , confirm = getMessage "Confirm" loggedData.translations
+ , confirmMsg = \model -> (
+ case Form.getOutput model.addPayment of
+ Just data ->
+ Ok (Msg.UpdateLoggedIn <| LoggedInMsg.AddPayment data.name data.cost data.frequency)
+ Nothing ->
+ Err (Msg.Dialog <| Dialog.UpdateModel <| DialogMsg.AddPaymentMsg <| Form.Submit)
+ )
+ , undo = getMessage "Undo" loggedData.translations
+ }
+ in button
+ [ class "addPayment"
+ , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset (DialogModel.addPaymentInitial frequency)))
+ ]
+ [ text (getMessage "AddPayment" loggedData.translations) ]
+
+addPaymentForm : LoggedData -> Form String DialogModel.AddPayment -> Html Msg
+addPaymentForm loggedData addPayment =
+ let htmlMap = Html.map (Msg.Dialog << Dialog.UpdateModel << DialogMsg.AddPaymentMsg)
+ in Html.form
+ [ class "addPayment"
+ , onSubmitPrevDefault Msg.NoOp
+ ]
+ [ Form.textInput loggedData.translations addPayment htmlMap "payment" "name"
+ , Form.textInput loggedData.translations addPayment htmlMap "payment" "cost"
+ , Form.radioInputs loggedData.translations addPayment htmlMap "payment" "frequency" [ toString Punctual, toString Monthly ]
+ ]
diff --git a/src/client/elm/Dialog/Model.elm b/src/client/elm/Dialog/Model.elm
new file mode 100644
index 0000000..9bd6a09
--- /dev/null
+++ b/src/client/elm/Dialog/Model.elm
@@ -0,0 +1,37 @@
+module Dialog.Model exposing
+ ( Model
+ , AddPayment
+ , init
+ , addPaymentInitial
+ )
+
+import Form exposing (Form)
+import Form.Field as Field exposing (Field)
+import Form.Validate as Validate exposing (Validation)
+
+import Model.Payment as Payment
+
+type alias Model =
+ { addPayment : Form String AddPayment
+ }
+
+type alias AddPayment =
+ { name : String
+ , cost : Int
+ , frequency : Payment.Frequency
+ }
+
+init : Model
+init =
+ { addPayment = Form.initial [] addPaymentValidation
+ }
+
+addPaymentInitial : Payment.Frequency -> List (String, Field)
+addPaymentInitial frequency = [ ("frequency", Field.Radio (toString frequency)) ]
+
+addPaymentValidation : Validation String AddPayment
+addPaymentValidation =
+ Validate.form3 AddPayment
+ (Validate.get "name" (Validate.string `Validate.andThen` (Validate.nonEmpty)))
+ (Validate.get "cost" (Validate.int `Validate.andThen` (Validate.minInt 1)))
+ (Validate.get "frequency" Payment.validateFrequency)
diff --git a/src/client/elm/Dialog/Msg.elm b/src/client/elm/Dialog/Msg.elm
new file mode 100644
index 0000000..c9e1596
--- /dev/null
+++ b/src/client/elm/Dialog/Msg.elm
@@ -0,0 +1,9 @@
+module Dialog.Msg exposing
+ ( Msg(..)
+ )
+
+import Form exposing (Form)
+
+type Msg =
+ NoOp
+ | AddPaymentMsg Form.Msg
diff --git a/src/client/elm/Dialog/Update.elm b/src/client/elm/Dialog/Update.elm
new file mode 100644
index 0000000..e1e2dba
--- /dev/null
+++ b/src/client/elm/Dialog/Update.elm
@@ -0,0 +1,24 @@
+module Dialog.Update exposing
+ ( update
+ )
+
+import Form exposing (Form)
+
+import Dialog.Msg as Dialog
+import Dialog.Model as Dialog
+
+update : Dialog.Msg -> Dialog.Model -> (Dialog.Model, Cmd Dialog.Msg)
+update msg model =
+ case msg of
+
+ Dialog.NoOp ->
+ ( model
+ , Cmd.none
+ )
+
+ Dialog.AddPaymentMsg formMsg ->
+ ( { model
+ | addPayment = Form.update formMsg model.addPayment
+ }
+ , Cmd.none
+ )