aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/Income/Model.elm
blob: 873eaf1f186d3e40f521036b55cfd346c8208fa9 (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
import Time exposing (Time)
import Date.Extra.Create exposing (dateFromFields)
import Date.Extra.Core exposing (intToMonth)

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

type alias Model =
  { addIncome : Form String AddIncome
  }

type alias AddIncome =
  { time : Time
  , amount : Int
  }

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

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

timeValidation : Validation String Time
timeValidation =
  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 (Date.toTime (dateFromFields yearNum (intToMonth monthNum) dayNum 0 0 0 0))
          _ -> Err (customError "InvalidDate")
      _ -> Err (customError "InvalidDate")
  )