aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/Frequency.elm
blob: 40f98937426bb270c1311e9ae8b020a7ee06b5e8 (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
module Model.Frequency exposing
  ( Frequency(..)
  , decoder
  , validate
  , fromString
  )

import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Extra as Decode

import Form.Validate as Validate exposing (Validation)

type Frequency = Punctual | Monthly

decoder : Decoder Frequency
decoder =
  let frequencyResult input =
        fromString input
          |> Result.fromMaybe ("Could not deduce Punctual nor Monthly from " ++ input)
  in  Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult)

validate : Validation String Frequency
validate =
  Validate.customValidation Validate.string (\str ->
    fromString str
      |> Result.fromMaybe (Validate.customError "InvalidFrequency")
  )

fromString : String -> Maybe Frequency
fromString str =
  if str == toString Punctual then
    Just Punctual
  else if str == toString Monthly then
    Just Monthly
  else
    Nothing