From baefda5a902a94cedf84cfcd2ae550267e5d932e Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 30 Mar 2016 00:28:55 +0200 Subject: Merge punctual and monthly payments in client model --- src/client/elm/LoggedData.elm | 2 -- src/client/elm/LoggedIn/Action.elm | 10 +++---- src/client/elm/LoggedIn/Home/AddPayment/Action.elm | 4 +-- src/client/elm/LoggedIn/Home/AddPayment/Model.elm | 6 ++-- src/client/elm/LoggedIn/Home/AddPayment/Update.elm | 2 +- src/client/elm/LoggedIn/Home/AddPayment/View.elm | 2 +- src/client/elm/LoggedIn/Home/Model.elm | 2 +- src/client/elm/LoggedIn/Home/View/Monthly.elm | 10 +++---- src/client/elm/LoggedIn/Home/View/Paging.elm | 4 +-- src/client/elm/LoggedIn/Home/View/Table.elm | 1 + src/client/elm/LoggedIn/Model.elm | 2 -- src/client/elm/LoggedIn/Update.elm | 26 ++++++++---------- src/client/elm/Model/Init.elm | 1 - src/client/elm/Model/Payment.elm | 32 +++++++++++++++++++--- src/client/elm/Server.elm | 5 ++-- src/server/Controller/Payment.hs | 9 +----- src/server/Design/Header.hs | 5 +--- src/server/Main.hs | 2 -- src/server/Model/Frequency.hs | 9 +++++- src/server/Model/Json/Payment.hs | 2 ++ src/server/Model/Payment.hs | 23 ++++++---------- 21 files changed, 82 insertions(+), 77 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 ()) diff --git a/src/server/Controller/Payment.hs b/src/server/Controller/Payment.hs index a9d1acb..204794a 100644 --- a/src/server/Controller/Payment.hs +++ b/src/server/Controller/Payment.hs @@ -2,7 +2,6 @@ module Controller.Payment ( getPayments - , getMonthlyPayments , createPayment , deletePayment ) where @@ -32,13 +31,7 @@ import Model.Message.Key (Key(PaymentNotDeleted)) getPayments :: ActionM () getPayments = Secure.loggedAction (\_ -> do - (liftIO $ runDb P.getPunctualPayments) >>= json - ) - -getMonthlyPayments :: ActionM () -getMonthlyPayments = - Secure.loggedAction (\user -> do - (liftIO $ runDb (P.getUserMonthlyPayments (entityKey user))) >>= json + (liftIO $ runDb P.getPayments) >>= json ) createPayment :: Text -> Text -> Frequency -> ActionM () diff --git a/src/server/Design/Header.hs b/src/server/Design/Header.hs index e44bce7..c4f9332 100644 --- a/src/server/Design/Header.hs +++ b/src/server/Design/Header.hs @@ -49,8 +49,5 @@ headerDesign = ".user" <> ".icon" ? do paddingLeft headerPadding paddingRight headerPadding - borderLeft solid (px 1) darkenedRed - ".icon" ? do - fontSize iconFontSize - borderRight solid (px 1) darkenedRed + ".icon" ? fontSize iconFontSize diff --git a/src/server/Main.hs b/src/server/Main.hs index 1cb324e..9734781 100644 --- a/src/server/Main.hs +++ b/src/server/Main.hs @@ -76,8 +76,6 @@ api conf = do get "/api/payments" getPayments - get "/api/monthlyPayments" getMonthlyPayments - post "/api/payment/add" $ do name <- param "name" :: ActionM Text cost <- param "cost" :: ActionM Text diff --git a/src/server/Model/Frequency.hs b/src/server/Model/Frequency.hs index 2b747b7..a6ba55c 100644 --- a/src/server/Model/Frequency.hs +++ b/src/server/Model/Frequency.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} @@ -5,15 +6,21 @@ module Model.Frequency ( Frequency(..) ) where +import GHC.Generics + import Web.Scotty import Database.Persist.TH +import Data.Aeson + data Frequency = Punctual | Monthly - deriving (Eq, Show, Read) + deriving (Eq, Show, Read, Generic) derivePersistField "Frequency" instance Parsable Frequency where parseParam = readEither +instance FromJSON Frequency +instance ToJSON Frequency diff --git a/src/server/Model/Json/Payment.hs b/src/server/Model/Json/Payment.hs index 8923c13..7f4de15 100644 --- a/src/server/Model/Json/Payment.hs +++ b/src/server/Model/Json/Payment.hs @@ -11,6 +11,7 @@ import Data.Text (Text) import Data.Aeson import Model.Database (PaymentId, UserId) +import Model.Frequency data Payment = Payment { id :: PaymentId @@ -18,6 +19,7 @@ data Payment = Payment , name :: Text , cost :: Int , userId :: UserId + , frequency :: Frequency } deriving (Show, Generic) instance FromJSON Payment diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index 5c7d940..47397ff 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -1,8 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} module Model.Payment - ( getPunctualPayments - , getUserMonthlyPayments + ( getPayments , getMonthlyPayments , createPayment , deleteOwnPayment @@ -15,7 +14,6 @@ import Data.Either (lefts) import Control.Monad.IO.Class (liftIO) import Database.Persist -import qualified Database.Persist as P import qualified Validation @@ -24,23 +22,17 @@ import Model.Frequency import qualified Model.Json.Payment as P import qualified Model.Message.Key as K -getPunctualPayments :: Persist [P.Payment] -getPunctualPayments = +getPayments :: Persist [P.Payment] +getPayments = map getJsonPayment <$> selectList - [ PaymentDeletedAt P.==. Nothing - , PaymentFrequency P.==. Punctual - ] + [ PaymentDeletedAt ==. Nothing ] [ Desc PaymentCreation ] -getUserMonthlyPayments :: UserId -> Persist [P.Payment] -getUserMonthlyPayments userId = - filter ((==) userId . P.userId) . map getJsonPayment <$> getMonthlyPayments - getMonthlyPayments :: Persist [Entity Payment] getMonthlyPayments = selectList - [ PaymentDeletedAt P.==. Nothing - , PaymentFrequency P.==. Monthly + [ PaymentDeletedAt ==. Nothing + , PaymentFrequency ==. Monthly ] [ Desc PaymentName ] @@ -53,6 +45,7 @@ getJsonPayment paymentEntity = , P.name = paymentName payment , P.cost = paymentCost payment , P.userId = paymentUserId payment + , P.frequency = paymentFrequency payment } createPayment :: UserId -> Text -> Text -> Frequency -> Persist (Either [(Text, K.Key)] PaymentId) @@ -84,7 +77,7 @@ deleteOwnPayment user paymentId = do if paymentUserId payment == entityKey user then do now <- liftIO getCurrentTime - P.update paymentId [PaymentDeletedAt P.=. Just now] + update paymentId [PaymentDeletedAt =. Just now] return True else return False -- cgit v1.2.3