From cfca18262c1ff48dcb683ddab7d03cf8e55573ff Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 24 Mar 2017 09:21:04 +0000 Subject: Features/categories --- src/client/elm/Model/Payment.elm | 61 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 27 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 5109b2f..f61ded8 100644 --- a/src/client/elm/Model/Payment.elm +++ b/src/client/elm/Model/Payment.elm @@ -6,6 +6,7 @@ module Model.Payment exposing , Frequency(..) , paymentsDecoder , paymentIdDecoder + , find , edit , delete , totalPayments @@ -18,15 +19,16 @@ module Model.Payment exposing import Date exposing (..) import Date.Extra.Core exposing (monthToInt, intToMonth) -import Json.Decode as Json exposing ((:=)) -import String +import Json.Decode as Decode exposing (Decoder) +import Json.Decode.Extra as Decode import List import Form.Validate as Validate exposing (Validation) -import Model.User exposing (UserId, userIdDecoder) import Model.Date exposing (dateDecoder) +import Model.User exposing (UserId, userIdDecoder) import Utils.List as List +import Utils.Search as Search perPage : Int perPage = 7 @@ -46,31 +48,36 @@ type alias PaymentId = Int type Frequency = Punctual | Monthly -paymentsDecoder : Json.Decoder Payments -paymentsDecoder = Json.list paymentDecoder +paymentsDecoder : Decoder Payments +paymentsDecoder = Decode.list paymentDecoder -paymentDecoder : Json.Decoder Payment +paymentDecoder : Decoder Payment paymentDecoder = - Json.object6 Payment - ("id" := paymentIdDecoder) - ("name" := Json.string) - ("cost" := Json.int) - ("date" := dateDecoder) - ("userId" := userIdDecoder) - ("frequency" := frequencyDecoder) - -paymentIdDecoder : Json.Decoder PaymentId -paymentIdDecoder = Json.int - -frequencyDecoder : Json.Decoder Frequency + Decode.map6 Payment + (Decode.field "id" paymentIdDecoder) + (Decode.field "name" Decode.string) + (Decode.field "cost" Decode.int) + (Decode.field "date" dateDecoder) + (Decode.field "userId" userIdDecoder) + (Decode.field "frequency" frequencyDecoder) + +paymentIdDecoder : Decoder PaymentId +paymentIdDecoder = Decode.int + +frequencyDecoder : Decoder Frequency frequencyDecoder = - Json.customDecoder - Json.string - (\input -> case input of - "Punctual" -> Ok Punctual - "Monthly" -> Ok Monthly - _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input) - ) + 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 + |> List.filter (\p -> p.id == paymentId) + |> List.head edit : Payment -> Payments -> Payments edit payment payments = payment :: delete payment.id payments @@ -98,7 +105,7 @@ groupAndSortByMonth : Payments -> List ((Month, Int), Payments) groupAndSortByMonth payments = payments |> List.groupBy (\payment -> (Date.year payment.date, monthToInt << Date.month <| payment.date)) - |> List.sortBy fst + |> List.sortBy Tuple.first |> List.map (\((year, month), payments) -> ((intToMonth month, year), payments)) |> List.reverse @@ -118,7 +125,7 @@ paymentSort frequency = searchSuccess : String -> Payment -> Bool searchSuccess search { name, cost } = let searchSuccessWord word = - ( String.contains (String.toLower word) (String.toLower name) + ( String.contains (Search.format word) (Search.format name) || String.contains word (toString cost) ) in List.all searchSuccessWord (String.words search) -- cgit v1.2.3