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
|
module Dialog.AddIncome.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.Translations exposing (Translations)
import Model.Income exposing (Income, IncomeId)
type alias Model =
{ id : Maybe IncomeId
, amount : Int
, date : Date
}
init : Form String Model
init = Form.initial [] validate
initialAdd : Translations -> Date -> List (String, Field)
initialAdd translations date =
[ ("date", Field.Text (Date.shortView date translations))
]
initialClone : Translations -> Date -> Income -> List (String, Field)
initialClone translations date income =
[ ("amount", Field.Text (toString income.amount))
, ("date", Field.Text (Date.shortView (Date.fromTime income.time) translations))
]
initialEdit : Translations -> IncomeId -> Income -> List (String, Field)
initialEdit translations incomeId income =
[ ("id", Field.Text (toString incomeId))
, ("amount", Field.Text (toString income.amount))
, ("date", Field.Text (Date.shortView (Date.fromTime income.time) translations))
]
validate : Validation String Model
validate =
Validate.form3 Model
(Validate.get "id" (Validate.maybe Validate.int))
(Validate.get "amount" (Validate.int `Validate.andThen` (Validate.minInt 1)))
(Validate.get "date" Validation.date)
|