aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Income/Model.elm
blob: bc09f0e35fba88904f1a0169289216be4fdc58af (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
module LoggedIn.Income.Model exposing
  ( Model
  , AddIncome
  , init
  )

import String exposing (toInt, split)
import Date exposing (Date)
import Date.Extra.Create exposing (dateFromFields)
import Utils.Date exposing (numToMonth)

import Form exposing (Form)
import Form.Validate as Validate exposing (..)
import Form.Error exposing (Error(InvalidString))

type alias Model =
  { addIncome : Form () AddIncome
  }

type alias AddIncome =
  { creation : Date
  , amount : Int
  }

init : Model
init =
  { addIncome = Form.initial [] validate
  }

validate : Validation () AddIncome
validate =
  form2 AddIncome
    (get "creation" dateValidation)
    (get "amount" (int `andThen` (minInt 1)))

dateValidation : Validation () Date
dateValidation =
  customValidation string (\str ->
    case split "/" str of
      [day, month, year] ->
        case (toInt day, toInt month, toInt year) of
          (Ok dayNum, Ok monthNum, Ok yearNum) ->
            Ok (dateFromFields yearNum (numToMonth monthNum) dayNum 0 0 0 0)
          _ -> Err InvalidString
      _ -> Err InvalidString
  )