aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Dialog/AddIncome/View.elm
blob: cc1ac13e42ee320e306d1bb252f57013b8ab15da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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