diff options
Diffstat (limited to 'src/client/elm/Dialog')
-rw-r--r-- | src/client/elm/Dialog/AddIncome/Model.elm | 52 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddIncome/View.elm | 75 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddPayment/Model.elm | 61 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddPayment/View.elm (renamed from src/client/elm/Dialog/AddPaymentButton/View.elm) | 16 | ||||
-rw-r--r-- | src/client/elm/Dialog/Model.elm | 53 | ||||
-rw-r--r-- | src/client/elm/Dialog/Msg.elm | 1 | ||||
-rw-r--r-- | src/client/elm/Dialog/Update.elm | 7 |
7 files changed, 211 insertions, 54 deletions
diff --git a/src/client/elm/Dialog/AddIncome/Model.elm b/src/client/elm/Dialog/AddIncome/Model.elm new file mode 100644 index 0000000..664557e --- /dev/null +++ b/src/client/elm/Dialog/AddIncome/Model.elm @@ -0,0 +1,52 @@ +module Dialog.AddIncome.Model exposing + ( Model + , init + , initialAdd + , initialClone + , initialEdit + ) + +import Date exposing (Date) +import View.Date as Date + +import Form exposing (Form) +import Form.Field as Field exposing (Field) +import Form.Validate as Validate exposing (Validation) +import Validation + +import Model.Translations exposing (Translations) +import Model.Income exposing (Income, IncomeId) + +type alias Model = + { id : Maybe IncomeId + , amount : Int + , date : Date + } + +init : Form String Model +init = Form.initial [] validate + +initialAdd : Translations -> Date -> List (String, Field) +initialAdd translations date = + [ ("date", Field.Text (Date.shortView date translations)) + ] + +initialClone : Translations -> Date -> Income -> List (String, Field) +initialClone translations date income = + [ ("amount", Field.Text (toString income.amount)) + , ("date", Field.Text (Date.shortView (Date.fromTime income.time) translations)) + ] + +initialEdit : Translations -> IncomeId -> Income -> List (String, Field) +initialEdit translations incomeId income = + [ ("id", Field.Text (toString incomeId)) + , ("amount", Field.Text (toString income.amount)) + , ("date", Field.Text (Date.shortView (Date.fromTime income.time) translations)) + ] + +validate : Validation String Model +validate = + Validate.form3 Model + (Validate.get "id" (Validate.maybe Validate.int)) + (Validate.get "amount" (Validate.int `Validate.andThen` (Validate.minInt 1))) + (Validate.get "date" Validation.date) diff --git a/src/client/elm/Dialog/AddIncome/View.elm b/src/client/elm/Dialog/AddIncome/View.elm new file mode 100644 index 0000000..cc1ac13 --- /dev/null +++ b/src/client/elm/Dialog/AddIncome/View.elm @@ -0,0 +1,75 @@ +module Dialog.AddIncome.View exposing + ( button + ) + +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 Dialog.AddIncome.Model as AddIncome +import Dialog.Msg as DialogMsg + +import Tooltip + +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.View exposing (View(LoggedInView)) + +import LoggedData exposing (LoggedData) +import LoggedIn.Home.Model as HomeModel + +button : String -> LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg +button className loggedData initialForm title buttonContent tooltip = + let dialogConfig = + { className = "incomeDialog" + , title = getMessage title loggedData.translations + , body = \model -> addIncomeForm loggedData model.addIncome + , confirm = getMessage "Confirm" loggedData.translations + , confirmMsg = submitForm << .addIncome + , undo = getMessage "Undo" loggedData.translations + } + in Html.button + ( ( case tooltip of + Just message -> Tooltip.show Msg.Tooltip message + Nothing -> [] + ) + ++ [ class className + , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddIncomeMsg <| Form.Reset initialForm)) + ] + ) + [ buttonContent ] + +addIncomeForm : LoggedData -> Form String AddIncome.Model -> Html Msg +addIncomeForm loggedData addIncome = + let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddIncomeMsg) + in Html.form + [ onSubmitPrevDefault Msg.NoOp ] + [ htmlMap <| Form.textInput loggedData.translations addIncome "income" "amount" + , htmlMap <| Form.textInput loggedData.translations addIncome "income" "date" + , Form.hiddenSubmit (submitForm addIncome) + ] + +submitForm : Form String AddIncome.Model -> Msg +submitForm addIncome = + case Form.getOutput addIncome of + Just data -> + case data.id of + Just incomeId -> + Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.EditIncome incomeId data.amount data.date + Nothing -> + Msg.Dialog <| Dialog.UpdateAndClose <| Msg.UpdateLoggedIn <| LoggedInMsg.CreateIncome data.amount data.date + Nothing -> + Msg.Dialog <| Dialog.Update <| DialogMsg.AddIncomeMsg <| Form.Submit diff --git a/src/client/elm/Dialog/AddPayment/Model.elm b/src/client/elm/Dialog/AddPayment/Model.elm new file mode 100644 index 0000000..19326f0 --- /dev/null +++ b/src/client/elm/Dialog/AddPayment/Model.elm @@ -0,0 +1,61 @@ +module Dialog.AddPayment.Model exposing + ( Model + , init + , initialAdd + , initialClone + , initialEdit + ) + +import Date exposing (Date) +import View.Date as Date + +import Form exposing (Form) +import Form.Field as Field exposing (Field) +import Form.Validate as Validate exposing (Validation) +import Validation + +import Model.Payment as Payment exposing (Payment, Frequency, PaymentId) +import Model.Translations exposing (Translations) + +type alias Model = + { id : Maybe PaymentId + , name : String + , cost : Int + , date : Date + , frequency : Frequency + } + +init : Form String Model +init = Form.initial [] validation + +initialAdd : Translations -> Date -> Frequency -> List (String, Field) +initialAdd translations date frequency = + [ ("date", Field.Text (Date.shortView date translations)) + , ("frequency", Field.Radio (toString frequency)) + ] + +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)) + ] + +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)) + ] + +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) diff --git a/src/client/elm/Dialog/AddPaymentButton/View.elm b/src/client/elm/Dialog/AddPayment/View.elm index 585b1b7..3ffe200 100644 --- a/src/client/elm/Dialog/AddPaymentButton/View.elm +++ b/src/client/elm/Dialog/AddPayment/View.elm @@ -1,5 +1,5 @@ -module Dialog.AddPaymentButton.View exposing - ( view +module Dialog.AddPayment.View exposing + ( button ) import Html exposing (..) @@ -13,7 +13,7 @@ import Form.Field as Field exposing (Field) import Utils.Form as Form import Dialog -import Dialog.Model as DialogModel +import Dialog.AddPayment.Model as AddPayment import Dialog.Msg as DialogMsg import Tooltip @@ -32,8 +32,8 @@ import Model.View exposing (View(LoggedInView)) import LoggedData exposing (LoggedData) import LoggedIn.Home.Model as HomeModel -view : LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg -view loggedData initialForm title buttonContent tooltip = +button : LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg +button loggedData initialForm title buttonContent tooltip = let dialogConfig = { className = "paymentDialog" , title = getMessage title loggedData.translations @@ -42,7 +42,7 @@ view loggedData initialForm title buttonContent tooltip = , confirmMsg = submitForm << .addPayment , undo = getMessage "Undo" loggedData.translations } - in button + in Html.button ( ( case tooltip of Just message -> Tooltip.show Msg.Tooltip message Nothing -> [] @@ -53,7 +53,7 @@ view loggedData initialForm title buttonContent tooltip = ) [ buttonContent ] -addPaymentForm : LoggedData -> Form String DialogModel.AddPayment -> Html Msg +addPaymentForm : LoggedData -> Form String AddPayment.Model -> Html Msg addPaymentForm loggedData addPayment = let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddPaymentMsg) in Html.form @@ -69,7 +69,7 @@ addPaymentForm loggedData addPayment = , Form.hiddenSubmit (submitForm addPayment) ] -submitForm : Form String DialogModel.AddPayment -> Msg +submitForm : Form String AddPayment.Model -> Msg submitForm addPayment = case Form.getOutput addPayment of Just data -> diff --git a/src/client/elm/Dialog/Model.elm b/src/client/elm/Dialog/Model.elm index b49d8f1..a3901f9 100644 --- a/src/client/elm/Dialog/Model.elm +++ b/src/client/elm/Dialog/Model.elm @@ -1,10 +1,6 @@ module Dialog.Model exposing ( Model - , AddPayment , init - , addPaymentInitial - , clonePaymentInitial - , editPaymentInitial ) import Date exposing (Date) @@ -18,51 +14,16 @@ import Validation import Model.Payment as Payment exposing (Payment, Frequency, PaymentId) import Model.Translations exposing (Translations) -type alias Model = - { addPayment : Form String AddPayment - } +import Dialog.AddPayment.Model as AddPayment +import Dialog.AddIncome.Model as AddIncome -type alias AddPayment = - { id : Maybe PaymentId - , name : String - , cost : Int - , date : Date - , frequency : Frequency +type alias Model = + { addPayment : Form String AddPayment.Model + , addIncome : Form String AddIncome.Model } init : Model init = - { addPayment = Form.initial [] addPaymentValidation + { addPayment = AddPayment.init + , addIncome = AddIncome.init } - -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)) - ] - -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.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) - (Validate.get "frequency" Payment.validateFrequency) diff --git a/src/client/elm/Dialog/Msg.elm b/src/client/elm/Dialog/Msg.elm index c9e1596..d504281 100644 --- a/src/client/elm/Dialog/Msg.elm +++ b/src/client/elm/Dialog/Msg.elm @@ -7,3 +7,4 @@ import Form exposing (Form) type Msg = NoOp | AddPaymentMsg Form.Msg + | AddIncomeMsg Form.Msg diff --git a/src/client/elm/Dialog/Update.elm b/src/client/elm/Dialog/Update.elm index e1e2dba..d69082d 100644 --- a/src/client/elm/Dialog/Update.elm +++ b/src/client/elm/Dialog/Update.elm @@ -22,3 +22,10 @@ update msg model = } , Cmd.none ) + + Dialog.AddIncomeMsg formMsg -> + ( { model + | addIncome = Form.update formMsg model.addIncome + } + , Cmd.none + ) |