From 64ff4707fdcd81c27c6be9903c3c82bc543ef016 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 27 Mar 2017 10:18:40 +0200 Subject: Modelize punctual and monthly payment pages --- src/client/Model/Frequency.elm | 36 ++++++++++++++++++++++++++++++++++++ src/client/Model/Payment.elm | 29 ++--------------------------- 2 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 src/client/Model/Frequency.elm (limited to 'src/client/Model') 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 diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm index f61ded8..2412ab9 100644 --- a/src/client/Model/Payment.elm +++ b/src/client/Model/Payment.elm @@ -3,7 +3,6 @@ module Model.Payment exposing , Payments , Payment , PaymentId - , Frequency(..) , paymentsDecoder , paymentIdDecoder , find @@ -14,7 +13,6 @@ module Model.Payment exposing , monthly , groupAndSortByMonth , search - , validateFrequency ) import Date exposing (..) @@ -24,9 +22,9 @@ import Json.Decode.Extra as Decode import List import Form.Validate as Validate exposing (Validation) +import Model.Frequency as Frequency exposing (Frequency(..)) import Model.Date exposing (dateDecoder) import Model.User exposing (UserId, userIdDecoder) - import Utils.List as List import Utils.Search as Search @@ -46,8 +44,6 @@ type alias Payment = type alias PaymentId = Int -type Frequency = Punctual | Monthly - paymentsDecoder : Decoder Payments paymentsDecoder = Decode.list paymentDecoder @@ -59,20 +55,11 @@ paymentDecoder = (Decode.field "cost" Decode.int) (Decode.field "date" dateDecoder) (Decode.field "userId" userIdDecoder) - (Decode.field "frequency" frequencyDecoder) + (Decode.field "frequency" Frequency.decoder) paymentIdDecoder : Decoder PaymentId paymentIdDecoder = Decode.int -frequencyDecoder : Decoder Frequency -frequencyDecoder = - let frequencyResult input = - case input of - "Punctual" -> Ok Punctual - "Monthly" -> Ok Monthly - _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input) - in Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult) - find : PaymentId -> Payments -> Maybe Payment find paymentId payments = payments @@ -129,15 +116,3 @@ searchSuccess search { name, cost } = || String.contains word (toString cost) ) in List.all searchSuccessWord (String.words search) - -validateFrequency : Validation String Frequency -validateFrequency = - Validate.customValidation Validate.string (\str -> - if str == toString Punctual - then - Ok Punctual - else - if str == toString Monthly - then Ok Monthly - else Err (Validate.customError "InvalidFrequency") - ) -- cgit v1.2.3