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