aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model/Income.elm
diff options
context:
space:
mode:
authorJoris2017-03-24 09:21:04 +0000
committerJoris2017-03-24 09:21:04 +0000
commitcfca18262c1ff48dcb683ddab7d03cf8e55573ff (patch)
tree8a438430cee7411259fc395d8f3898488e85d750 /src/client/elm/Model/Income.elm
parent293eb8295162bf0a038f488237db9c9d1316c04d (diff)
downloadbudget-cfca18262c1ff48dcb683ddab7d03cf8e55573ff.tar.gz
budget-cfca18262c1ff48dcb683ddab7d03cf8e55573ff.tar.bz2
budget-cfca18262c1ff48dcb683ddab7d03cf8e55573ff.zip
Features/categories
Diffstat (limited to 'src/client/elm/Model/Income.elm')
-rw-r--r--src/client/elm/Model/Income.elm37
1 files changed, 15 insertions, 22 deletions
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 =