aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Income/Header.hs
blob: a26e16a9380cfcdab8d4d5889cd7067e6139674f (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
module View.Income.Header
  ( view
  , In(..)
  , Out(..)
  ) where

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

import           Common.Model           (Currency, Income (..),
                                         IncomeHeader (..), User (..))
import qualified Common.Model           as CM
import qualified Common.Msg             as Msg
import qualified Common.View.Format     as Format

import qualified Component.Button       as Button
import qualified Component.Modal        as Modal
import qualified View.Income.Form       as Form

data In t = In
  { _in_users    :: [User]
  , _in_header   :: IncomeHeader
  , _in_currency :: Currency
  }

data Out t = Out
  { _out_add :: Event t ()
  }

view :: forall t m. MonadWidget t m => In t -> m (Out t)
view input =
  R.divClass "withMargin" $ do

    currentTime <- liftIO Clock.getCurrentTime

    case _incomeHeader_since $ _in_header input of
      Nothing ->
        R.blank

      Just since ->
        R.el "div" $ do

          R.el "h1" $ do
            R.text $ Msg.get (Msg.Income_CumulativeSince (Format.longDay since))

          R.el "ul" $
            flip mapM_ (M.toList . _incomeHeader_byUser $ _in_header input) $ \(userId, amount) ->
              R.el "li" $
                R.text $
                  T.intercalate " "
                    [ Maybe.fromMaybe "" . fmap _user_name $ CM.findUser userId (_in_users input)
                    , "−"
                    , Format.price (_in_currency input) amount
                    ]

    R.divClass "titleButton" $ do
      R.el "h1" $
        R.text $
          Msg.get Msg.Income_MonthlyNet

      addIncome <- Button._out_clic <$>
        (Button.view . Button.defaultIn . R.text $
          Msg.get Msg.Income_AddLong)

      addIncome <- Modal.view $ Modal.In
        { Modal._in_show    = addIncome
        , Modal._in_content = Form.view $ Form.In { Form._in_operation = Form.New }
        }

      return $ Out
        { _out_add = addIncome
        }