blob: d0c0a4551d6a61054d44b1db8a306197a53584a1 (
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
|
module View.Income.Income
( view
, IncomeIn(..)
) where
import Control.Monad.IO.Class (liftIO)
import qualified Data.List as L
import qualified Data.Maybe as Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Time.Clock as Clock
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
import qualified Util.Date as DateUtil
data IncomeIn = IncomeIn
{ _incomeIn_init :: Init
}
view :: forall t m. MonadWidget t m => IncomeIn -> m ()
view incomeIn =
R.elClass "main" "income" $ do
header (_incomeIn_init incomeIn)
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 ()
header :: forall t m. MonadWidget t m => Init -> m ()
header init =
R.divClass "withMargin" $ do
currentTime <- liftIO Clock.getCurrentTime
Maybe.fromMaybe R.blank $
flip fmap useIncomesFrom $ \since ->
R.el "div" $ do
R.el "h1" $ do
day <- liftIO $ DateUtil.utcToLocalDay since
R.text $ Msg.get (Msg.Income_CumulativeSince (Format.longDay day))
R.el "ul" $
flip mapM_ (_init_users init) $ \user ->
R.el "li" $
R.text $ do
let incomes = filter ((==) (_user_id user) . _income_userId) (_init_incomes init)
T.intercalate " "
[ _user_name user
, "−"
, Format.price (_init_currency init) $
CM.cumulativeIncomesSince currentTime since incomes
]
R.divClass "titleButton" $
R.el "h1" $
R.text $
Msg.get Msg.Income_MonthlyNet
where
useIncomesFrom = CM.useIncomesFrom
(map _user_id $_init_users init)
(_init_incomes init)
(_init_payments init)
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
|