aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Income/Income.hs
blob: 5e9ce1d6ed93d2bb99aef0713ed66dc36f8bdf4f (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
module View.Income.Income
  ( view
  , IncomeIn(..)
  ) where

import qualified Data.List          as L
import qualified Data.Maybe         as Maybe
import           Data.Text          (Text)
import qualified Data.Text          as T
import           Reflex.Dom         (MonadWidget)
import qualified Reflex.Dom         as R

import           Common.Model       (Income (..), Init (..), User (..))
import qualified Common.Model       as CM
import qualified Common.Msg         as Msg
import qualified Common.View.Format as Format
import           Component          (TableIn (..))
import qualified Component

data IncomeIn = IncomeIn
  { _incomeIn_init :: Init
  }

view :: forall t m. MonadWidget t m => IncomeIn -> m ()
view incomeIn =
  R.elClass "main" "income" $ do

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

    Component.table $ TableIn
      { _tableIn_headerLabel = headerLabel
      , _tableIn_rows =
        R.constDyn
        . reverse
        . L.sortOn _income_date
        . _init_incomes
        . _incomeIn_init
        $ incomeIn
      , _tableIn_cell = cell (_incomeIn_init incomeIn)
      }
    return ()

data Header
  = UserHeader
  | AmountHeader
  | DateHeader
  deriving (Eq, Show, Bounded, Enum)

headerLabel :: Header -> Text
headerLabel UserHeader   = Msg.get Msg.Income_Name
headerLabel DateHeader   = Msg.get Msg.Income_Date
headerLabel AmountHeader = Msg.get Msg.Income_Amount

cell :: Init -> Header -> Income -> Text
cell init header income =
  case header of
    UserHeader ->
      Maybe.fromMaybe "" . fmap _user_name $ CM.findUser (_income_userId income) (_init_users init)

    DateHeader ->
      Format.longDay . _income_date $ income

    AmountHeader ->
      Format.price (_init_currency init) . _income_amount $ income