From 3b738e0d4cc65f314da7389d4542ec826ba0f454 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 5 Sep 2015 13:53:36 +0200 Subject: Using UserId instead of UserName to indentify users --- src/client/Main.elm | 10 ++++--- src/client/Model/Payers.elm | 18 +++++++------ src/client/Model/Payment.elm | 12 +++++---- src/client/Model/User.elm | 42 +++++++++++++++++++++++++++++ src/client/Model/View/PaymentView.elm | 11 +++++--- src/client/ServerCommunication.elm | 17 ++++++------ src/client/Update.elm | 7 ++--- src/client/Update/Payment.elm | 13 ++++----- src/client/View/Payments/Add.elm | 2 +- src/client/View/Payments/ExceedingPayer.elm | 19 +++++++++---- src/client/View/Payments/Table.elm | 13 ++++++--- 11 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 src/client/Model/User.elm (limited to 'src/client') diff --git a/src/client/Main.elm b/src/client/Main.elm index 685d3b2..07f8294 100644 --- a/src/client/Main.elm +++ b/src/client/Main.elm @@ -12,6 +12,7 @@ import Time exposing (..) import Json.Decode as Json exposing ((:=)) import Model exposing (Model, initialModel) +import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) import Model.Payment exposing (Payments, paymentsDecoder, perPage) import Model.Payers exposing (Payers, payersDecoder) import Model.Message exposing (messageDecoder) @@ -56,12 +57,15 @@ port initView = Just msg -> Signal.send actions.address (SignInError msg) Nothing -> - Task.map4 GoPaymentView getUserName getPayments getPaymentsCount getPayers + Task.map5 GoPaymentView getUsers whoAmI getPayments getPaymentsCount getPayers |> flip Task.andThen (Signal.send actions.address) |> flip Task.onError (\_ -> Signal.send actions.address GoSignInView) -getUserName : Task Http.Error String -getUserName = Http.get messageDecoder "/userName" +getUsers : Task Http.Error Users +getUsers = Http.get usersDecoder "/users" + +whoAmI : Task Http.Error UserId +whoAmI = Http.get ("id" := userIdDecoder) "/whoAmI" getPayments : Task Http.Error Payments getPayments = Http.get paymentsDecoder ("/payments?page=1&perPage=" ++ toString perPage) 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 diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm index 719a563..5ddcfb9 100644 --- a/src/client/ServerCommunication.elm +++ b/src/client/ServerCommunication.elm @@ -11,6 +11,7 @@ import Json.Decode exposing (..) import Date import Model.Message exposing (messageDecoder) +import Model.User exposing (UserId) import Model.Payment exposing (PaymentId, perPage, paymentsDecoder) import Update as U @@ -20,8 +21,8 @@ import Update.Payment as UP type Communication = NoCommunication | SignIn String - | AddPayment String String Int - | DeletePayment PaymentId String Int Int + | AddPayment UserId String Int + | DeletePayment PaymentId UserId Int Int | UpdatePage Int | SignOut @@ -43,10 +44,10 @@ getRequest communication = Nothing SignIn login -> Just (simple "post" ("/signIn?login=" ++ login)) - AddPayment userName paymentName cost -> + AddPayment userId paymentName cost -> Just (simple "post" ("/payment/add?name=" ++ paymentName ++ "&cost=" ++ (toString cost))) DeletePayment paymentId _ _ _ -> - Just (simple "post" ("payment/delete?id=" ++ paymentId)) + Just (simple "post" ("payment/delete?id=" ++ (toString paymentId))) UpdatePage page -> Just (updatePageRequest page) SignOut -> @@ -73,7 +74,7 @@ serverResult communication response = Task.succeed U.NoOp SignIn login -> Task.succeed (U.UpdateSignIn (ValidLogin login)) - AddPayment userName paymentName cost -> + AddPayment userId paymentName cost -> Http.send Http.defaultSettings (updatePageRequest 1) |> Task.map (\response -> if response.status == 200 @@ -81,11 +82,11 @@ serverResult communication response = decodeResponse response paymentsDecoder - (\payments -> U.UpdatePayment (UP.AddPayment userName cost payments)) + (\payments -> U.UpdatePayment (UP.AddPayment userId cost payments)) else U.NoOp ) - DeletePayment id userName cost currentPage -> + DeletePayment id userId cost currentPage -> Http.send Http.defaultSettings (updatePageRequest currentPage) |> Task.map (\response -> if response.status == 200 @@ -93,7 +94,7 @@ serverResult communication response = decodeResponse response paymentsDecoder - (\payments -> U.UpdatePayment (UP.Remove userName cost payments)) + (\payments -> U.UpdatePayment (UP.Remove userId cost payments)) else U.NoOp ) diff --git a/src/client/Update.elm b/src/client/Update.elm index 374c5d0..910f080 100644 --- a/src/client/Update.elm +++ b/src/client/Update.elm @@ -7,6 +7,7 @@ module Update import Time exposing (Time) import Model exposing (Model) +import Model.User exposing (Users, UserId) import Model.Payment exposing (Payments) import Model.Payers exposing (Payers) import Model.View as V @@ -22,7 +23,7 @@ type Action = | GoSignInView | SignInError String | UpdateSignIn SignInAction - | GoPaymentView String Payments Int Payers + | GoPaymentView Users UserId Payments Int Payers | UpdatePayment PaymentAction actions : Signal.Mailbox Action @@ -37,8 +38,8 @@ updateModel action model = { model | currentTime <- time } GoSignInView -> { model | view <- V.SignInView initSignInView } - GoPaymentView userName payments paymentsCount payers -> - { model | view <- V.PaymentView (initPaymentView userName payments paymentsCount payers) } + GoPaymentView users me payments paymentsCount payers -> + { model | view <- V.PaymentView (initPaymentView users me payments paymentsCount payers) } SignInError msg -> let signInView = { initSignInView | result <- Just (Err msg) } in { model | view <- V.SignInView signInView } diff --git a/src/client/Update/Payment.elm b/src/client/Update/Payment.elm index b9b60dd..1b43f6f 100644 --- a/src/client/Update/Payment.elm +++ b/src/client/Update/Payment.elm @@ -7,6 +7,7 @@ import Date import Dict import Model exposing (Model) +import Model.User exposing (UserId) import Model.Payment exposing (..) import Model.Payers exposing (..) import Model.View.PaymentView exposing (..) @@ -17,9 +18,9 @@ import Update.Payment.Add exposing (..) type PaymentAction = UpdateAdd AddPaymentAction | UpdatePayments Payments - | AddPayment String Int Payments + | AddPayment UserId Int Payments | ToggleEdit PaymentId - | Remove String Int Payments + | Remove UserId Int Payments | UpdatePage Int Payments updatePayment : Model -> PaymentAction -> PaymentView -> PaymentView @@ -29,20 +30,20 @@ updatePayment model action paymentView = { paymentView | add <- updateAddPayment addPaymentAction paymentView.add } UpdatePayments payments -> { paymentView | payments <- payments } - AddPayment userName cost payments -> + AddPayment userId cost payments -> { paymentView | payments <- payments , currentPage <- 1 , add <- initAddPayment - , payers <- updatePayers paymentView.payers userName cost + , payers <- updatePayers paymentView.payers userId cost , paymentsCount <- paymentView.paymentsCount + 1 } ToggleEdit id -> { paymentView | edition <- if paymentView.edition == Just id then Nothing else Just id } - Remove userName cost payments -> + Remove userId cost payments -> { paymentView | payments <- payments - , payers <- updatePayers paymentView.payers userName -cost + , payers <- updatePayers paymentView.payers userId -cost , paymentsCount <- paymentView.paymentsCount - 1 } UpdatePage page payments -> diff --git a/src/client/View/Payments/Add.elm b/src/client/View/Payments/Add.elm index 115fed2..32233ed 100644 --- a/src/client/View/Payments/Add.elm +++ b/src/client/View/Payments/Add.elm @@ -31,7 +31,7 @@ addPayment model paymentView = [ class "add" , case (validateName paymentView.add.name model.translations, validateCost paymentView.add.cost model.translations) of (Ok name, Ok cost) -> - onSubmitPrevDefault serverCommunications.address (SC.AddPayment paymentView.userName name cost) + onSubmitPrevDefault serverCommunications.address (SC.AddPayment paymentView.me name cost) (resName, resCost) -> onSubmitPrevDefault actions.address (UpdatePayment <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost)) ] diff --git a/src/client/View/Payments/ExceedingPayer.elm b/src/client/View/Payments/ExceedingPayer.elm index 903ad5b..f249383 100644 --- a/src/client/View/Payments/ExceedingPayer.elm +++ b/src/client/View/Payments/ExceedingPayer.elm @@ -7,6 +7,7 @@ import Html.Attributes exposing (..) import List import Model exposing (Model) +import Model.User exposing (getUserName) import Model.Payers exposing (..) import Model.View.PaymentView exposing (PaymentView) import Model.Translations exposing (getMessage) @@ -15,12 +16,20 @@ exceedingPayers : Model -> PaymentView -> Html exceedingPayers model paymentView = div [ class "exceedingPayers" ] - (List.map (exceedingPayer model) (getOrderedExceedingPayers paymentView.payers)) + (List.map (exceedingPayer model paymentView) (getOrderedExceedingPayers paymentView.payers)) -exceedingPayer : Model -> ExceedingPayer -> Html -exceedingPayer model payer = +exceedingPayer : Model -> PaymentView -> ExceedingPayer -> Html +exceedingPayer model paymentView payer = div [ class "exceedingPayer" ] - [ span [ class "userName" ] [ text payer.userName ] - , span [ class "amount" ] [ text ("+ " ++ (toString payer.amount) ++ " " ++ (getMessage "MoneySymbol" model.translations)) ] + [ span + [ class "userName" ] + [ payer.userId + |> getUserName paymentView.users + |> Maybe.withDefault "−" + |> text + ] + , span + [ class "amount" ] + [ text ("+ " ++ (toString payer.amount) ++ " " ++ (getMessage "MoneySymbol" model.translations)) ] ] diff --git a/src/client/View/Payments/Table.elm b/src/client/View/Payments/Table.elm index 06bec17..743a8a9 100644 --- a/src/client/View/Payments/Table.elm +++ b/src/client/View/Payments/Table.elm @@ -13,6 +13,7 @@ import Date exposing (Date) import String exposing (append) import Model exposing (Model) +import Model.User exposing (getUserName) import Model.Payment exposing (..) import Model.View.PaymentView exposing (PaymentView) import Model.Translations exposing (getMessage) @@ -55,7 +56,13 @@ paymentLine model paymentView (id, payment) = ] [ div [ class "cell category" ] [ text payment.name ] , div [ class "cell cost" ] [ text ((toString payment.cost) ++ " " ++ (getMessage "MoneySymbol" model.translations)) ] - , div [ class "cell user" ] [ text payment.userName ] + , div + [ class "cell user" ] + [ payment.userId + |> getUserName paymentView.users + |> Maybe.withDefault "−" + |> text + ] , div [ class "cell date" ] [ span @@ -65,11 +72,11 @@ paymentLine model paymentView (id, payment) = [ class "longDate" ] [ text (renderLongDate payment.creation model.translations) ] ] - , if paymentView.userName == payment.userName + , if paymentView.me == payment.userId then div [ class "cell remove" - , onClick serverCommunications.address (SC.DeletePayment id payment.userName payment.cost paymentView.currentPage) + , onClick serverCommunications.address (SC.DeletePayment id payment.userId payment.cost paymentView.currentPage) ] [ renderIcon "times" ] else -- cgit v1.2.3