From 885dfd7708e338a3220c85b7f22a3ac267aad3f7 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 26 Jun 2016 17:30:03 +0200 Subject: Add the clone functionality on payments --- src/client/elm/Dialog/AddPayment/View.elm | 73 ------------------------- src/client/elm/Dialog/AddPaymentButton/View.elm | 72 ++++++++++++++++++++++++ src/client/elm/Dialog/Model.elm | 15 ++++- src/client/elm/LoggedIn/Home/Header/View.elm | 18 ++++-- src/client/elm/LoggedIn/Home/View/Table.elm | 13 ++++- src/client/elm/Model/Payment.elm | 9 ++- src/server/Design/LoggedIn/Home/Table.hs | 4 +- 7 files changed, 117 insertions(+), 87 deletions(-) delete mode 100644 src/client/elm/Dialog/AddPayment/View.elm create mode 100644 src/client/elm/Dialog/AddPaymentButton/View.elm diff --git a/src/client/elm/Dialog/AddPayment/View.elm b/src/client/elm/Dialog/AddPayment/View.elm deleted file mode 100644 index 30df617..0000000 --- a/src/client/elm/Dialog/AddPayment/View.elm +++ /dev/null @@ -1,73 +0,0 @@ -module Dialog.AddPayment.View exposing - ( view - ) - -import Html exposing (..) -import Html.Attributes exposing (..) -import Html.Events exposing (..) -import Html.App as Html -import Task -import Date - -import Form exposing (Form) -import Utils.Form as Form - -import Dialog - -import View.Form as Form -import View.Events exposing (onSubmitPrevDefault) - -import Msg exposing (Msg) -import LoggedIn.Msg as LoggedInMsg -import LoggedIn.Home.Msg as HomeMsg - -import Model.Translations exposing (getMessage) -import Model.Payment as Payment exposing (Frequency(..)) -import Model.View exposing (View(LoggedInView)) - -import Dialog.Model as DialogModel -import Dialog.Msg as DialogMsg - -import LoggedData exposing (LoggedData) -import LoggedIn.Home.Model as HomeModel - -view : LoggedData -> Frequency -> Html Msg -view loggedData frequency = - let dialogConfig = - { className = "paymentDialog" - , title = getMessage "AddPayment" loggedData.translations - , body = \model -> addPaymentForm loggedData model.addPayment - , confirm = getMessage "Confirm" loggedData.translations - , confirmMsg = submitForm << .addPayment - , undo = getMessage "Undo" loggedData.translations - } - currentDate = Date.fromTime loggedData.currentTime - in button - [ class "addPayment" - , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset (DialogModel.addPaymentInitial loggedData.translations currentDate frequency))) - ] - [ text (getMessage "AddPayment" loggedData.translations) ] - -addPaymentForm : LoggedData -> Form String DialogModel.AddPayment -> Html Msg -addPaymentForm loggedData addPayment = - let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg) - in Html.form - [ class "addPayment" - , onSubmitPrevDefault Msg.NoOp - ] - [ htmlMap <| Form.textInput loggedData.translations addPayment "payment" "name" - , htmlMap <| Form.textInput loggedData.translations addPayment "payment" "cost" - , if Form.frequency addPayment == Punctual - then htmlMap <| Form.textInput loggedData.translations addPayment "payment" "date" - else text "" - , htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ] - , Form.hiddenSubmit (submitForm addPayment) - ] - -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 - Nothing -> - Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg <| Form.Submit diff --git a/src/client/elm/Dialog/AddPaymentButton/View.elm b/src/client/elm/Dialog/AddPaymentButton/View.elm new file mode 100644 index 0000000..8014571 --- /dev/null +++ b/src/client/elm/Dialog/AddPaymentButton/View.elm @@ -0,0 +1,72 @@ +module Dialog.AddPaymentButton.View exposing + ( view + ) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Html.App as Html +import Task + +import Form exposing (Form) +import Form.Field as Field exposing (Field) +import Utils.Form as Form + +import Dialog + +import View.Form as Form +import View.Events exposing (onSubmitPrevDefault) + +import Msg exposing (Msg) +import LoggedIn.Msg as LoggedInMsg +import LoggedIn.Home.Msg as HomeMsg + +import Model.Translations exposing (getMessage) +import Model.Payment as Payment exposing (Frequency(..)) +import Model.View exposing (View(LoggedInView)) + +import Dialog.Model as DialogModel +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 = + let dialogConfig = + { className = "paymentDialog" + , title = getMessage "AddPayment" loggedData.translations + , body = \model -> addPaymentForm loggedData model.addPayment + , confirm = getMessage "Confirm" loggedData.translations + , confirmMsg = submitForm << .addPayment + , undo = getMessage "Undo" loggedData.translations + } + in button + [ class "addPayment" + , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset initialForm)) + ] + [ content ] + +addPaymentForm : LoggedData -> Form String DialogModel.AddPayment -> Html Msg +addPaymentForm loggedData addPayment = + let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg) + in Html.form + [ class "addPayment" + , onSubmitPrevDefault Msg.NoOp + ] + [ htmlMap <| Form.textInput loggedData.translations addPayment "payment" "name" + , htmlMap <| Form.textInput loggedData.translations addPayment "payment" "cost" + , if Form.frequency addPayment == Punctual + then htmlMap <| Form.textInput loggedData.translations addPayment "payment" "date" + else text "" + , htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ] + , Form.hiddenSubmit (submitForm addPayment) + ] + +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 + 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 2ac4591..6256fea 100644 --- a/src/client/elm/Dialog/Model.elm +++ b/src/client/elm/Dialog/Model.elm @@ -3,6 +3,7 @@ module Dialog.Model exposing , AddPayment , init , addPaymentInitial + , clonePaymentInitial ) import Date exposing (Date) @@ -13,7 +14,7 @@ import Form.Field as Field exposing (Field) import Form.Validate as Validate exposing (Validation) import Validation -import Model.Payment as Payment +import Model.Payment as Payment exposing (Payment, Frequency) import Model.Translations exposing (Translations) type alias Model = @@ -24,7 +25,7 @@ type alias AddPayment = { name : String , cost : Int , date : Date - , frequency : Payment.Frequency + , frequency : Frequency } init : Model @@ -32,12 +33,20 @@ init = { addPayment = Form.initial [] addPaymentValidation } -addPaymentInitial : Translations -> Date -> Payment.Frequency -> List (String, Field) +addPaymentInitial : Translations -> Date -> Frequency -> List (String, Field) addPaymentInitial translations date frequency = [ ("date", Field.Text (Date.shortView date translations)) , ("frequency", Field.Radio (toString frequency)) ] +clonePaymentInitial : Translations -> Date -> Payment -> List (String, Field) +clonePaymentInitial translations date payment = + [ ("name", Field.Text payment.name) + , ("cost", Field.Text (toString payment.cost)) + , ("date", Field.Text (Date.shortView date translations)) + , ("frequency", Field.Radio (toString payment.frequency)) + ] + addPaymentValidation : Validation String AddPayment addPaymentValidation = Validate.form4 AddPayment diff --git a/src/client/elm/LoggedIn/Home/Header/View.elm b/src/client/elm/LoggedIn/Home/Header/View.elm index 9468a04..753a120 100644 --- a/src/client/elm/LoggedIn/Home/Header/View.elm +++ b/src/client/elm/LoggedIn/Home/Header/View.elm @@ -8,6 +8,7 @@ import Html.Events exposing (..) import Html.App as Html import String import Dict +import Date import Form exposing (Form) import View.Form as Form @@ -24,7 +25,8 @@ import Model.Conf exposing (Conf) import Model.Payment as Payment exposing (Payments, Frequency(..)) import Model.Translations exposing (getMessage) -import Dialog.AddPayment.View as AddPayment +import Dialog.Model as DialogModel +import Dialog.AddPaymentButton.View as AddPaymentButton import LoggedIn.Home.View.ExceedingPayers as ExceedingPayers import LoggedIn.View.Format as Format @@ -43,11 +45,15 @@ view loggedData { search } payments frequency = searchLine : LoggedData -> Form String Home.Search -> Frequency -> Html Msg searchLine loggedData search frequency = - Html.div - [ class "searchLine" ] - [ searchForm loggedData search - , AddPayment.view loggedData frequency - ] + let currentDate = Date.fromTime loggedData.currentTime + in Html.div + [ class "searchLine" ] + [ searchForm loggedData search + , AddPaymentButton.view + loggedData + (DialogModel.addPaymentInitial loggedData.translations currentDate frequency) + (text (getMessage "AddPayment" loggedData.translations)) + ] searchForm : LoggedData -> Form String Home.Search -> Html Msg searchForm loggedData search = diff --git a/src/client/elm/LoggedIn/Home/View/Table.elm b/src/client/elm/LoggedIn/Home/View/Table.elm index 282f00a..d2087bb 100644 --- a/src/client/elm/LoggedIn/Home/View/Table.elm +++ b/src/client/elm/LoggedIn/Home/View/Table.elm @@ -29,6 +29,8 @@ import Model.Payment as Payment exposing (..) import Model.Translations exposing (getMessage) import Dialog +import Dialog.Model as DialogModel +import Dialog.AddPaymentButton.View as AddPaymentButton view : LoggedData -> HomeModel.Model -> Payments -> Frequency -> Html Msg view loggedData homeModel payments frequency = @@ -61,6 +63,7 @@ headerLine loggedData frequency = Punctual -> div [ class "cell date" ] [ text <| getMessage "Date" loggedData.translations ] Monthly -> text "" , div [ class "cell" ] [] + , div [ class "cell" ] [] ] paymentLine : LoggedData -> HomeModel.Model -> Frequency -> Payment -> Html Msg @@ -96,7 +99,15 @@ paymentLine loggedData homeModel frequency payment = Monthly -> text "" , div - [ class "cell delete" ] + [ class "cell button" ] + [ let currentDate = Date.fromTime loggedData.currentTime + in AddPaymentButton.view + loggedData + (DialogModel.clonePaymentInitial loggedData.translations currentDate payment) + (FontAwesome.clone Color.chestnutRose 18) + ] + , div + [ class "cell button" ] [ if loggedData.me /= payment.userId then text "" diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm index c89e709..7e5b37d 100644 --- a/src/client/elm/Model/Payment.elm +++ b/src/client/elm/Model/Payment.elm @@ -101,9 +101,14 @@ search : String -> Frequency -> Payments -> Payments search name frequency payments = payments |> List.filter ((==) frequency << .frequency) - |> List.sortBy (Date.toTime << .date) + |> paymentSort frequency |> List.filter (searchSuccess name) - |> List.reverse + +paymentSort : Frequency -> Payments -> Payments +paymentSort frequency = + case frequency of + Punctual -> List.reverse << List.sortBy (Date.toTime << .date) + Monthly -> List.sortBy (String.toLower << .name) searchSuccess : String -> Payment -> Bool searchSuccess text { name } = (String.toLower text) `String.contains` (String.toLower name) diff --git a/src/server/Design/LoggedIn/Home/Table.hs b/src/server/Design/LoggedIn/Home/Table.hs index 0085e4a..23b924f 100644 --- a/src/server/Design/LoggedIn/Home/Table.hs +++ b/src/server/Design/LoggedIn/Home/Table.hs @@ -45,7 +45,7 @@ design = do display tableCell position relative verticalAlign middle - ".category" & width (pct 40) + ".category" & width (pct 37) ".cost" & do width (pct 17) ".refund" & color Color.mossGreen @@ -58,7 +58,7 @@ design = do Media.desktop $ do ".shortDate" ? display none ".longDate" ? display inline - ".delete" & do + ".cell.button" & do position relative width (pct 3) textAlign (alignSide sideCenter) -- cgit v1.2.3