aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/Model')
-rw-r--r--src/client/Model/Payers.elm18
-rw-r--r--src/client/Model/Payment.elm12
-rw-r--r--src/client/Model/User.elm42
-rw-r--r--src/client/Model/View/PaymentView.elm11
4 files changed, 66 insertions, 17 deletions
diff --git a/src/client/Model/Payers.elm b/src/client/Model/Payers.elm
index 6550eaa..983e7b3 100644
--- a/src/client/Model/Payers.elm
+++ b/src/client/Model/Payers.elm
@@ -11,21 +11,23 @@ import Dict exposing (..)
import List
import Maybe
-type alias Payers = Dict String Int
+import Model.User exposing (UserId, userIdDecoder)
+
+type alias Payers = Dict UserId Int
payersDecoder : Decoder Payers
payersDecoder = Json.map Dict.fromList (list payerDecoder)
-payerDecoder : Decoder (String, Int)
+payerDecoder : Decoder (UserId, Int)
payerDecoder =
object2 (,)
- ("userName" := string)
+ ("userId" := userIdDecoder)
("totalPayment" := int)
-updatePayers : Payers -> String -> Int -> Payers
-updatePayers payers userName amountDiff =
+updatePayers : Payers -> UserId -> Int -> Payers
+updatePayers payers userId amountDiff =
Dict.update
- userName
+ userId
(\mbAmount ->
case mbAmount of
Just amount -> Just (amount + amountDiff)
@@ -34,7 +36,7 @@ updatePayers payers userName amountDiff =
payers
type alias ExceedingPayer =
- { userName : String
+ { userId : UserId
, amount : Int
}
@@ -42,7 +44,7 @@ getOrderedExceedingPayers : Payers -> List ExceedingPayer
getOrderedExceedingPayers payers =
let orderedPayers =
Dict.toList payers
- |> List.map (\(userName, amount) -> ExceedingPayer userName amount)
+ |> List.map (\(userId, amount) -> ExceedingPayer userId amount)
|> List.sortBy .amount
maybeMinAmount =
List.head orderedPayers
diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm
index 8a51c66..4ae50de 100644
--- a/src/client/Model/Payment.elm
+++ b/src/client/Model/Payment.elm
@@ -12,6 +12,8 @@ import Date exposing (..)
import Json.Decode as Json exposing ((:=))
import Dict exposing (..)
+import Model.User exposing (UserId, userIdDecoder)
+
perPage : Int
perPage = 8
@@ -23,17 +25,17 @@ type alias Payment =
{ creation : Date
, name : String
, cost : Int
- , userName : String
+ , userId : UserId
}
-type alias PaymentId = String
+type alias PaymentId = Int
paymentsDecoder : Json.Decoder Payments
paymentsDecoder = Json.map Dict.fromList (Json.list paymentWithIdDecoder)
paymentWithIdDecoder : Json.Decoder (PaymentId, Payment)
paymentWithIdDecoder =
- paymentDecoder `Json.andThen` (\payment -> Json.map (\id -> (id, payment)) ("id" := Json.string))
+ paymentDecoder `Json.andThen` (\payment -> Json.map (\id -> (id, payment)) ("id" := paymentIdDecoder))
paymentDecoder : Json.Decoder Payment
paymentDecoder =
@@ -41,10 +43,10 @@ paymentDecoder =
("creation" := dateDecoder)
("name" := Json.string)
("cost" := Json.int)
- ("userName" := Json.string)
+ ("userId" := userIdDecoder)
paymentIdDecoder : Json.Decoder PaymentId
-paymentIdDecoder = Json.string
+paymentIdDecoder = Json.int
dateDecoder : Json.Decoder Date
dateDecoder = Json.customDecoder Json.string Date.fromString
diff --git a/src/client/Model/User.elm b/src/client/Model/User.elm
new file mode 100644
index 0000000..b0d62a6
--- /dev/null
+++ b/src/client/Model/User.elm
@@ -0,0 +1,42 @@
+module Model.User
+ ( Users
+ , usersDecoder
+ , User
+ , userDecoder
+ , UserId
+ , userIdDecoder
+ , getUserName
+ ) where
+
+import Json.Decode as Json exposing ((:=))
+import Dict exposing (Dict)
+
+type alias Users = Dict UserId User
+
+type alias UserId = Int
+
+type alias User =
+ { name : String
+ , email : String
+ }
+
+usersDecoder : Json.Decoder Users
+usersDecoder = Json.map Dict.fromList (Json.list userWithIdDecoder)
+
+userWithIdDecoder : Json.Decoder (UserId, User)
+userWithIdDecoder =
+ userDecoder `Json.andThen` (\user -> Json.map (\id -> (id, user)) ("id" := userIdDecoder))
+
+userDecoder : Json.Decoder User
+userDecoder =
+ Json.object2 User
+ ("name" := Json.string)
+ ("email" := Json.string)
+
+userIdDecoder : Json.Decoder UserId
+userIdDecoder = Json.int
+
+getUserName : Users -> UserId -> Maybe String
+getUserName users userId =
+ Dict.get userId users
+ |> Maybe.map .name
diff --git a/src/client/Model/View/PaymentView.elm b/src/client/Model/View/PaymentView.elm
index bf5804f..93ab457 100644
--- a/src/client/Model/View/PaymentView.elm
+++ b/src/client/Model/View/PaymentView.elm
@@ -3,13 +3,15 @@ module Model.View.PaymentView
, initPaymentView
) where
+import Model.User exposing (Users, UserId)
import Model.Payment exposing (Payments)
import Model.Payers exposing (Payers)
import Model.View.Payment.Add exposing (..)
import Model.View.Payment.Edition exposing (..)
type alias PaymentView =
- { userName : String
+ { users : Users
+ , me : UserId
, add : AddPayment
, payments : Payments
, paymentsCount : Int
@@ -18,9 +20,10 @@ type alias PaymentView =
, currentPage : Int
}
-initPaymentView : String -> Payments -> Int -> Payers -> PaymentView
-initPaymentView userName payments paymentsCount payers =
- { userName = userName
+initPaymentView : Users -> UserId -> Payments -> Int -> Payers -> PaymentView
+initPaymentView users me payments paymentsCount payers =
+ { users = users
+ , me = me
, add = initAddPayment
, payments = payments
, paymentsCount = paymentsCount