diff options
author | Joris | 2016-06-25 15:10:03 +0200 |
---|---|---|
committer | Joris | 2016-06-25 22:09:04 +0200 |
commit | 70720548c9af024dbb6080638ac8e5470c2213eb (patch) | |
tree | a149bd6d8f1448de11ac4b0b41c52bc82052035a /src/client/elm/Model | |
parent | 4ce4de89a5400b0d8b9cddaa2922901a081fdaaa (diff) |
Use the search to view either punctual or monthly payments
Diffstat (limited to 'src/client/elm/Model')
-rw-r--r-- | src/client/elm/Model/Payment.elm | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm index 7a6c630..ab3cbb7 100644 --- a/src/client/elm/Model/Payment.elm +++ b/src/client/elm/Model/Payment.elm @@ -11,7 +11,8 @@ module Model.Payment exposing , punctual , monthly , groupAndSortByMonth - , sortedFiltredPunctual + , search + , validateFrequency ) import Date exposing (..) @@ -19,6 +20,7 @@ import Date.Extra.Core exposing (monthToInt, intToMonth) import Json.Decode as Json exposing ((:=)) import String +import Form.Validate as Validate exposing (Validation) import Model.User exposing (UserId, userIdDecoder) import Model.Date exposing (dateDecoder) @@ -77,7 +79,6 @@ totalPayments paymentFilter userId payments = |> List.filter (\payment -> paymentFilter payment && payment.userId == userId - && payment.frequency == Punctual ) |> List.map .cost |> List.sum @@ -85,8 +86,8 @@ totalPayments paymentFilter userId payments = punctual : Payments -> Payments punctual = List.filter ((==) Punctual << .frequency) -monthly : UserId -> Payments -> Payments -monthly userId = List.filter (\p -> p.frequency == Monthly && p.userId == userId) +monthly : Payments -> Payments +monthly = List.filter ((==) Monthly << .frequency) groupAndSortByMonth : Payments -> List ((Month, Int), Payments) groupAndSortByMonth payments = @@ -96,12 +97,25 @@ groupAndSortByMonth payments = |> List.map (\((year, month), payments) -> ((intToMonth month, year), payments)) |> List.reverse -sortedFiltredPunctual : String -> Payments -> Payments -sortedFiltredPunctual search payments = - punctual payments +search : String -> Frequency -> Payments -> Payments +search name frequency payments = + payments + |> List.filter ((==) frequency << .frequency) |> List.sortBy (Date.toTime << .creation) - |> List.filter (searchSuccess search) + |> List.filter (searchSuccess name) |> List.reverse searchSuccess : String -> Payment -> Bool searchSuccess text { name } = (String.toLower text) `String.contains` (String.toLower name) + +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") + ) |