aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Payment/Add.hs
blob: 69e29a7f1b2d4df327395cf1cefc41f4711bec00 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
module View.Payment.Add
  ( view
  , AddIn(..)
  , AddOut(..)
  ) where

import           Control.Monad             (join)
import           Control.Monad.IO.Class    (liftIO)
import qualified Data.List                 as L
import qualified Data.Map                  as M
import qualified Data.Maybe                as Maybe
import           Data.Text                 (Text)
import qualified Data.Text                 as T
import qualified Data.Time.Calendar        as Calendar
import qualified Data.Time.Clock           as Time
import qualified Data.Validation           as V
import           Reflex.Dom                (Dynamic, Event, MonadWidget, Reflex)
import qualified Reflex.Dom                as R
import qualified Text.Read                 as T

import           Common.Model              (Category (..), CategoryId,
                                            CreatePayment (..),
                                            CreatedPayment (..), Frequency (..),
                                            Payment (..), PaymentCategory (..))
import qualified Common.Msg                as Msg
import qualified Common.Util.Time          as Time
import qualified Common.Validation.Payment as PaymentValidation
import           Component                 (ButtonIn (..), InputIn (..),
                                            InputOut (..), SelectIn (..),
                                            SelectOut (..))
import qualified Component                 as Component
import qualified Util.Ajax                 as Ajax
import qualified Util.Either               as EitherUtil
import qualified Util.Validation           as ValidationUtil
import qualified Util.WaitFor              as WaitFor

data AddIn t = AddIn
  { _addIn_categories        :: [Category]
  , _addIn_paymentCategories :: Dynamic t [PaymentCategory]
  , _addIn_frequency         :: Dynamic t Frequency
  , _addIn_cancel            :: Event t ()
  }

data AddOut t = AddOut
  { _addOut_cancel             :: Event t ()
  , _addOut_addPayment         :: Event t CreatedPayment
  , _addOut_addPaymentCategory :: Event t PaymentCategory
  }

view :: forall t m. MonadWidget t m => AddIn t -> m (AddOut t)
view addIn = do
  R.divClass "add" $ do
    R.divClass "addHeader" $ R.text $ Msg.get Msg.Payment_Add

    R.divClass "addContent" $ do
      rec
        let reset = R.leftmost
              [ "" <$ cancel
              , "" <$ addPayment
              , "" <$ _addIn_cancel addIn
              ]

        name <- Component.input
          (Component.defaultInputIn
            { _inputIn_label = Msg.get Msg.Payment_Name
            , _inputIn_validation = PaymentValidation.name
            })
          reset
          confirm

        cost <- _inputOut_value <$> (Component.input
          (Component.defaultInputIn
            { _inputIn_label = Msg.get Msg.Payment_Cost
            , _inputIn_validation = PaymentValidation.cost
            })
          reset
          confirm)

        now <- liftIO Time.getCurrentTime

        currentDay <- do
          d <- liftIO $ Time.timeToDay now
          return . T.pack . Calendar.showGregorian $ d

        date <- _inputOut_value <$> (Component.input
          (Component.defaultInputIn
            { _inputIn_label = Msg.get Msg.Payment_Date
            , _inputIn_initialValue = currentDay
            , _inputIn_inputType = "date"
            , _inputIn_hasResetButton = False
            , _inputIn_validation = PaymentValidation.date
            })
          (currentDay <$ reset)
          confirm)

        let setCategory =
              R.fmapMaybe id
                . R.updated
                $ findCategory <$> (_inputOut_raw name) <*> (_addIn_paymentCategories addIn)

        category <- _selectOut_value <$> (Component.select $ SelectIn
          { _selectIn_label = Msg.get Msg.Payment_Category
          , _selectIn_initialValue = -1
          , _selectIn_value = setCategory
          , _selectIn_values = R.constDyn categories
          , _selectIn_reset = reset
          , _selectIn_isValid = \id -> id /= -1
          , _selectIn_validate = confirm
          })

        let payment = do
              n <- _inputOut_value name
              c <- cost
              d <- date
              cat <- category
              f <- _addIn_frequency addIn
              return (CreatePayment
                <$> ValidationUtil.nelError n
                <*> ValidationUtil.nelError c
                <*> ValidationUtil.nelError d
                <*> ValidationUtil.nelError cat
                <*> V.Success f)

        (addPayment, cancel, confirm) <- R.divClass "buttons" $ do
          rec
            cancel <- Component._buttonOut_clic <$> (Component.button $
              (Component.defaultButtonIn (R.text $ Msg.get Msg.Dialog_Undo))
                { _buttonIn_class = R.constDyn "undo" })

            confirm <- Component._buttonOut_clic <$> (Component.button $
              (Component.defaultButtonIn (R.text $ Msg.get Msg.Dialog_Confirm))
                { _buttonIn_class = R.constDyn "confirm"
                , _buttonIn_waiting = waiting
                , _buttonIn_submit = True
                })

            (addPayment, waiting) <- WaitFor.waitFor
              (Ajax.postJson "/payment")
              (ValidationUtil.fireValidation payment confirm)

          return (R.fmapMaybe EitherUtil.eitherToMaybe addPayment, cancel, confirm)

      return AddOut
        { _addOut_cancel = cancel
        , _addOut_addPayment = addPayment
        }

  where
    frequencies = M.fromList
      [ (Punctual, Msg.get Msg.Payment_PunctualMale)
      , (Monthly, Msg.get Msg.Payment_MonthlyMale)
      ]

    categories = M.fromList . flip map (_addIn_categories addIn) $ \c ->
      (_category_id c, _category_name c)


findCategory :: Text -> [PaymentCategory] -> Maybe CategoryId
findCategory paymentName =
  fmap _paymentCategory_category
    . L.find ((==) (T.toLower paymentName) . _paymentCategory_name)