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
|
module View.Payment
( widget
, PaymentIn(..)
, PaymentOut(..)
) where
import Data.Text (Text)
import qualified Data.Text as T
import Prelude hiding (init)
import Reflex.Dom (Dynamic, Event, MonadWidget, Reflex)
import qualified Reflex.Dom as R
import Common.Model (Frequency, Init (..), Payment (..),
PaymentId)
import qualified Common.Util.Text as T
import View.Payment.Header (HeaderIn (..), HeaderOut (..))
import qualified View.Payment.Header as Header
import View.Payment.Pages (PagesIn (..), PagesOut (..))
import qualified View.Payment.Pages as Pages
import View.Payment.Table (TableIn (..), TableOut (..))
import qualified View.Payment.Table as Table
data PaymentIn = PaymentIn
{ _paymentIn_init :: Init
}
data PaymentOut = PaymentOut
{
}
widget :: forall t m. MonadWidget t m => PaymentIn -> m PaymentOut
widget paymentIn = do
R.divClass "payment" $ do
rec
let init = _paymentIn_init paymentIn
paymentsPerPage = 7
payments <- getPayments
(_init_payments init)
(_headerOut_addedPayment header)
(_tableOut_deletedPayment table)
let searchPayments =
getSearchPayments
(_headerOut_searchName header)
(_headerOut_searchFrequency header)
payments
header <- Header.widget $ HeaderIn
{ _headerIn_init = init
, _headerIn_searchPayments = searchPayments
}
table <- Table.widget $ TableIn
{ _tableIn_init = init
, _tableIn_currentPage = _pagesOut_currentPage pages
, _tableIn_payments = searchPayments
, _tableIn_perPage = paymentsPerPage
}
pages <- Pages.widget $ PagesIn
{ _pagesIn_total = length <$> searchPayments
, _pagesIn_perPage = paymentsPerPage
, _pagesIn_reset = R.leftmost $
[ fmap (const ()) . R.updated . _headerOut_searchName $ header
, fmap (const ()) . _headerOut_addedPayment $ header
]
}
pure $ PaymentOut {}
getPayments
:: forall t m. MonadWidget t m
=> [Payment]
-> Event t Payment
-> Event t PaymentId
-> m (Dynamic t [Payment])
getPayments initPayments addedPayment deletedPayment =
R.foldDyn id initPayments $ R.leftmost
[ flip fmap addedPayment (:)
, flip fmap deletedPayment (\paymentId -> filter ((/= paymentId) . _payment_id))
]
getSearchPayments
:: forall t. Reflex t
=> Dynamic t Text
-> Dynamic t Frequency
-> Dynamic t [Payment]
-> Dynamic t [Payment]
getSearchPayments name frequency payments = do
n <- name
f <- frequency
ps <- payments
pure $ flip filter ps (\p ->
( (T.search n (_payment_name p) || T.search n (T.pack . show . _payment_cost $ p))
&& (_payment_frequency p == f)
))
|