aboutsummaryrefslogtreecommitdiff
path: root/src/client/Dialog/AddPayment
diff options
context:
space:
mode:
authorJoris2017-03-26 21:10:42 +0200
committerJoris2017-03-26 21:10:42 +0200
commit1e47a7754ca38bd1a6c74765d8378caf68ce4619 (patch)
treed0d9238479dc2529a1b558bbbcde346e7e8c2935 /src/client/Dialog/AddPayment
parentc0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (diff)
Separate client and server watch
Diffstat (limited to 'src/client/Dialog/AddPayment')
-rw-r--r--src/client/Dialog/AddPayment/Model.elm70
-rw-r--r--src/client/Dialog/AddPayment/View.elm95
2 files changed, 165 insertions, 0 deletions
diff --git a/src/client/Dialog/AddPayment/Model.elm b/src/client/Dialog/AddPayment/Model.elm
new file mode 100644
index 0000000..11d59b1
--- /dev/null
+++ b/src/client/Dialog/AddPayment/Model.elm
@@ -0,0 +1,70 @@
+module Dialog.AddPayment.Model exposing
+ ( Model
+ , init
+ , initialAdd
+ , initialClone
+ , initialEdit
+ , validation
+ )
+
+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)
+import Model.Category as Category exposing (Categories, CategoryId)
+
+import Utils.Maybe as Maybe
+
+type alias Model =
+ { id : Maybe PaymentId
+ , name : String
+ , cost : Int
+ , date : Date
+ , category : CategoryId
+ , frequency : Frequency
+ }
+
+init : Form String Model
+init = Form.initial [] (validation Category.empty)
+
+initialAdd : Translations -> Date -> Frequency -> List (String, Field)
+initialAdd translations date frequency =
+ [ ("date", Field.string (Date.shortView date translations))
+ , ("frequency", Field.string (toString frequency))
+ , ("category", Field.string "")
+ ]
+
+initialClone : Translations -> Date -> Maybe CategoryId -> Payment -> List (String, Field)
+initialClone translations date category payment =
+ [ ("name", Field.string payment.name)
+ , ("cost", Field.string (toString payment.cost))
+ , ("date", Field.string (Date.shortView date translations))
+ , ("frequency", Field.string (toString payment.frequency))
+ , ("category", Field.string (Maybe.map toString category |> Maybe.withDefault ""))
+ ]
+
+initialEdit : Translations -> Maybe CategoryId -> Payment -> List (String, Field)
+initialEdit translations category payment =
+ [ ("id", Field.string (toString payment.id))
+ , ("name", Field.string payment.name)
+ , ("cost", Field.string (toString payment.cost))
+ , ("date", Field.string (Date.shortView payment.date translations))
+ , ("frequency", Field.string (toString payment.frequency))
+ , ("category", Field.string (Maybe.map toString category |> Maybe.withDefault ""))
+ ]
+
+validation : Categories -> Validation String Model
+validation categories =
+ Validate.map6 Model
+ (Validate.field "id" (Validate.maybe Validate.int))
+ (Validate.field "name" (Validate.string |> Validate.andThen Validate.nonEmpty))
+ (Validate.field "cost" Validation.cost)
+ (Validate.field "date" Validation.date)
+ (Validate.field "category" (Validation.category categories))
+ (Validate.field "frequency" Payment.validateFrequency)
diff --git a/src/client/Dialog/AddPayment/View.elm b/src/client/Dialog/AddPayment/View.elm
new file mode 100644
index 0000000..078d5b7
--- /dev/null
+++ b/src/client/Dialog/AddPayment/View.elm
@@ -0,0 +1,95 @@
+module Dialog.AddPayment.View exposing
+ ( button
+ )
+
+import Dict
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+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.Events exposing (onSubmitPrevDefault)
+import View.Form as Form
+
+import LoggedIn.Home.Msg as HomeMsg
+import LoggedIn.Msg as LoggedInMsg
+import Msg exposing (Msg)
+
+import Model.Category exposing (Categories)
+import Model.Payment as Payment exposing (Frequency(..))
+import Model.PaymentCategory exposing (PaymentCategories)
+import Model.Translations exposing (getMessage)
+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 loggedData.translations title
+ , body = \model -> addPaymentForm loggedData model.addPayment
+ , confirm = getMessage loggedData.translations "Confirm"
+ , confirmMsg = submitForm loggedData.categories loggedData.paymentCategories << .addPayment
+ , undo = getMessage loggedData.translations "Undo"
+ }
+ in Html.button
+ ( ( case tooltip of
+ Just message -> Tooltip.show Msg.Tooltip message
+ Nothing -> []
+ )
+ ++ [ class "addPayment"
+ , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.Init "paymentname" (DialogMsg.AddPaymentMsg loggedData.categories loggedData.paymentCategories <| 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 loggedData.categories loggedData.paymentCategories)
+ categoryOptions =
+ loggedData.categories
+ |> Dict.toList
+ |> List.sortBy (.name << Tuple.second)
+ |> List.map (\(id, category) -> (toString id, category.name))
+ 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.selectInput loggedData.translations addPayment "payment" "category" categoryOptions
+
+ , htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ]
+ , Form.hiddenSubmit (submitForm loggedData.categories loggedData.paymentCategories addPayment)
+ ]
+
+submitForm : Categories -> PaymentCategories -> Form String AddPayment.Model -> Msg
+submitForm categories paymentCategories addPayment =
+ case Form.getOutput addPayment of
+ Just data ->
+ case data.id of
+ Just paymentId ->
+ Msg.Dialog
+ <| Dialog.UpdateAndClose
+ <| Msg.EditPayment paymentId data.name data.cost data.date data.category data.frequency
+ Nothing ->
+ Msg.Dialog
+ <| Dialog.UpdateAndClose
+ <| Msg.CreatePayment data.name data.cost data.date data.category data.frequency
+ Nothing ->
+ Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg categories paymentCategories <| Form.Submit