From cfca18262c1ff48dcb683ddab7d03cf8e55573ff Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 24 Mar 2017 09:21:04 +0000 Subject: Features/categories --- src/client/elm/Dialog/AddPayment/Model.elm | 57 +++++++++++++++++------------- src/client/elm/Dialog/AddPayment/View.elm | 41 +++++++++++++-------- 2 files changed, 60 insertions(+), 38 deletions(-) (limited to 'src/client/elm/Dialog/AddPayment') diff --git a/src/client/elm/Dialog/AddPayment/Model.elm b/src/client/elm/Dialog/AddPayment/Model.elm index 19326f0..a287d37 100644 --- a/src/client/elm/Dialog/AddPayment/Model.elm +++ b/src/client/elm/Dialog/AddPayment/Model.elm @@ -4,6 +4,7 @@ module Dialog.AddPayment.Model exposing , initialAdd , initialClone , initialEdit + , validation ) import Date exposing (Date) @@ -16,46 +17,54 @@ import Validation import Model.Payment as Payment exposing (Payment, Frequency, PaymentId) import Model.Translations exposing (Translations) +import Model.Category as Category exposing (Categories, CategoryId) + +import Utils.Maybe as Maybe type alias Model = { id : Maybe PaymentId , name : String , cost : Int , date : Date + , category : CategoryId , frequency : Frequency } init : Form String Model -init = Form.initial [] validation +init = Form.initial [] (validation Category.empty) initialAdd : Translations -> Date -> Frequency -> List (String, Field) initialAdd translations date frequency = - [ ("date", Field.Text (Date.shortView date translations)) - , ("frequency", Field.Radio (toString frequency)) + [ ("date", Field.string (Date.shortView date translations)) + , ("frequency", Field.string (toString frequency)) + , ("category", Field.string "") ] -initialClone : Translations -> Date -> Payment -> List (String, Field) -initialClone 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)) +initialClone : Translations -> Date -> Maybe CategoryId -> Payment -> List (String, Field) +initialClone translations date category payment = + [ ("name", Field.string payment.name) + , ("cost", Field.string (toString payment.cost)) + , ("date", Field.string (Date.shortView date translations)) + , ("frequency", Field.string (toString payment.frequency)) + , ("category", Field.string (Maybe.map toString category |> Maybe.withDefault "")) ] -initialEdit : Translations -> Payment -> List (String, Field) -initialEdit 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)) +initialEdit : Translations -> Maybe CategoryId -> Payment -> List (String, Field) +initialEdit translations category payment = + [ ("id", Field.string (toString payment.id)) + , ("name", Field.string payment.name) + , ("cost", Field.string (toString payment.cost)) + , ("date", Field.string (Date.shortView payment.date translations)) + , ("frequency", Field.string (toString payment.frequency)) + , ("category", Field.string (Maybe.map toString category |> Maybe.withDefault "")) ] -validation : Validation String Model -validation = - Validate.form5 Model - (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) - (Validate.get "frequency" Payment.validateFrequency) +validation : Categories -> Validation String Model +validation categories = + Validate.map6 Model + (Validate.field "id" (Validate.maybe Validate.int)) + (Validate.field "name" (Validate.string |> Validate.andThen Validate.nonEmpty)) + (Validate.field "cost" (Validate.int |> Validate.andThen (Validate.minInt 1))) + (Validate.field "date" Validation.date) + (Validate.field "category" (Validation.category categories)) + (Validate.field "frequency" Payment.validateFrequency) diff --git a/src/client/elm/Dialog/AddPayment/View.elm b/src/client/elm/Dialog/AddPayment/View.elm index df1ad3b..078d5b7 100644 --- a/src/client/elm/Dialog/AddPayment/View.elm +++ b/src/client/elm/Dialog/AddPayment/View.elm @@ -2,10 +2,10 @@ module Dialog.AddPayment.View exposing ( button ) +import Dict import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) -import Html.App as Html import Task import Form exposing (Form) @@ -18,15 +18,17 @@ import Dialog.Msg as DialogMsg import Tooltip -import View.Form as Form import View.Events exposing (onSubmitPrevDefault) +import View.Form as Form -import Msg exposing (Msg) -import LoggedIn.Msg as LoggedInMsg import LoggedIn.Home.Msg as HomeMsg +import LoggedIn.Msg as LoggedInMsg +import Msg exposing (Msg) -import Model.Translations exposing (getMessage) +import Model.Category exposing (Categories) import Model.Payment as Payment exposing (Frequency(..)) +import Model.PaymentCategory exposing (PaymentCategories) +import Model.Translations exposing (getMessage) import Model.View exposing (View(LoggedInView)) import LoggedData exposing (LoggedData) @@ -39,7 +41,7 @@ button loggedData initialForm title buttonContent tooltip = , title = getMessage loggedData.translations title , body = \model -> addPaymentForm loggedData model.addPayment , confirm = getMessage loggedData.translations "Confirm" - , confirmMsg = submitForm << .addPayment + , confirmMsg = submitForm loggedData.categories loggedData.paymentCategories << .addPayment , undo = getMessage loggedData.translations "Undo" } in Html.button @@ -48,14 +50,19 @@ button loggedData initialForm title buttonContent tooltip = Nothing -> [] ) ++ [ class "addPayment" - , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddPaymentMsg <| Form.Reset initialForm)) + , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.Init "paymentname" (DialogMsg.AddPaymentMsg loggedData.categories loggedData.paymentCategories <| Form.Reset initialForm))) ] ) [ buttonContent ] addPaymentForm : LoggedData -> Form String AddPayment.Model -> Html Msg addPaymentForm loggedData addPayment = - let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg) + let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg loggedData.categories loggedData.paymentCategories) + categoryOptions = + loggedData.categories + |> Dict.toList + |> List.sortBy (.name << Tuple.second) + |> List.map (\(id, category) -> (toString id, category.name)) in Html.form [ class "addPayment" , onSubmitPrevDefault Msg.NoOp @@ -65,18 +72,24 @@ addPaymentForm loggedData addPayment = , if (Form.getFieldAsString "frequency" addPayment).value == Just (toString Punctual) then htmlMap <| Form.textInput loggedData.translations addPayment "payment" "date" else text "" + , htmlMap <| Form.selectInput loggedData.translations addPayment "payment" "category" categoryOptions + , htmlMap <| Form.radioInputs loggedData.translations addPayment "payment" "frequency" [ toString Punctual, toString Monthly ] - , Form.hiddenSubmit (submitForm addPayment) + , Form.hiddenSubmit (submitForm loggedData.categories loggedData.paymentCategories addPayment) ] -submitForm : Form String AddPayment.Model -> Msg -submitForm addPayment = +submitForm : Categories -> PaymentCategories -> Form String AddPayment.Model -> Msg +submitForm categories paymentCategories addPayment = case Form.getOutput addPayment of Just data -> case data.id of Just paymentId -> - Msg.Dialog <| Dialog.UpdateAndClose <| Msg.EditPayment paymentId data.name data.cost data.date data.frequency + Msg.Dialog + <| Dialog.UpdateAndClose + <| Msg.EditPayment paymentId data.name data.cost data.date data.category data.frequency Nothing -> - Msg.Dialog <| Dialog.UpdateAndClose <| Msg.CreatePayment data.name data.cost data.date data.frequency + Msg.Dialog + <| Dialog.UpdateAndClose + <| Msg.CreatePayment data.name data.cost data.date data.category data.frequency Nothing -> - Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg <| Form.Submit + Msg.Dialog <| Dialog.Update <| DialogMsg.AddPaymentMsg categories paymentCategories <| Form.Submit -- cgit v1.2.3