aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/elm/Dialog/AddPaymentButton/View.elm16
-rw-r--r--src/client/elm/Dialog/Model.elm18
-rw-r--r--src/client/elm/LoggedIn/Home/Header/View.elm1
-rw-r--r--src/client/elm/LoggedIn/Home/View/Table.elm14
-rw-r--r--src/client/elm/LoggedIn/Income/View.elm2
-rw-r--r--src/client/elm/LoggedIn/Msg.elm11
-rw-r--r--src/client/elm/LoggedIn/Update.elm40
-rw-r--r--src/client/elm/Model/Payment.elm10
-rw-r--r--src/client/elm/Server.elm35
-rw-r--r--src/client/elm/Utils/Form.elm8
-rw-r--r--src/client/elm/Utils/Http.elm30
11 files changed, 116 insertions, 69 deletions
diff --git a/src/client/elm/Dialog/AddPaymentButton/View.elm b/src/client/elm/Dialog/AddPaymentButton/View.elm
index 8014571..5da380c 100644
--- a/src/client/elm/Dialog/AddPaymentButton/View.elm
+++ b/src/client/elm/Dialog/AddPaymentButton/View.elm
@@ -31,11 +31,11 @@ import Dialog.Msg as DialogMsg
import LoggedData exposing (LoggedData)
import LoggedIn.Home.Model as HomeModel
-view : LoggedData -> List (String, Field) -> Html Msg -> Html Msg
-view loggedData initialForm content =
+view : LoggedData -> List (String, Field) -> String -> Html Msg -> Html Msg
+view loggedData initialForm title buttonContent =
let dialogConfig =
{ className = "paymentDialog"
- , title = getMessage "AddPayment" loggedData.translations
+ , title = getMessage title loggedData.translations
, body = \model -> addPaymentForm loggedData model.addPayment
, confirm = getMessage "Confirm" loggedData.translations
, confirmMsg = submitForm << .addPayment
@@ -45,7 +45,7 @@ view loggedData initialForm content =
[ class "addPayment"
, onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset initialForm))
]
- [ content ]
+ [ buttonContent ]
addPaymentForm : LoggedData -> Form String DialogModel.AddPayment -> Html Msg
addPaymentForm loggedData addPayment =
@@ -56,7 +56,7 @@ addPaymentForm loggedData addPayment =
]
[ htmlMap <| Form.textInput loggedData.translations addPayment "payment" "name"
, htmlMap <| Form.textInput loggedData.translations addPayment "payment" "cost"
- , if Form.frequency addPayment == Punctual
+ , if (Maybe.map .frequency <| Form.getOutput addPayment) == Just Punctual
then htmlMap <| Form.textInput loggedData.translations addPayment "payment" "date"
else text ""
, htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ]
@@ -67,6 +67,10 @@ submitForm : Form String DialogModel.AddPayment -> Msg
submitForm addPayment =
case Form.getOutput addPayment of
Just data ->
- Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.AddPayment data.name data.cost data.date data.frequency
+ case data.id of
+ Just paymentId ->
+ Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.EditPayment paymentId data.name data.cost data.date data.frequency
+ Nothing ->
+ Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.CreatePayment data.name data.cost data.date data.frequency
Nothing ->
Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg <| Form.Submit
diff --git a/src/client/elm/Dialog/Model.elm b/src/client/elm/Dialog/Model.elm
index 6256fea..b49d8f1 100644
--- a/src/client/elm/Dialog/Model.elm
+++ b/src/client/elm/Dialog/Model.elm
@@ -4,6 +4,7 @@ module Dialog.Model exposing
, init
, addPaymentInitial
, clonePaymentInitial
+ , editPaymentInitial
)
import Date exposing (Date)
@@ -14,7 +15,7 @@ import Form.Field as Field exposing (Field)
import Form.Validate as Validate exposing (Validation)
import Validation
-import Model.Payment as Payment exposing (Payment, Frequency)
+import Model.Payment as Payment exposing (Payment, Frequency, PaymentId)
import Model.Translations exposing (Translations)
type alias Model =
@@ -22,7 +23,8 @@ type alias Model =
}
type alias AddPayment =
- { name : String
+ { id : Maybe PaymentId
+ , name : String
, cost : Int
, date : Date
, frequency : Frequency
@@ -47,9 +49,19 @@ clonePaymentInitial translations date payment =
, ("frequency", Field.Radio (toString payment.frequency))
]
+editPaymentInitial : Translations -> Payment -> List (String, Field)
+editPaymentInitial translations payment =
+ [ ("id", Field.Text (toString payment.id))
+ , ("name", Field.Text payment.name)
+ , ("cost", Field.Text (toString payment.cost))
+ , ("date", Field.Text (Date.shortView payment.date translations))
+ , ("frequency", Field.Radio (toString payment.frequency))
+ ]
+
addPaymentValidation : Validation String AddPayment
addPaymentValidation =
- Validate.form4 AddPayment
+ Validate.form5 AddPayment
+ (Validate.get "id" (Validate.maybe Validate.int))
(Validate.get "name" (Validate.string `Validate.andThen` (Validate.nonEmpty)))
(Validate.get "cost" (Validate.int `Validate.andThen` (Validate.minInt 1)))
(Validate.get "date" Validation.date)
diff --git a/src/client/elm/LoggedIn/Home/Header/View.elm b/src/client/elm/LoggedIn/Home/Header/View.elm
index 753a120..b23e6fe 100644
--- a/src/client/elm/LoggedIn/Home/Header/View.elm
+++ b/src/client/elm/LoggedIn/Home/Header/View.elm
@@ -52,6 +52,7 @@ searchLine loggedData search frequency =
, AddPaymentButton.view
loggedData
(DialogModel.addPaymentInitial loggedData.translations currentDate frequency)
+ "AddPayment"
(text (getMessage "AddPayment" loggedData.translations))
]
diff --git a/src/client/elm/LoggedIn/Home/View/Table.elm b/src/client/elm/LoggedIn/Home/View/Table.elm
index d2087bb..ebffeb9 100644
--- a/src/client/elm/LoggedIn/Home/View/Table.elm
+++ b/src/client/elm/LoggedIn/Home/View/Table.elm
@@ -64,6 +64,7 @@ headerLine loggedData frequency =
Monthly -> text ""
, div [ class "cell" ] []
, div [ class "cell" ] []
+ , div [ class "cell" ] []
]
paymentLine : LoggedData -> HomeModel.Model -> Frequency -> Payment -> Html Msg
@@ -104,6 +105,7 @@ paymentLine loggedData homeModel frequency payment =
in AddPaymentButton.view
loggedData
(DialogModel.clonePaymentInitial loggedData.translations currentDate payment)
+ "ClonePayment"
(FontAwesome.clone Color.chestnutRose 18)
]
, div
@@ -112,6 +114,18 @@ paymentLine loggedData homeModel frequency payment =
then
text ""
else
+ AddPaymentButton.view
+ loggedData
+ (DialogModel.editPaymentInitial loggedData.translations payment)
+ "EditPayment"
+ (FontAwesome.edit Color.chestnutRose 18)
+ ]
+ , div
+ [ class "cell button" ]
+ [ if loggedData.me /= payment.userId
+ then
+ text ""
+ else
let dialogConfig =
{ className = "paymentDialog"
, title = getMessage "ConfirmPaymentDelete" loggedData.translations
diff --git a/src/client/elm/LoggedIn/Income/View.elm b/src/client/elm/LoggedIn/Income/View.elm
index 6466f70..9638ddc 100644
--- a/src/client/elm/LoggedIn/Income/View.elm
+++ b/src/client/elm/LoggedIn/Income/View.elm
@@ -86,7 +86,7 @@ addIncomeView loggedData addIncome =
[ class "add"
, case Form.getOutput addIncome of
Just data ->
- onClick (Msg.UpdateLoggedIn <| LoggedInMsg.AddIncome data.amount data.date)
+ onClick (Msg.UpdateLoggedIn <| LoggedInMsg.CreateIncome data.amount data.date)
Nothing ->
onClick (Msg.UpdateLoggedIn <| LoggedInMsg.IncomeMsg <| IncomeMsg.AddIncomeMsg <| Form.Submit)
]
diff --git a/src/client/elm/LoggedIn/Msg.elm b/src/client/elm/LoggedIn/Msg.elm
index cbae67f..77fb0ed 100644
--- a/src/client/elm/LoggedIn/Msg.elm
+++ b/src/client/elm/LoggedIn/Msg.elm
@@ -15,14 +15,17 @@ type Msg =
| HomeMsg HomeMsg.Msg
| IncomeMsg IncomeMsg.Msg
- | AddPayment String Int Date Frequency
- | ValidateAddPayment PaymentId String Int Date Frequency
+ | CreatePayment String Int Date Frequency
+ | ValidateCreatePayment PaymentId String Int Date Frequency
+
+ | EditPayment PaymentId String Int Date Frequency
+ | ValidateEditPayment PaymentId String Int Date Frequency
| DeletePayment PaymentId
| ValidateDeletePayment PaymentId
- | AddIncome Int Date
- | ValidateAddIncome IncomeId Int Date
+ | CreateIncome Int Date
+ | ValidateCreateIncome IncomeId Int Date
| DeleteIncome IncomeId
| ValidateDeleteIncome IncomeId
diff --git a/src/client/elm/LoggedIn/Update.elm b/src/client/elm/LoggedIn/Update.elm
index 1f09271..db851f1 100644
--- a/src/client/elm/LoggedIn/Update.elm
+++ b/src/client/elm/LoggedIn/Update.elm
@@ -14,7 +14,7 @@ import Form
import Model exposing (Model)
import Model.Translations exposing (getMessage)
-import Model.Payment as Payment exposing (Payment, Frequency(..), deletePayment)
+import Model.Payment as Payment exposing (Payment, Frequency(..))
import Server
import LoggedData
@@ -58,23 +58,16 @@ update model msg loggedIn =
, Cmd.map LoggedInMsg.IncomeMsg cmd
)
- LoggedInMsg.AddPayment name cost date frequency ->
+ LoggedInMsg.CreatePayment name cost date frequency ->
( loggedIn
, Server.createPayment name cost date frequency
|> Task.perform
- (\err ->
- case err of
- BadResponse 400 jsonErr ->
- LoggedInMsg.NoOp
- _ ->
- LoggedInMsg.NoOp
- )
- (\paymentId -> LoggedInMsg.ValidateAddPayment paymentId name cost date frequency)
+ (always LoggedInMsg.NoOp)
+ (\paymentId -> LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency)
)
- LoggedInMsg.ValidateAddPayment paymentId name cost date frequency ->
+ LoggedInMsg.ValidateCreatePayment paymentId name cost date frequency ->
update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg (Form.Reset (HomeModel.searchInitial frequency))) loggedIn
- :> update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg Form.Submit)
:> update model (LoggedInMsg.HomeMsg <| HomeMsg.UpdatePage 1)
:> (\loggedIn ->
let newPayment = Payment paymentId name cost date loggedIn.me frequency
@@ -83,6 +76,20 @@ update model msg loggedIn =
)
)
+ LoggedInMsg.EditPayment paymentId name cost date frequency ->
+ ( loggedIn
+ , Server.editPayment paymentId name cost date frequency
+ |> Task.perform
+ (always LoggedInMsg.NoOp)
+ (always <| LoggedInMsg.ValidateEditPayment paymentId name cost date frequency)
+ )
+
+ LoggedInMsg.ValidateEditPayment paymentId name cost date frequency ->
+ let updatedPayment = Payment paymentId name cost date loggedIn.me frequency
+ in ( { loggedIn | payments = Payment.edit updatedPayment loggedIn.payments }
+ , Cmd.none
+ )
+
LoggedInMsg.DeletePayment paymentId ->
( loggedIn
, Server.deletePayment paymentId
@@ -92,7 +99,7 @@ update model msg loggedIn =
)
LoggedInMsg.ValidateDeletePayment paymentId ->
- let payments = deletePayment paymentId loggedIn.payments
+ let payments = Payment.delete paymentId loggedIn.payments
frequency =
case Form.getOutput loggedIn.home.search of
Just data -> data.frequency
@@ -104,7 +111,6 @@ update model msg loggedIn =
in if switchToPunctual
then
update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg (Form.Reset (HomeModel.searchInitial Punctual))) loggedIn
- :> update model (LoggedInMsg.HomeMsg <| HomeMsg.SearchMsg Form.Submit)
:> (\loggedIn ->
( { loggedIn | payments = payments }
, Cmd.none
@@ -115,15 +121,15 @@ update model msg loggedIn =
, Cmd.none
)
- LoggedInMsg.AddIncome amount date ->
+ LoggedInMsg.CreateIncome amount date ->
( loggedIn
, Server.createIncome amount date
|> Task.perform
(always LoggedInMsg.NoOp)
- (\incomeId -> (LoggedInMsg.ValidateAddIncome incomeId amount date))
+ (\incomeId -> (LoggedInMsg.ValidateCreateIncome incomeId amount date))
)
- LoggedInMsg.ValidateAddIncome incomeId amount date ->
+ LoggedInMsg.ValidateCreateIncome incomeId amount date ->
let newIncome = { userId = loggedIn.me, amount = amount, time = Date.toTime date }
loggedInIncome = loggedIn.income
in ( { loggedIn
diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm
index 7e5b37d..b08166e 100644
--- a/src/client/elm/Model/Payment.elm
+++ b/src/client/elm/Model/Payment.elm
@@ -6,7 +6,8 @@ module Model.Payment exposing
, Frequency(..)
, paymentsDecoder
, paymentIdDecoder
- , deletePayment
+ , edit
+ , delete
, totalPayments
, punctual
, monthly
@@ -70,8 +71,11 @@ frequencyDecoder =
_ -> Err ("Could not deduce Punctual nor Monthly from " ++ input)
)
-deletePayment : PaymentId -> Payments -> Payments
-deletePayment paymentId = List.filter (((/=) paymentId) << .id)
+edit : Payment -> Payments -> Payments
+edit payment payments = payment :: delete payment.id payments
+
+delete : PaymentId -> Payments -> Payments
+delete paymentId = List.filter (((/=) paymentId) << .id)
totalPayments : (Payment -> Bool) -> UserId -> Payments -> Int
totalPayments paymentFilter userId payments =
diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm
index 9522d17..fd32cec 100644
--- a/src/client/elm/Server.elm
+++ b/src/client/elm/Server.elm
@@ -1,6 +1,7 @@
module Server exposing
( signIn
, createPayment
+ , editPayment
, deletePayment
, createIncome
, deleteIncome
@@ -16,7 +17,7 @@ import Date exposing (Date)
import Date.Extra.Format as DateFormat
-import Utils.Http exposing (..)
+import Utils.Http as HttpUtils
import Model.Payment exposing (..)
import Model.Income exposing (incomesDecoder, incomeIdDecoder, IncomeId)
@@ -25,7 +26,7 @@ import Model.Init exposing (Init)
signIn : String -> Task Http.Error ()
signIn email =
- post ("/signIn?email=" ++ email)
+ HttpUtils.request "POST" ("/signIn?email=" ++ email)
|> Task.map (always ())
createPayment : String -> Int -> Date -> Frequency -> Task Http.Error PaymentId
@@ -36,14 +37,24 @@ createPayment name cost date frequency =
, ("date", Json.string (DateFormat.isoDateString date))
, ("frequency", Json.string (toString frequency))
]
- |> Json.encode 0
- |> Http.string
- |> postWithBody "/payment"
- |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder)
+ |> HttpUtils.jsonRequest "POST" "/payment"
+ |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := paymentIdDecoder)
+
+editPayment : PaymentId -> String -> Int -> Date -> Frequency -> Task Http.Error ()
+editPayment paymentId name cost date frequency =
+ Json.object
+ [ ("id", Json.int paymentId)
+ , ("name", Json.string name)
+ , ("cost", Json.int cost)
+ , ("date", Json.string (DateFormat.isoDateString date))
+ , ("frequency", Json.string (toString frequency))
+ ]
+ |> HttpUtils.jsonRequest "PUT" "/payment"
+ |> Task.map (always ())
deletePayment : PaymentId -> Task Http.Error ()
deletePayment paymentId =
- delete ("/payment?id=" ++ (toString paymentId))
+ HttpUtils.request "DELETE" ("/payment?id=" ++ (toString paymentId))
|> Task.map (always ())
createIncome : Int -> Date -> Task Http.Error IncomeId
@@ -52,17 +63,15 @@ createIncome amount date =
[ ("amount", Json.int amount)
, ("date", Json.string (DateFormat.isoDateString date))
]
- |> Json.encode 0
- |> Http.string
- |> postWithBody "/income"
- |> flip Task.andThen (decodeHttpValue <| "id" := incomeIdDecoder)
+ |> HttpUtils.jsonRequest "POST" "/income"
+ |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := incomeIdDecoder)
deleteIncome : IncomeId -> Task Http.Error ()
deleteIncome incomeId =
- delete ("/income?id=" ++ (toString incomeId))
+ HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId))
|> Task.map (always ())
signOut : Task Http.Error ()
signOut =
- post "/signOut"
+ HttpUtils.request "POST" "/signOut"
|> Task.map (always ())
diff --git a/src/client/elm/Utils/Form.elm b/src/client/elm/Utils/Form.elm
index 482db5f..8d75a32 100644
--- a/src/client/elm/Utils/Form.elm
+++ b/src/client/elm/Utils/Form.elm
@@ -1,6 +1,5 @@
module Utils.Form exposing
( fieldAsText
- , frequency
)
import Form exposing (Form)
@@ -12,10 +11,3 @@ fieldAsText form field =
Form.getFieldAsString field form
|> .value
|> Maybe.withDefault ""
-
-frequency : Form a b -> Frequency
-frequency form =
- let field = Form.getFieldAsString "frequency" form
- in if field.value == Just (toString Monthly)
- then Monthly
- else Punctual
diff --git a/src/client/elm/Utils/Http.elm b/src/client/elm/Utils/Http.elm
index 9bcfad7..4edc233 100644
--- a/src/client/elm/Utils/Http.elm
+++ b/src/client/elm/Utils/Http.elm
@@ -1,26 +1,28 @@
module Utils.Http exposing
- ( post
- , postWithBody
- , delete
+ ( jsonRequest
+ , request
+ , requestWithBody
, decodeHttpValue
, errorKey
)
import Http exposing (..)
import Task exposing (..)
-import Json.Decode as Json exposing (Decoder)
+import Json.Decode as JsonDecode exposing (Decoder)
+import Json.Encode as JsonEncode
-post : String -> Task Error Value
-post url = postWithBody url empty
+jsonRequest : String -> String -> JsonEncode.Value -> Task Error Value
+jsonRequest method url json =
+ json
+ |> JsonEncode.encode 0
+ |> Http.string
+ |> requestWithBody method url
-postWithBody : String -> Body -> Task Error Value
-postWithBody = request "POST"
+request : String -> String -> Task Error Value
+request method url = requestWithBody method url empty
-delete : String -> Task Error Value
-delete url = request "DELETE" url empty
-
-request : String -> String -> Body -> Task Error Value
-request method url body =
+requestWithBody : String -> String -> Body -> Task Error Value
+requestWithBody method url body =
{ verb = method
, headers = []
, url = url
@@ -52,7 +54,7 @@ decodeHttpValue : Decoder a -> Value -> Task Error a
decodeHttpValue decoder value =
case value of
Text str ->
- case Json.decodeString decoder str of
+ case JsonDecode.decodeString decoder str of
Ok v -> succeed v
Err msg -> fail (UnexpectedPayload msg)
_ ->