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/Income.elm | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'src/client/elm/Model/Income.elm') diff --git a/src/client/elm/Model/Income.elm b/src/client/elm/Model/Income.elm index a5ca34b..34578c6 100644 --- a/src/client/elm/Model/Income.elm +++ b/src/client/elm/Model/Income.elm @@ -9,7 +9,8 @@ module Model.Income exposing , cumulativeIncomesSince ) -import Json.Decode as Json exposing ((:=)) +import Json.Decode as Decode exposing (Decoder) +import Utils.Json as Json import Time exposing (Time, hour) import List exposing (..) import Dict exposing (Dict) @@ -17,7 +18,7 @@ import Dict exposing (Dict) import Model.Date exposing (timeDecoder) import Model.User exposing (UserId, userIdDecoder) -import Utils.Maybe exposing (isJust, catMaybes, maybeToList) +import Utils.Maybe as Maybe type alias Incomes = Dict IncomeId Income @@ -29,31 +30,23 @@ type alias Income = , amount : Int } -incomesDecoder : Json.Decoder Incomes -incomesDecoder = Json.map Dict.fromList (Json.list incomeWithIdDecoder) +incomesDecoder : Decoder Incomes +incomesDecoder = + Json.dictDecoder (Decode.field "id" incomeIdDecoder) <| + Decode.map3 Income + (Decode.field "userId" userIdDecoder) + (Decode.field "date" timeDecoder) + (Decode.field "amount" Decode.int) -incomeWithIdDecoder : Json.Decoder (IncomeId, Income) -incomeWithIdDecoder = - Json.object2 (,) - ("id" := incomeIdDecoder) - incomeDecoder - -incomeIdDecoder : Json.Decoder IncomeId -incomeIdDecoder = Json.int - -incomeDecoder : Json.Decoder Income -incomeDecoder = - Json.object3 Income - ("userId" := userIdDecoder) - ("date" := timeDecoder) - ("amount" := Json.int) +incomeIdDecoder : Decoder IncomeId +incomeIdDecoder = Decode.int incomeDefinedForAll : List UserId -> Incomes -> Maybe Time incomeDefinedForAll userIds incomes = let userIncomes = List.map (\userId -> List.filter ((==) userId << .userId) << Dict.values <| incomes) userIds firstIncomes = map (head << sortBy .time) userIncomes - in if all isJust firstIncomes - then head << reverse << List.sort << map .time << catMaybes <| firstIncomes + in if all Maybe.isJust firstIncomes + then head << reverse << List.sort << map .time << Maybe.cat <| firstIncomes else Nothing userCumulativeIncomeSince : Time -> Time -> Incomes -> UserId -> Int @@ -71,7 +64,7 @@ getOrderedIncomesSince : Time -> List Income -> List Income getOrderedIncomesSince time incomes = let mbStarterIncome = getIncomeAt time incomes orderedIncomesSince = filter (\income -> income.time >= time) incomes - in (maybeToList mbStarterIncome) ++ orderedIncomesSince + in (Maybe.toList mbStarterIncome) ++ orderedIncomesSince getIncomeAt : Time -> List Income -> Maybe Income getIncomeAt time incomes = -- cgit v1.2.3