aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorJoris2015-09-06 00:05:50 +0200
committerJoris2015-09-06 00:05:50 +0200
commit24633871359ec9fbd63fdfebf79a6351b2792f77 (patch)
treea87c8a964a3c5114da13e622c604cf99ab905a06 /src/client
parent8c328987901973cd0ffd2e03cae547717ebbbc67 (diff)
Can add monthly payments, not visible at the moment though, just the count is printed
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Main.elm14
-rw-r--r--src/client/Model/Payment.elm22
-rw-r--r--src/client/Model/View/LoggedView.elm8
-rw-r--r--src/client/Model/View/Payment/Add.elm6
-rw-r--r--src/client/ServerCommunication.elm9
-rw-r--r--src/client/Update.elm6
-rw-r--r--src/client/Update/Payment.elm2
-rw-r--r--src/client/View/Payments.elm2
-rw-r--r--src/client/View/Payments/Add.elm2
-rw-r--r--src/client/View/Payments/Monthly.elm24
-rw-r--r--src/client/View/Payments/Table.elm13
11 files changed, 67 insertions, 41 deletions
diff --git a/src/client/Main.elm b/src/client/Main.elm
index 368d930..3174ba6 100644
--- a/src/client/Main.elm
+++ b/src/client/Main.elm
@@ -57,9 +57,14 @@ port initView =
Just msg ->
Signal.send actions.address (SignInError msg)
Nothing ->
- Task.map5 GoLoggedView getUsers whoAmI getPayments getPaymentsCount getPayers
- |> flip Task.andThen (Signal.send actions.address)
- |> flip Task.onError (\_ -> Signal.send actions.address GoSignInView)
+ Task.andThen getUsers <| \users ->
+ Task.andThen whoAmI <| \me ->
+ Task.andThen getMonthlyPayments <| \monthlyPayments ->
+ Task.andThen getPayments <| \payments ->
+ Task.andThen getPaymentsCount <| \paymentsCount ->
+ Task.andThen getPayers <| \payers ->
+ Signal.send actions.address (GoLoggedView users me monthlyPayments payments paymentsCount payers)
+ |> flip Task.onError (\_ -> Signal.send actions.address GoSignInView)
getUsers : Task Http.Error Users
getUsers = Http.get usersDecoder "/users"
@@ -67,6 +72,9 @@ getUsers = Http.get usersDecoder "/users"
whoAmI : Task Http.Error UserId
whoAmI = Http.get ("id" := userIdDecoder) "/whoAmI"
+getMonthlyPayments : Task Http.Error Payments
+getMonthlyPayments = Http.get paymentsDecoder "/monthlyPayments"
+
getPayments : Task Http.Error Payments
getPayments = Http.get paymentsDecoder ("/payments?page=1&perPage=" ++ toString perPage)
diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm
index 4ae50de..313c6be 100644
--- a/src/client/Model/Payment.elm
+++ b/src/client/Model/Payment.elm
@@ -3,26 +3,22 @@ module Model.Payment
, Payments
, Payment
, PaymentId
- , PaymentWithId
, paymentsDecoder
- , removePayment
) where
import Date exposing (..)
import Json.Decode as Json exposing ((:=))
-import Dict exposing (..)
import Model.User exposing (UserId, userIdDecoder)
perPage : Int
perPage = 8
-type alias Payments = Dict PaymentId Payment
-
-type alias PaymentWithId = (PaymentId, Payment)
+type alias Payments = List Payment
type alias Payment =
- { creation : Date
+ { id : PaymentId
+ , creation : Date
, name : String
, cost : Int
, userId : UserId
@@ -31,15 +27,12 @@ type alias Payment =
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" := paymentIdDecoder))
+paymentsDecoder = Json.list paymentDecoder
paymentDecoder : Json.Decoder Payment
paymentDecoder =
- Json.object4 Payment
+ Json.object5 Payment
+ ("id" := paymentIdDecoder)
("creation" := dateDecoder)
("name" := Json.string)
("cost" := Json.int)
@@ -50,6 +43,3 @@ paymentIdDecoder = Json.int
dateDecoder : Json.Decoder Date
dateDecoder = Json.customDecoder Json.string Date.fromString
-
-removePayment : Payments -> PaymentId -> Payments
-removePayment payments paymentId = Dict.remove paymentId payments
diff --git a/src/client/Model/View/LoggedView.elm b/src/client/Model/View/LoggedView.elm
index 35fd9e5..34a55a2 100644
--- a/src/client/Model/View/LoggedView.elm
+++ b/src/client/Model/View/LoggedView.elm
@@ -13,6 +13,7 @@ type alias LoggedView =
{ users : Users
, me : UserId
, add : AddPayment
+ , monthlyPayments : Payments
, payments : Payments
, paymentsCount : Int
, payers : Payers
@@ -20,11 +21,12 @@ type alias LoggedView =
, currentPage : Int
}
-initLoggedView : Users -> UserId -> Payments -> Int -> Payers -> LoggedView
-initLoggedView users me payments paymentsCount payers =
+initLoggedView : Users -> UserId -> Payments -> Payments -> Int -> Payers -> LoggedView
+initLoggedView users me monthlyPayments payments paymentsCount payers =
{ users = users
, me = me
- , add = initAddPayment
+ , add = initAddPayment Punctual
+ , monthlyPayments = monthlyPayments
, payments = payments
, paymentsCount = paymentsCount
, payers = payers
diff --git a/src/client/Model/View/Payment/Add.elm b/src/client/Model/View/Payment/Add.elm
index da5d0f2..dc00e86 100644
--- a/src/client/Model/View/Payment/Add.elm
+++ b/src/client/Model/View/Payment/Add.elm
@@ -20,13 +20,13 @@ type alias AddPayment =
, frequency : Frequency
}
-initAddPayment : AddPayment
-initAddPayment =
+initAddPayment : Frequency -> AddPayment
+initAddPayment frequency =
{ name = ""
, nameError = Nothing
, cost = ""
, costError = Nothing
- , frequency = Punctual
+ , frequency = frequency
}
validateName : String -> Translations -> Result String String
diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm
index c38805b..1f35fa1 100644
--- a/src/client/ServerCommunication.elm
+++ b/src/client/ServerCommunication.elm
@@ -13,6 +13,7 @@ import Date
import Model.Message exposing (messageDecoder)
import Model.User exposing (UserId)
import Model.Payment exposing (PaymentId, perPage, paymentsDecoder)
+import Model.View.Payment.Add exposing (Frequency)
import Update as U
import Update.SignIn exposing (..)
@@ -21,7 +22,7 @@ import Update.Payment as UP
type Communication =
NoCommunication
| SignIn String
- | AddPayment UserId String Int
+ | AddPayment UserId String Int Frequency
| DeletePayment PaymentId UserId Int Int
| UpdatePage Int
| SignOut
@@ -45,8 +46,8 @@ getRequest communication =
Nothing
SignIn login ->
Just (simple "post" ("/signIn?login=" ++ login))
- AddPayment userId paymentName cost ->
- Just (simple "post" ("/payment/add?name=" ++ paymentName ++ "&cost=" ++ (toString cost)))
+ AddPayment userId paymentName cost frequency ->
+ Just (simple "post" ("/payment/add?name=" ++ paymentName ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)))
DeletePayment paymentId _ _ _ ->
Just (simple "post" ("payment/delete?id=" ++ (toString paymentId)))
UpdatePage page ->
@@ -75,7 +76,7 @@ serverResult communication response =
Task.succeed U.NoOp
SignIn login ->
Task.succeed (U.UpdateSignIn (ValidLogin login))
- AddPayment userId paymentName cost ->
+ AddPayment userId paymentName cost frequency ->
Http.send Http.defaultSettings (updatePageRequest 1)
|> Task.map (\response ->
if response.status == 200
diff --git a/src/client/Update.elm b/src/client/Update.elm
index d643d4e..08547e3 100644
--- a/src/client/Update.elm
+++ b/src/client/Update.elm
@@ -23,7 +23,7 @@ type Action =
| GoSignInView
| SignInError String
| UpdateSignIn SignInAction
- | GoLoggedView Users UserId Payments Int Payers
+ | GoLoggedView Users UserId Payments Payments Int Payers
| UpdatePayment PaymentAction
actions : Signal.Mailbox Action
@@ -38,8 +38,8 @@ updateModel action model =
{ model | currentTime <- time }
GoSignInView ->
{ model | view <- V.SignInView initSignInView }
- GoLoggedView users me payments paymentsCount payers ->
- { model | view <- V.LoggedView (initLoggedView users me payments paymentsCount payers) }
+ GoLoggedView users me monthlyPayments payments paymentsCount payers ->
+ { model | view <- V.LoggedView (initLoggedView users me monthlyPayments 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 f063b4c..2cae679 100644
--- a/src/client/Update/Payment.elm
+++ b/src/client/Update/Payment.elm
@@ -34,7 +34,7 @@ updatePayment model action loggedView =
{ loggedView
| payments <- payments
, currentPage <- 1
- , add <- initAddPayment
+ , add <- initAddPayment loggedView.add.frequency
, payers <- updatePayers loggedView.payers userId cost
, paymentsCount <- loggedView.paymentsCount + 1
}
diff --git a/src/client/View/Payments.elm b/src/client/View/Payments.elm
index b51c9a0..ac19df7 100644
--- a/src/client/View/Payments.elm
+++ b/src/client/View/Payments.elm
@@ -11,6 +11,7 @@ import Model.View.LoggedView exposing (LoggedView)
import View.Payments.ExceedingPayer exposing (exceedingPayers)
import View.Payments.Add exposing (addPayment)
+import View.Payments.Monthly exposing (monthlyPayments)
import View.Payments.Table exposing (paymentsTable)
import View.Payments.Paging exposing (paymentsPaging)
@@ -20,6 +21,7 @@ renderPayments model loggedView =
[ class "payments" ]
[ exceedingPayers model loggedView
, addPayment model loggedView
+ , monthlyPayments model loggedView
, paymentsTable model loggedView
, paymentsPaging loggedView
]
diff --git a/src/client/View/Payments/Add.elm b/src/client/View/Payments/Add.elm
index 085b16d..a22c1f1 100644
--- a/src/client/View/Payments/Add.elm
+++ b/src/client/View/Payments/Add.elm
@@ -31,7 +31,7 @@ addPayment model loggedView =
[ class "add"
, case (validateName loggedView.add.name model.translations, validateCost loggedView.add.cost model.translations) of
(Ok name, Ok cost) ->
- onSubmitPrevDefault serverCommunications.address (SC.AddPayment loggedView.me name cost)
+ onSubmitPrevDefault serverCommunications.address (SC.AddPayment loggedView.me name cost loggedView.add.frequency)
(resName, resCost) ->
onSubmitPrevDefault actions.address (UpdatePayment <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost))
]
diff --git a/src/client/View/Payments/Monthly.elm b/src/client/View/Payments/Monthly.elm
new file mode 100644
index 0000000..366af92
--- /dev/null
+++ b/src/client/View/Payments/Monthly.elm
@@ -0,0 +1,24 @@
+module View.Payments.Monthly
+ ( monthlyPayments
+ ) where
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+
+import Model exposing (Model)
+import Model.Payment exposing (Payments)
+import Model.View.LoggedView exposing (LoggedView)
+import Model.Translations exposing (getVarMessage)
+
+monthlyPayments : Model -> LoggedView -> Html
+monthlyPayments model loggedView =
+ div
+ [ class "monthlyPayments" ]
+ [ monthlyCount model loggedView.monthlyPayments ]
+
+monthlyCount : Model -> Payments -> Html
+monthlyCount model monthlyPayments =
+ let count = List.length monthlyPayments
+ key = if count > 1 then "PluralMonthlyCount" else "SingularMonthlyCount"
+ in text (getVarMessage [toString count] key model.translations)
diff --git a/src/client/View/Payments/Table.elm b/src/client/View/Payments/Table.elm
index 5374c44..4a1ed50 100644
--- a/src/client/View/Payments/Table.elm
+++ b/src/client/View/Payments/Table.elm
@@ -43,16 +43,15 @@ paymentsTable model loggedView =
paymentLines : Model -> LoggedView -> List Html
paymentLines model loggedView =
loggedView.payments
- |> Dict.toList
- |> List.sortBy (\(_, payment) -> Date.toTime payment.creation)
+ |> List.sortBy (Date.toTime << .creation)
|> List.reverse
|> List.map (paymentLine model loggedView)
-paymentLine : Model -> LoggedView -> PaymentWithId -> Html
-paymentLine model loggedView (id, payment) =
+paymentLine : Model -> LoggedView -> Payment -> Html
+paymentLine model loggedView payment =
a
- [ class ("row " ++ (if loggedView.edition == Just id then "edition" else ""))
- , onClick actions.address (UpdatePayment (ToggleEdit id))
+ [ class ("row " ++ (if loggedView.edition == Just payment.id then "edition" else ""))
+ , onClick actions.address (UpdatePayment (ToggleEdit payment.id))
]
[ div [ class "cell category" ] [ text payment.name ]
, div [ class "cell cost" ] [ text ((toString payment.cost) ++ " " ++ (getMessage "MoneySymbol" model.translations)) ]
@@ -76,7 +75,7 @@ paymentLine model loggedView (id, payment) =
then
div
[ class "cell remove"
- , onClick serverCommunications.address (SC.DeletePayment id payment.userId payment.cost loggedView.currentPage)
+ , onClick serverCommunications.address (SC.DeletePayment payment.id payment.userId payment.cost loggedView.currentPage)
]
[ renderIcon "times" ]
else