diff options
Diffstat (limited to 'src/client/elm/Dialog')
-rw-r--r-- | src/client/elm/Dialog/AddCategory/Model.elm | 53 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddCategory/View.elm | 72 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddIncome/Model.elm | 27 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddIncome/View.elm | 9 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddPayment/Model.elm | 57 | ||||
-rw-r--r-- | src/client/elm/Dialog/AddPayment/View.elm | 41 | ||||
-rw-r--r-- | src/client/elm/Dialog/Model.elm | 3 | ||||
-rw-r--r-- | src/client/elm/Dialog/Msg.elm | 7 | ||||
-rw-r--r-- | src/client/elm/Dialog/Update.elm | 51 |
9 files changed, 258 insertions, 62 deletions
diff --git a/src/client/elm/Dialog/AddCategory/Model.elm b/src/client/elm/Dialog/AddCategory/Model.elm new file mode 100644 index 0000000..8aeec1a --- /dev/null +++ b/src/client/elm/Dialog/AddCategory/Model.elm @@ -0,0 +1,53 @@ +module Dialog.AddCategory.Model exposing + ( Model + , init + , initialAdd + , initialClone + , initialEdit + , validation + ) + +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.Category exposing (Category, CategoryId) + +type alias Model = + { id : Maybe CategoryId + , name : String + , color : String + } + +init : Form String Model +init = Form.initial [] validation + +initialAdd : Translations -> List (String, Field) +initialAdd translations = + [ ("color", Field.string "#000000") + ] + +initialClone : Translations -> Category -> List (String, Field) +initialClone translations category = + [ ("name", Field.string category.name) + , ("color", Field.string category.color) + ] + +initialEdit : Translations -> CategoryId -> Category -> List (String, Field) +initialEdit translations categoryId category = + [ ("id", Field.string (toString categoryId)) + , ("name", Field.string category.name) + , ("color", Field.string category.color) + ] + +validation : Validation String Model +validation = + Validate.map3 Model + (Validate.field "id" (Validate.maybe Validate.int)) + (Validate.field "name" (Validate.string |> Validate.andThen Validate.nonEmpty)) + (Validate.field "color" (Validate.string |> Validate.andThen Validate.nonEmpty)) diff --git a/src/client/elm/Dialog/AddCategory/View.elm b/src/client/elm/Dialog/AddCategory/View.elm new file mode 100644 index 0000000..6c02351 --- /dev/null +++ b/src/client/elm/Dialog/AddCategory/View.elm @@ -0,0 +1,72 @@ +module Dialog.AddCategory.View exposing + ( button + ) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Task + +import Form exposing (Form) +import Form.Field as Field exposing (Field) +import Utils.Form as Form + +import Dialog +import Dialog.AddCategory.Model as AddCategory +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 : LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg +button loggedData initialForm title buttonContent tooltip = + let dialogConfig = + { className = "categoryDialog" + , title = getMessage loggedData.translations title + , body = \model -> addCategoryForm loggedData model.addCategory + , confirm = getMessage loggedData.translations "Confirm" + , confirmMsg = submitForm << .addCategory + , undo = getMessage loggedData.translations "Undo" + } + in Html.button + ( ( case tooltip of + Just message -> Tooltip.show Msg.Tooltip message + Nothing -> [] + ) + ++ [ onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.Init "categoryname" (DialogMsg.AddCategoryMsg <| Form.Reset initialForm))) ] + ) + [ buttonContent ] + +addCategoryForm : LoggedData -> Form String AddCategory.Model -> Html Msg +addCategoryForm loggedData addCategory = + let htmlMap = Html.map (Msg.Dialog << Dialog.Update << DialogMsg.AddCategoryMsg) + in Html.form + [ onSubmitPrevDefault Msg.NoOp ] + [ htmlMap <| Form.textInput loggedData.translations addCategory "category" "name" + , htmlMap <| Form.colorInput loggedData.translations addCategory "category" "color" + , Form.hiddenSubmit (submitForm addCategory) + ] + +submitForm : Form String AddCategory.Model -> Msg +submitForm addCategory = + case Form.getOutput addCategory of + Just data -> + case data.id of + Just categoryId -> + Msg.Dialog <| Dialog.UpdateAndClose <| Msg.EditCategory categoryId data.name data.color + Nothing -> + Msg.Dialog <| Dialog.UpdateAndClose <| Msg.CreateCategory data.name data.color + Nothing -> + Msg.Dialog <| Dialog.Update <| DialogMsg.AddCategoryMsg <| Form.Submit diff --git a/src/client/elm/Dialog/AddIncome/Model.elm b/src/client/elm/Dialog/AddIncome/Model.elm index 0d52b22..ad7b25a 100644 --- a/src/client/elm/Dialog/AddIncome/Model.elm +++ b/src/client/elm/Dialog/AddIncome/Model.elm @@ -4,6 +4,7 @@ module Dialog.AddIncome.Model exposing , initialAdd , initialClone , initialEdit + , validation ) import Date exposing (Date) @@ -24,29 +25,29 @@ type alias Model = } init : Form String Model -init = Form.initial [] validate +init = Form.initial [] validation initialAdd : Translations -> Date -> List (String, Field) initialAdd translations date = - [ ("date", Field.Text (Date.shortView date translations)) + [ ("date", Field.string (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 translations)) + [ ("amount", Field.string (toString income.amount)) + , ("date", Field.string (Date.shortView date 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)) + [ ("id", Field.string (toString incomeId)) + , ("amount", Field.string (toString income.amount)) + , ("date", Field.string (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) +validation : Validation String Model +validation = + Validate.map3 Model + (Validate.field "id" (Validate.maybe Validate.int)) + (Validate.field "amount" (Validate.int |> Validate.andThen (Validate.minInt 1))) + (Validate.field "date" Validation.date) diff --git a/src/client/elm/Dialog/AddIncome/View.elm b/src/client/elm/Dialog/AddIncome/View.elm index c628d37..b413308 100644 --- a/src/client/elm/Dialog/AddIncome/View.elm +++ b/src/client/elm/Dialog/AddIncome/View.elm @@ -5,7 +5,6 @@ module Dialog.AddIncome.View exposing import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) -import Html.App as Html import Task import Form exposing (Form) @@ -31,8 +30,8 @@ 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 = +button : LoggedData -> List (String, Field) -> String -> Html Msg -> Maybe String -> Html Msg +button loggedData initialForm title buttonContent tooltip = let dialogConfig = { className = "incomeDialog" , title = getMessage loggedData.translations title @@ -46,9 +45,7 @@ button className loggedData initialForm title buttonContent tooltip = Just message -> Tooltip.show Msg.Tooltip message Nothing -> [] ) - ++ [ class className - , onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.AddIncomeMsg <| Form.Reset initialForm)) - ] + ++ [ onClick (Msg.Dialog <| Dialog.OpenWithUpdate dialogConfig (DialogMsg.Init "incomeamount" (DialogMsg.AddIncomeMsg <| Form.Reset initialForm))) ] ) [ buttonContent ] 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 diff --git a/src/client/elm/Dialog/Model.elm b/src/client/elm/Dialog/Model.elm index a3901f9..d4fd484 100644 --- a/src/client/elm/Dialog/Model.elm +++ b/src/client/elm/Dialog/Model.elm @@ -16,14 +16,17 @@ import Model.Translations exposing (Translations) import Dialog.AddPayment.Model as AddPayment import Dialog.AddIncome.Model as AddIncome +import Dialog.AddCategory.Model as AddCategory type alias Model = { addPayment : Form String AddPayment.Model , addIncome : Form String AddIncome.Model + , addCategory : Form String AddCategory.Model } init : Model init = { addPayment = AddPayment.init , addIncome = AddIncome.init + , addCategory = AddCategory.init } diff --git a/src/client/elm/Dialog/Msg.elm b/src/client/elm/Dialog/Msg.elm index d504281..68ed146 100644 --- a/src/client/elm/Dialog/Msg.elm +++ b/src/client/elm/Dialog/Msg.elm @@ -4,7 +4,12 @@ module Dialog.Msg exposing import Form exposing (Form) +import Model.Category exposing (Categories) +import Model.PaymentCategory exposing (PaymentCategories) + type Msg = NoOp - | AddPaymentMsg Form.Msg + | Init String Msg + | AddPaymentMsg Categories PaymentCategories Form.Msg | AddIncomeMsg Form.Msg + | AddCategoryMsg Form.Msg diff --git a/src/client/elm/Dialog/Update.elm b/src/client/elm/Dialog/Update.elm index d69082d..3915548 100644 --- a/src/client/elm/Dialog/Update.elm +++ b/src/client/elm/Dialog/Update.elm @@ -2,10 +2,19 @@ module Dialog.Update exposing ( update ) +import Dom exposing (Id) import Form exposing (Form) +import Form.Field as Field +import Task -import Dialog.Msg as Dialog +import Dialog.AddCategory.Model as AddCategory +import Dialog.AddIncome.Model as AddIncome +import Dialog.AddPayment.Model as AddPayment import Dialog.Model as Dialog +import Dialog.Msg as Dialog + +import Model.Category exposing (Categories) +import Model.PaymentCategory as PaymentCategory exposing (PaymentCategories) update : Dialog.Msg -> Dialog.Model -> (Dialog.Model, Cmd Dialog.Msg) update msg model = @@ -16,16 +25,50 @@ update msg model = , Cmd.none ) - Dialog.AddPaymentMsg formMsg -> + Dialog.Init inputId dialogMsg -> + update dialogMsg model + |> Tuple.mapSecond (\cmd -> Cmd.batch [cmd, inputFocus inputId]) + + Dialog.AddPaymentMsg categories paymentCategories formMsg -> ( { model - | addPayment = Form.update formMsg model.addPayment + | addPayment = + Form.update (AddPayment.validation categories) formMsg model.addPayment + |> updateCategory categories paymentCategories formMsg } , Cmd.none ) Dialog.AddIncomeMsg formMsg -> ( { model - | addIncome = Form.update formMsg model.addIncome + | addIncome = Form.update AddIncome.validation formMsg model.addIncome } , Cmd.none ) + + Dialog.AddCategoryMsg formMsg -> + ( { model + | addCategory = Form.update AddCategory.validation formMsg model.addCategory + } + , Cmd.none + ) + +inputFocus : Id -> Cmd Dialog.Msg +inputFocus id = + Dom.focus id + |> Task.map (always Dialog.NoOp) + |> Task.onError (\_ -> Task.succeed Dialog.NoOp) + |> Task.perform (always Dialog.NoOp) + +updateCategory : Categories -> PaymentCategories -> Form.Msg -> (Form String AddPayment.Model -> Form String AddPayment.Model) +updateCategory categories paymentCategories formMsg = + case formMsg of + Form.Input "name" Form.Text (Field.String paymentName) -> + case PaymentCategory.search paymentName paymentCategories of + Just category -> + Form.update + (AddPayment.validation categories) + (Form.Input "category" Form.Text (Field.String <| toString category)) + Nothing -> + identity + _ -> + identity |