aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/Frequency.elm
diff options
context:
space:
mode:
authorJoris2017-03-27 10:18:40 +0200
committerJoris2017-03-27 10:18:40 +0200
commit64ff4707fdcd81c27c6be9903c3c82bc543ef016 (patch)
treefa0c3a9112f4f7c8bd383ad3e597041ab7d5a503 /src/client/Model/Frequency.elm
parent40273c30166877b3341125ad5248793b2f2fcc64 (diff)
downloadbudget-64ff4707fdcd81c27c6be9903c3c82bc543ef016.tar.gz
budget-64ff4707fdcd81c27c6be9903c3c82bc543ef016.tar.bz2
budget-64ff4707fdcd81c27c6be9903c3c82bc543ef016.zip
Modelize punctual and monthly payment pages
Diffstat (limited to 'src/client/Model/Frequency.elm')
-rw-r--r--src/client/Model/Frequency.elm36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/client/Model/Frequency.elm b/src/client/Model/Frequency.elm
new file mode 100644
index 0000000..40f9893
--- /dev/null
+++ b/src/client/Model/Frequency.elm
@@ -0,0 +1,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