From ac8a7c6210e2f430a3015e8004ff0726ef24d63b Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 6 Apr 2016 14:28:16 +0200 Subject: Add payments by month in stat --- src/client/elm/LoggedIn/Stat/View.elm | 31 +++++++++++++++++++------------ src/client/elm/Model/Payment.elm | 11 +++++++++++ src/client/elm/Utils/List.elm | 13 +++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 src/client/elm/Utils/List.elm diff --git a/src/client/elm/LoggedIn/Stat/View.elm b/src/client/elm/LoggedIn/Stat/View.elm index 3fe9d1f..a289002 100644 --- a/src/client/elm/LoggedIn/Stat/View.elm +++ b/src/client/elm/LoggedIn/Stat/View.elm @@ -37,25 +37,32 @@ paymentsDetail loggedData payments = ] , li [] - [ payments - |> List.map .cost - |> List.sum - |> Format.price loggedData.conf - |> text - ] + [ text (paymentsSum loggedData.conf payments) ] ] monthsDetail : LoggedData -> Html monthsDetail loggedData = ul [] - [] + ( Payment.punctual loggedData.payments + |> Payment.groupAndSortByMonth + |> List.map (monthDetail loggedData.conf) + ) -monthDetail : String -> Int -> Html -monthDetail month amount = +monthDetail : Conf -> ((Int, Int), Payments) -> Html +monthDetail conf ((year, month), payments) = li [] - [ text month - , text " " - , text (toString amount) + [ text (toString month) + , text "/" + , text (toString year) + , text " − " + , text (paymentsSum conf payments) ] + +paymentsSum : Conf -> Payments -> String +paymentsSum conf payments = + payments + |> List.map .cost + |> List.sum + |> Format.price conf diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm index 27d5bed..69315a9 100644 --- a/src/client/elm/Model/Payment.elm +++ b/src/client/elm/Model/Payment.elm @@ -10,6 +10,7 @@ module Model.Payment , totalPayments , punctual , monthly + , groupAndSortByMonth ) where import Date exposing (..) @@ -18,6 +19,9 @@ import Json.Decode as Json exposing ((:=)) import Model.User exposing (UserId, userIdDecoder) import Model.Date exposing (dateDecoder) +import Utils.List as List +import Utils.Date as Date + perPage : Int perPage = 8 @@ -81,3 +85,10 @@ punctual = List.filter ((==) Punctual << .frequency) monthly : UserId -> Payments -> Payments monthly userId = List.filter (\p -> p.frequency == Monthly && p.userId == userId) + +groupAndSortByMonth : Payments -> List ((Int, Int), Payments) +groupAndSortByMonth payments = + payments + |> List.groupBy (\payment -> (Date.year payment.creation, Date.monthToNum << Date.month <| payment.creation)) + |> List.sortBy fst + |> List.reverse diff --git a/src/client/elm/Utils/List.elm b/src/client/elm/Utils/List.elm new file mode 100644 index 0000000..85cdc24 --- /dev/null +++ b/src/client/elm/Utils/List.elm @@ -0,0 +1,13 @@ +module Utils.List + ( groupBy + ) where + +import Dict + +groupBy : (a -> comparable) -> List a -> List (comparable, List a) +groupBy f xs = + let addItem item dict = + let groupItems = Dict.get (f item) dict |> Maybe.withDefault [] + in Dict.insert (f item) (item :: groupItems) dict + in List.foldr addItem Dict.empty xs + |> Dict.toList -- cgit v1.2.3