From 70720548c9af024dbb6080638ac8e5470c2213eb Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 25 Jun 2016 15:10:03 +0200 Subject: Use the search to view either punctual or monthly payments --- src/client/elm/Model/Payment.elm | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/client/elm/Model/Payment.elm') 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") + ) -- cgit v1.2.3