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
|
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
|