From b73ba24f3440b81698c9d5c370739d03f958f059 Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 31 Dec 2015 19:34:29 +0100 Subject: Fetch all the payments, do the paging only in the UI --- src/client/elm/InitViewAction.elm | 4 ++-- src/client/elm/ServerCommunication.elm | 15 ++------------- src/client/elm/Update/LoggedIn.elm | 19 ++++++++----------- src/client/elm/View/LoggedIn/Paging.elm | 14 ++++++-------- src/client/elm/View/LoggedIn/Table.elm | 2 ++ 5 files changed, 20 insertions(+), 34 deletions(-) (limited to 'src/client/elm') diff --git a/src/client/elm/InitViewAction.elm b/src/client/elm/InitViewAction.elm index 7c353a7..cdfc0fc 100644 --- a/src/client/elm/InitViewAction.elm +++ b/src/client/elm/InitViewAction.elm @@ -8,7 +8,7 @@ import Json.Decode as Json exposing ((:=)) import Update exposing (Action(GoLoggedInView, GoSignInView)) -import Model.Payment exposing (Payments, paymentsDecoder, perPage) +import Model.Payment exposing (Payments, paymentsDecoder) import Model.Payer exposing (Payers, payersDecoder) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) @@ -20,6 +20,6 @@ loggedInView = Task.map GoLoggedInView (Http.get usersDecoder "/users") `Task.andMap` (Http.get ("id" := userIdDecoder) "/whoAmI") `Task.andMap` (Http.get paymentsDecoder "/monthlyPayments") - `Task.andMap` (Http.get paymentsDecoder ("/payments?page=1&perPage=" ++ toString perPage)) + `Task.andMap` (Http.get paymentsDecoder "/payments") `Task.andMap` (Http.get ("number" := Json.int) "/payments/count") `Task.andMap` (Http.get payersDecoder "/payers") diff --git a/src/client/elm/ServerCommunication.elm b/src/client/elm/ServerCommunication.elm index 3ecfc3b..390bcfd 100644 --- a/src/client/elm/ServerCommunication.elm +++ b/src/client/elm/ServerCommunication.elm @@ -36,7 +36,6 @@ type Communication = | SetIncome Time Int | DeletePayment Payment Int | DeleteMonthlyPayment PaymentId - | UpdatePage Int | SignOut serverCommunications : Signal.Mailbox Communication @@ -59,8 +58,7 @@ sendRequest communication = AddPayment userId name cost -> post (addPaymentURL name cost Punctual) - |> flip Task.andThen (always (getPaymentsAtPage 1)) - |> Task.map (\payments -> U.UpdateLoggedIn (UL.AddPayment userId name cost payments)) + |> Task.map (always (U.UpdateLoggedIn (UL.AddPayment userId name cost))) AddMonthlyPayment name cost -> post (addPaymentURL name cost Monthly) @@ -69,17 +67,12 @@ sendRequest communication = DeletePayment payment currentPage -> post (deletePaymentURL payment.id) - |> flip Task.andThen (always (getPaymentsAtPage currentPage)) - |> Task.map (\payments -> U.UpdateLoggedIn (UL.DeletePayment payment payments)) + |> Task.map (always (U.UpdateLoggedIn (UL.DeletePayment payment))) DeleteMonthlyPayment id -> post (deletePaymentURL id) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateMonthly (UM.DeletePayment id)))) - UpdatePage page -> - getPaymentsAtPage page - |> flip Task.andThen (Task.succeed << U.UpdateLoggedIn << UL.UpdatePage page) - SetIncome currentTime amount -> post ("/income?amount=" ++ (toString amount)) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateAccount (UA.UpdateIncome currentTime amount)))) @@ -88,10 +81,6 @@ sendRequest communication = post "/signOut" |> Task.map (always U.GoSignInView) -getPaymentsAtPage : Int -> Task Http.Error Payments -getPaymentsAtPage page = - Http.get paymentsDecoder ("payments?page=" ++ toString page ++ "&perPage=" ++ toString perPage) - addPaymentURL : String -> Int -> Frequency -> String addPaymentURL name cost frequency = "/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency) diff --git a/src/client/elm/Update/LoggedIn.elm b/src/client/elm/Update/LoggedIn.elm index 5292c25..2dc65c3 100644 --- a/src/client/elm/Update/LoggedIn.elm +++ b/src/client/elm/Update/LoggedIn.elm @@ -19,11 +19,11 @@ import Update.LoggedIn.Account as UA type LoggedAction = UpdateAdd AddPaymentAction | UpdatePayments Payments - | AddPayment UserId String Int Payments + | AddPayment UserId String Int | AddMonthlyPayment PaymentId String Int | ToggleEdit PaymentId - | DeletePayment Payment Payments - | UpdatePage Int Payments + | DeletePayment Payment + | UpdatePage Int | UpdateMonthly UM.MonthlyAction | UpdateAccount UA.AccountAction @@ -34,10 +34,9 @@ updateLoggedIn model action loggedInView = { loggedInView | add = updateAddPayment addPaymentAction loggedInView.add } UpdatePayments payments -> { loggedInView | payments = payments } - AddPayment userId name cost payments -> + AddPayment userId name cost -> { loggedInView - | payments = payments - , currentPage = 1 + | currentPage = 1 , add = initAddPayment Punctual , account = UA.updateAccount (UA.UpdatePayer userId model.currentTime cost) loggedInView.account , paymentsCount = loggedInView.paymentsCount + 1 @@ -51,16 +50,14 @@ updateLoggedIn model action loggedInView = } ToggleEdit id -> { loggedInView | paymentEdition = if loggedInView.paymentEdition == Just id then Nothing else Just id } - DeletePayment payment payments -> + DeletePayment payment -> { loggedInView - | payments = payments - , account = UA.updateAccount (UA.UpdatePayer payment.userId (Date.toTime payment.creation) -payment.cost) loggedInView.account + | account = UA.updateAccount (UA.UpdatePayer payment.userId (Date.toTime payment.creation) -payment.cost) loggedInView.account , paymentsCount = loggedInView.paymentsCount - 1 } - UpdatePage page payments -> + UpdatePage page -> { loggedInView | currentPage = page - , payments = payments } UpdateMonthly monthlyAction -> { loggedInView | monthly = UM.updateMonthly monthlyAction loggedInView.monthly } diff --git a/src/client/elm/View/LoggedIn/Paging.elm b/src/client/elm/View/LoggedIn/Paging.elm index 608113b..e40c5aa 100644 --- a/src/client/elm/View/LoggedIn/Paging.elm +++ b/src/client/elm/View/LoggedIn/Paging.elm @@ -9,8 +9,6 @@ import Html.Events exposing (..) import Model.View.LoggedInView exposing (..) import Model.Payment exposing (perPage) -import ServerCommunication as SC exposing (serverCommunications) - import Update exposing (..) import Update.LoggedIn exposing (..) @@ -58,7 +56,7 @@ firstPage : Html firstPage = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage 1) + , onClick actions.address (UpdateLoggedIn (UpdatePage 1)) ] [ renderIcon "fast-backward" ] @@ -66,7 +64,7 @@ previousPage : LoggedInView -> Html previousPage loggedInView = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage - 1)) + , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage - 1))) ] [ renderIcon "backward" ] @@ -74,7 +72,7 @@ nextPage : LoggedInView -> Html nextPage loggedInView = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage + 1)) + , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage + 1))) ] [ renderIcon "forward" ] @@ -82,7 +80,7 @@ lastPage : Int -> Html lastPage maxPage = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage maxPage) + , onClick actions.address (UpdateLoggedIn (UpdatePage maxPage)) ] [ renderIcon "fast-forward" ] @@ -94,7 +92,7 @@ paymentsPage loggedInView page = [ ("page", True) , ("current", onCurrentPage) ] - , onClick serverCommunications.address <| - if onCurrentPage then SC.NoCommunication else SC.UpdatePage page + , onClick actions.address <| + if onCurrentPage then NoOp else UpdateLoggedIn (UpdatePage page) ] [ text (toString page) ] diff --git a/src/client/elm/View/LoggedIn/Table.elm b/src/client/elm/View/LoggedIn/Table.elm index f5a08b5..51a7b73 100644 --- a/src/client/elm/View/LoggedIn/Table.elm +++ b/src/client/elm/View/LoggedIn/Table.elm @@ -49,6 +49,8 @@ paymentLines model loggedInView = loggedInView.payments |> List.sortBy (Date.toTime << .creation) |> List.reverse + |> List.drop ((loggedInView.currentPage - 1) * perPage) + |> List.take perPage |> List.map (paymentLine model loggedInView) paymentLine : Model -> LoggedInView -> Payment -> Html -- cgit v1.2.3