diff options
author | Joris | 2016-03-30 00:28:55 +0200 |
---|---|---|
committer | Joris | 2016-03-30 00:28:55 +0200 |
commit | baefda5a902a94cedf84cfcd2ae550267e5d932e (patch) | |
tree | 72fa7b4e6c49d025563e3d7cba1ec13af43aa1c2 /src/client | |
parent | 76f8b85eb9f796d6df861a04f702ef5f48630795 (diff) |
Merge punctual and monthly payments in client model
Diffstat (limited to 'src/client')
-rw-r--r-- | src/client/elm/LoggedData.elm | 2 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Action.elm | 10 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/AddPayment/Action.elm | 4 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/AddPayment/Model.elm | 6 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/AddPayment/Update.elm | 2 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/AddPayment/View.elm | 2 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/Model.elm | 2 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/View/Monthly.elm | 10 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/View/Paging.elm | 4 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Home/View/Table.elm | 1 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Model.elm | 2 | ||||
-rw-r--r-- | src/client/elm/LoggedIn/Update.elm | 26 | ||||
-rw-r--r-- | src/client/elm/Model/Init.elm | 1 | ||||
-rw-r--r-- | src/client/elm/Model/Payment.elm | 32 | ||||
-rw-r--r-- | src/client/elm/Server.elm | 5 |
15 files changed, 62 insertions, 47 deletions
diff --git a/src/client/elm/LoggedData.elm b/src/client/elm/LoggedData.elm index 02e7908..a3cbec6 100644 --- a/src/client/elm/LoggedData.elm +++ b/src/client/elm/LoggedData.elm @@ -23,7 +23,6 @@ type alias LoggedData = , users : Users , me : UserId , payments : Payments - , monthlyPayments : Payments , incomes : Incomes } @@ -35,6 +34,5 @@ build model loggedIn = , users = loggedIn.users , me = loggedIn.me , payments = loggedIn.payments - , monthlyPayments = loggedIn.monthlyPayments , incomes = loggedIn.incomes } diff --git a/src/client/elm/LoggedIn/Action.elm b/src/client/elm/LoggedIn/Action.elm index ae72dea..93bb04d 100644 --- a/src/client/elm/LoggedIn/Action.elm +++ b/src/client/elm/LoggedIn/Action.elm @@ -2,7 +2,7 @@ module LoggedIn.Action ( Action(..) ) where -import Model.Payment exposing (Payment, PaymentId, PaymentFrequency) +import Model.Payment exposing (Payment, PaymentId, Frequency) import Model.Income exposing (IncomeId) import LoggedIn.Home.Action as HomeAction @@ -10,9 +10,9 @@ import LoggedIn.Home.Action as HomeAction type Action = NoOp | HomeAction HomeAction.Action - | AddPayment String String PaymentFrequency - | ValidateAddPayment PaymentId String Int PaymentFrequency - | DeletePayment Payment PaymentFrequency - | ValidateDeletePayment Payment PaymentFrequency + | AddPayment String String Frequency + | ValidateAddPayment PaymentId String Int Frequency + | DeletePayment Payment Frequency + | ValidateDeletePayment Payment Frequency | UpdateIncome Int | ValidateUpdateIncome IncomeId Int diff --git a/src/client/elm/LoggedIn/Home/AddPayment/Action.elm b/src/client/elm/LoggedIn/Home/AddPayment/Action.elm index 81c32e2..a692b15 100644 --- a/src/client/elm/LoggedIn/Home/AddPayment/Action.elm +++ b/src/client/elm/LoggedIn/Home/AddPayment/Action.elm @@ -2,11 +2,11 @@ module LoggedIn.Home.AddPayment.Action ( Action(..) ) where -import Model.Payment exposing (PaymentFrequency) +import Model.Payment exposing (Frequency) type Action = NoOp - | Init PaymentFrequency + | Init Frequency | UpdateName String | UpdateCost String | AddError (Maybe String) (Maybe String) diff --git a/src/client/elm/LoggedIn/Home/AddPayment/Model.elm b/src/client/elm/LoggedIn/Home/AddPayment/Model.elm index 22b6883..19933fd 100644 --- a/src/client/elm/LoggedIn/Home/AddPayment/Model.elm +++ b/src/client/elm/LoggedIn/Home/AddPayment/Model.elm @@ -7,18 +7,18 @@ import Result as Result exposing (Result(..)) import Json.Decode exposing ((:=)) import Model.Translations exposing (..) -import Model.Payment exposing (PaymentFrequency(..)) +import Model.Payment exposing (Frequency(..)) type alias Model = { name : String , nameError : Maybe String , cost : String , costError : Maybe String - , frequency : PaymentFrequency + , frequency : Frequency , waitingServer : Bool } -init : PaymentFrequency -> Model +init : Frequency -> Model init frequency = { name = "" , nameError = Nothing diff --git a/src/client/elm/LoggedIn/Home/AddPayment/Update.elm b/src/client/elm/LoggedIn/Home/AddPayment/Update.elm index 473e998..7f5fb0a 100644 --- a/src/client/elm/LoggedIn/Home/AddPayment/Update.elm +++ b/src/client/elm/LoggedIn/Home/AddPayment/Update.elm @@ -10,7 +10,7 @@ import LoggedIn.Home.AddPayment.Action as AddPaymentAction import LoggedIn.Home.AddPayment.Model as AddPaymentModel import Model.Translations exposing (Translations, getMessage) -import Model.Payment exposing (PaymentFrequency(..)) +import Model.Payment exposing (Frequency(..)) update : AddPaymentAction.Action -> AddPaymentModel.Model -> AddPaymentModel.Model update action addPayment = diff --git a/src/client/elm/LoggedIn/Home/AddPayment/View.elm b/src/client/elm/LoggedIn/Home/AddPayment/View.elm index 47dd6e8..562f69b 100644 --- a/src/client/elm/LoggedIn/Home/AddPayment/View.elm +++ b/src/client/elm/LoggedIn/Home/AddPayment/View.elm @@ -16,7 +16,7 @@ import LoggedIn.Home.Model as HomeModel import LoggedIn.Home.AddPayment.Action as AddPaymentAction import LoggedIn.Home.AddPayment.Model as AddPaymentModel -import Model.Payment exposing (PaymentFrequency(..)) +import Model.Payment exposing (Frequency(..)) import Model.Translations exposing (getMessage) import LoggedData exposing (LoggedData) diff --git a/src/client/elm/LoggedIn/Home/Model.elm b/src/client/elm/LoggedIn/Home/Model.elm index 2bcbe9a..cd8b4d0 100644 --- a/src/client/elm/LoggedIn/Home/Model.elm +++ b/src/client/elm/LoggedIn/Home/Model.elm @@ -6,7 +6,7 @@ module LoggedIn.Home.Model import LoggedIn.Home.Model.Payer exposing (Payers) import Model.User exposing (Users, UserId) -import Model.Payment exposing (PaymentId, Payments, PaymentFrequency(..)) +import Model.Payment exposing (PaymentId, Payments, Frequency(..)) import LoggedIn.Home.Account.Model as AccountModel import LoggedIn.Home.AddPayment.Model as AddPaymentModel diff --git a/src/client/elm/LoggedIn/Home/View/Monthly.elm b/src/client/elm/LoggedIn/Home/View/Monthly.elm index 487c05d..c1f3b51 100644 --- a/src/client/elm/LoggedIn/Home/View/Monthly.elm +++ b/src/client/elm/LoggedIn/Home/View/Monthly.elm @@ -15,7 +15,7 @@ import LoggedIn.Home.Model as HomeModel import LoggedIn.Home.View.Price exposing (price) import LoggedIn.Home.View.Expand exposing (..) -import Model.Payment as Payment exposing (Payment) +import Model.Payment as Payment exposing (Payment, monthlyPayments) import Model.Translations exposing (getMessage, getParamMessage) import LoggedData exposing (LoggedData) @@ -26,7 +26,7 @@ import View.Icon exposing (renderIcon) view : LoggedData -> HomeModel.Model -> Html view loggedData homeModel = - if List.length loggedData.monthlyPayments == 0 + if List.length (monthlyPayments loggedData.payments) == 0 then text "" else @@ -44,8 +44,8 @@ view loggedData homeModel = monthlyCount : LoggedData -> HomeModel.Model -> Html monthlyCount loggedData homeModel = - let count = List.length loggedData.monthlyPayments - total = List.sum << List.map .cost <| loggedData.monthlyPayments + let count = List.length (monthlyPayments loggedData.payments) + total = List.sum << List.map .cost <| (monthlyPayments loggedData.payments) key = if count > 1 then "PluralMonthlyCount" else "SingularMonthlyCount" in button [ class "header" @@ -59,7 +59,7 @@ paymentsTable : LoggedData -> HomeModel.Model -> Html paymentsTable loggedData homeModel = div [ class "table" ] - ( loggedData.monthlyPayments + ( (monthlyPayments loggedData.payments) |> List.sortBy (String.toLower << .name) |> List.map (paymentLine loggedData homeModel) ) diff --git a/src/client/elm/LoggedIn/Home/View/Paging.elm b/src/client/elm/LoggedIn/Home/View/Paging.elm index 09a5b62..8433494 100644 --- a/src/client/elm/LoggedIn/Home/View/Paging.elm +++ b/src/client/elm/LoggedIn/Home/View/Paging.elm @@ -15,7 +15,7 @@ import LoggedIn.Home.Model as HomeModel import Action exposing (Action) import LoggedData exposing (LoggedData) -import Model.Payment exposing (Payments, perPage) +import Model.Payment exposing (Payments, perPage, punctualPayments) import View.Icon exposing (renderIcon) @@ -24,7 +24,7 @@ showedPages = 5 paymentsPaging : Address Action -> Payments -> HomeModel.Model -> Html paymentsPaging address payments homeModel = - let maxPage = ceiling (toFloat (List.length payments) / toFloat perPage) + let maxPage = ceiling (toFloat (List.length (punctualPayments payments)) / toFloat perPage) pages = truncatePages homeModel.currentPage [1..maxPage] in if maxPage == 1 then diff --git a/src/client/elm/LoggedIn/Home/View/Table.elm b/src/client/elm/LoggedIn/Home/View/Table.elm index 58f2d0b..c73b428 100644 --- a/src/client/elm/LoggedIn/Home/View/Table.elm +++ b/src/client/elm/LoggedIn/Home/View/Table.elm @@ -46,6 +46,7 @@ headerLine loggedData = paymentLines : LoggedData -> HomeModel.Model -> List Html paymentLines loggedData homeModel = loggedData.payments + |> punctualPayments |> List.sortBy (Date.toTime << .creation) |> List.reverse |> List.drop ((homeModel.currentPage - 1) * perPage) diff --git a/src/client/elm/LoggedIn/Model.elm b/src/client/elm/LoggedIn/Model.elm index 90f1b2b..cc1ade7 100644 --- a/src/client/elm/LoggedIn/Model.elm +++ b/src/client/elm/LoggedIn/Model.elm @@ -17,7 +17,6 @@ type alias Model = , users : Users , me : UserId , payments : Payments - , monthlyPayments : Payments , incomes : Incomes } @@ -27,6 +26,5 @@ init initData = , users = initData.users , me = initData.me , payments = initData.payments - , monthlyPayments = initData.monthlyPayments , incomes = initData.incomes } diff --git a/src/client/elm/LoggedIn/Update.elm b/src/client/elm/LoggedIn/Update.elm index 8fe8d4c..fd141c8 100644 --- a/src/client/elm/LoggedIn/Update.elm +++ b/src/client/elm/LoggedIn/Update.elm @@ -12,7 +12,7 @@ import Date import Model exposing (Model) import Model.Translations exposing (getMessage) -import Model.Payment exposing (Payment, PaymentFrequency(..), deletePayment) +import Model.Payment exposing (Payment, Frequency(..), deletePayment) import Server import LoggedData @@ -74,14 +74,15 @@ update model action loggedIn = LoggedInAction.ValidateAddPayment paymentId name cost frequency -> update model (LoggedInAction.HomeAction <| HomeAction.UpdateAdd <| AddPaymentAction.Init frequency) loggedIn |> flip Effects.andThen (\loggedIn -> - let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedIn.me - in case frequency of - Punctual -> - update model (LoggedInAction.HomeAction <| HomeAction.UpdatePage 1) loggedIn - |> Tuple.mapFst (\loggedIn -> { loggedIn | payments = newPayment :: loggedIn.payments }) - Monthly -> - update model (LoggedInAction.HomeAction <| HomeAction.ShowMonthlyDetail) loggedIn - |> Tuple.mapFst (\loggedIn -> { loggedIn | monthlyPayments = newPayment :: loggedIn.monthlyPayments }) + case frequency of + Punctual -> + update model (LoggedInAction.HomeAction <| HomeAction.UpdatePage 1) loggedIn + Monthly -> + update model (LoggedInAction.HomeAction <| HomeAction.ShowMonthlyDetail) loggedIn + ) + |> Tuple.mapFst (\loggedIn -> + let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedIn.me frequency + in { loggedIn | payments = newPayment :: loggedIn.payments } ) LoggedInAction.DeletePayment payment frequency -> @@ -93,15 +94,10 @@ update model action loggedIn = ) LoggedInAction.ValidateDeletePayment payment frequency -> - ( case frequency of - Monthly -> - { loggedIn | monthlyPayments = deletePayment payment.id loggedIn.monthlyPayments } - Punctual -> - { loggedIn | payments = deletePayment payment.id loggedIn.payments } + ( { loggedIn | payments = deletePayment payment.id loggedIn.payments } , Effects.none ) - LoggedInAction.UpdateIncome amount -> ( loggedIn , Server.setIncome amount diff --git a/src/client/elm/Model/Init.elm b/src/client/elm/Model/Init.elm index 7028427..7fccf00 100644 --- a/src/client/elm/Model/Init.elm +++ b/src/client/elm/Model/Init.elm @@ -10,6 +10,5 @@ type alias Init = { users : Users , me : UserId , payments : Payments - , monthlyPayments : Payments , incomes : Incomes } diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm index 80579e2..e792c6c 100644 --- a/src/client/elm/Model/Payment.elm +++ b/src/client/elm/Model/Payment.elm @@ -3,11 +3,13 @@ module Model.Payment , Payments , Payment , PaymentId + , Frequency(..) , paymentsDecoder , paymentIdDecoder , deletePayment - , PaymentFrequency(..) , totalPayments + , punctualPayments + , monthlyPayments ) where import Date exposing (..) @@ -27,33 +29,55 @@ type alias Payment = , name : String , cost : Int , userId : UserId + , frequency : Frequency } type alias PaymentId = Int -type PaymentFrequency = Punctual | Monthly +type Frequency = Punctual | Monthly paymentsDecoder : Json.Decoder Payments paymentsDecoder = Json.list paymentDecoder paymentDecoder : Json.Decoder Payment paymentDecoder = - Json.object5 Payment + Json.object6 Payment ("id" := paymentIdDecoder) ("creation" := dateDecoder) ("name" := Json.string) ("cost" := Json.int) ("userId" := userIdDecoder) + ("frequency" := frequencyDecoder) paymentIdDecoder : Json.Decoder PaymentId paymentIdDecoder = Json.int +frequencyDecoder : Json.Decoder Frequency +frequencyDecoder = + Json.customDecoder + Json.string + (\input -> case input of + "Punctual" -> Ok Punctual + "Monthly" -> Ok Monthly + _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input) + ) + deletePayment : PaymentId -> Payments -> Payments deletePayment paymentId = List.filter (((/=) paymentId) << .id) totalPayments : (Payment -> Bool) -> UserId -> Payments -> Int totalPayments paymentFilter userId payments = payments - |> List.filter (\payment -> paymentFilter payment && payment.userId == userId) + |> List.filter (\payment -> + paymentFilter payment + && payment.userId == userId + && payment.frequency == Punctual + ) |> List.map .cost |> List.sum + +punctualPayments : Payments -> Payments +punctualPayments = List.filter ((==) Punctual << .frequency) + +monthlyPayments : Payments -> Payments +monthlyPayments = List.filter ((==) Monthly << .frequency) diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm index 99ce938..ad6d212 100644 --- a/src/client/elm/Server.elm +++ b/src/client/elm/Server.elm @@ -25,7 +25,6 @@ init = Task.map Init (Http.get usersDecoder "/api/users") `Task.andMap` (Http.get ("id" := userIdDecoder) "/api/whoAmI") `Task.andMap` (Http.get paymentsDecoder "/api/payments") - `Task.andMap` (Http.get paymentsDecoder "/api/monthlyPayments") `Task.andMap` (Http.get incomesDecoder "/api/incomes") signIn : String -> Task Http.Error () @@ -33,12 +32,12 @@ signIn email = post ("/api/signIn?email=" ++ email) |> Task.map (always ()) -addPayment : String -> String -> PaymentFrequency -> Task Http.Error PaymentId +addPayment : String -> String -> Frequency -> Task Http.Error PaymentId addPayment name cost frequency = post ("/api/payment/add?name=" ++ name ++ "&cost=" ++ cost ++ "&frequency=" ++ (toString frequency)) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) -deletePayment : Payment -> PaymentFrequency -> Task Http.Error () +deletePayment : Payment -> Frequency -> Task Http.Error () deletePayment payment frequency = post ("/api/payment/delete?id=" ++ (toString payment.id)) |> Task.map (always ()) |