aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Payment/Clone.hs
blob: 82b0c272f001d237f666c2824a532d8d07d9646c (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
module View.Payment.Clone
  ( In(..)
  , view
  ) where

import qualified Control.Monad          as Monad
import           Control.Monad.IO.Class (liftIO)
import qualified Data.Text              as T
import qualified Data.Time.Clock        as Time
import           Reflex.Dom             (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom             as R

import           Common.Model           (Category (..), CategoryId,
                                         CreatePaymentForm (..), Frequency (..),
                                         Payment (..), PaymentCategory (..),
                                         SavedPayment (..))
import qualified Common.Msg             as Msg
import qualified Common.Util.Time       as TimeUtil
import qualified Component.Modal        as Modal
import qualified Util.Ajax              as Ajax
import qualified Util.Reflex            as ReflexUtil
import qualified View.Payment.Form      as Form

data In t = In
  { _in_show              :: Event t ()
  , _in_categories        :: [Category]
  , _in_paymentCategories :: Dynamic t [PaymentCategory]
  , _in_payment           :: Dynamic t Payment
  , _in_category          :: Dynamic t CategoryId
  }

view :: forall t m. MonadWidget t m => In t -> Modal.Content t m SavedPayment
view input cancel = do

  currentDay <- liftIO $ Time.getCurrentTime >>= TimeUtil.timeToDay

  form <- R.dyn $ do
    paymentCategories <- _in_paymentCategories input
    payment <- _in_payment input
    category <- _in_category input
    return . Form.view $ Form.In
      { Form._in_cancel = cancel
      , Form._in_headerLabel = Msg.get Msg.Payment_CloneLong
      , Form._in_categories = _in_categories input
      , Form._in_paymentCategories = paymentCategories
      , Form._in_name = _payment_name payment
      , Form._in_cost = T.pack . show . _payment_cost $ payment
      , Form._in_date = currentDay
      , Form._in_category = category
      , Form._in_frequency = _payment_frequency payment
      , Form._in_mkPayload = CreatePaymentForm
      , Form._in_ajax = Ajax.post
      }

  hide <- ReflexUtil.flatten (Form._output_hide <$> form)
  clonePayment <- ReflexUtil.flatten (Form._output_addPayment <$> form)

  return $
    ( hide
    , clonePayment
    )