diff options
Diffstat (limited to 'src/client/elm/Dialog/AddIncome')
-rw-r--r-- | src/client/elm/Dialog/AddIncome/Model.elm | 52 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddIncome/View.elm | 75 |
2 files changed, 127 insertions, 0 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 |