aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Dialog/Model.elm
blob: b49d8f1650cc2d40a687fe75a13367a49467b749 (plain)
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
module Dialog.Model exposing
  ( Model
  , AddPayment
  , init
  , addPaymentInitial
  , clonePaymentInitial
  , editPaymentInitial
  )

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)

type alias Model =
  { addPayment : Form String AddPayment
  }

type alias AddPayment =
  { id : Maybe PaymentId
  , name : String
  , cost : Int
  , date : Date
  , frequency : Frequency
  }

init : Model
init =
  { addPayment = Form.initial [] addPaymentValidation
  }

addPaymentInitial : Translations -> Date -> Frequency -> List (String, Field)
addPaymentInitial translations date frequency =
  [ ("date", Field.Text (Date.shortView date translations))
  , ("frequency", Field.Radio (toString frequency))
  ]

clonePaymentInitial : Translations -> Date -> Payment -> List (String, Field)
clonePaymentInitial translations date payment =
  [ ("name", Field.Text payment.name)
  , ("cost", Field.Text (toString payment.cost))
  , ("date", Field.Text (Date.shortView date translations))
  , ("frequency", Field.Radio (toString payment.frequency))
  ]

editPaymentInitial : Translations -> Payment -> List (String, Field)
editPaymentInitial translations payment =
  [ ("id", Field.Text (toString payment.id))
  , ("name", Field.Text payment.name)
  , ("cost", Field.Text (toString payment.cost))
  , ("date", Field.Text (Date.shortView payment.date translations))
  , ("frequency", Field.Radio (toString payment.frequency))
  ]

addPaymentValidation : Validation String AddPayment
addPaymentValidation =
  Validate.form5 AddPayment
    (Validate.get "id" (Validate.maybe Validate.int))
    (Validate.get "name" (Validate.string `Validate.andThen` (Validate.nonEmpty)))
    (Validate.get "cost" (Validate.int `Validate.andThen` (Validate.minInt 1)))
    (Validate.get "date" Validation.date)
    (Validate.get "frequency" Payment.validateFrequency)