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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
module View.Payment
( widget
, PaymentIn(..)
, PaymentOut(..)
) where
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (NominalDiffTime)
import Prelude hiding (init)
import Reflex.Dom (Dynamic, Event, MonadWidget, Reflex)
import qualified Reflex.Dom as R
import Common.Model (Frequency, Init (..), Payment (..),
PaymentCategory (..), PaymentId,
SavedPayment (..))
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.elClass "main" "payment" $ do
rec
let init = _paymentIn_init paymentIn
paymentsPerPage = 7
savedPayments = R.leftmost
[ _headerOut_addPayment header
, _tableOut_addPayment table
]
payments <- getPayments
(_init_payments init)
(_savedPayment_payment <$> savedPayments)
(_tableOut_deletePayment table)
paymentCategories <- getPaymentCategories
(_init_paymentCategories init)
(_savedPayment_paymentCategory <$> savedPayments)
payments
(_tableOut_deletePayment table)
(searchNameEvent, searchName) <-
debounceSearchName (_headerOut_searchName header)
let searchPayments =
getSearchPayments searchName (_headerOut_searchFrequency header) payments
header <- Header.widget $ HeaderIn
{ _headerIn_init = init
, _headerIn_payments = payments
, _headerIn_searchPayments = searchPayments
, _headerIn_paymentCategories = paymentCategories
}
table <- Table.widget $ TableIn
{ _tableIn_init = init
, _tableIn_currentPage = _pagesOut_currentPage pages
, _tableIn_payments = searchPayments
, _tableIn_perPage = paymentsPerPage
, _tableIn_paymentCategories = paymentCategories
}
pages <- Pages.widget $ PagesIn
{ _pagesIn_total = length <$> searchPayments
, _pagesIn_perPage = paymentsPerPage
, _pagesIn_reset = R.leftmost $
[ () <$ searchNameEvent
, () <$ _headerOut_addPayment header
]
}
pure $ PaymentOut {}
debounceSearchName
:: forall t m. MonadWidget t m
=> Dynamic t Text
-> m (Event t Text, Dynamic t Text)
debounceSearchName searchName = do
event <- R.debounce (0.5 :: NominalDiffTime) (R.updated searchName)
dynamic <- R.holdDyn "" event
return (event, dynamic)
getPayments
:: forall t m. MonadWidget t m
=> [Payment]
-> Event t Payment
-> Event t Payment
-> m (Dynamic t [Payment])
getPayments initPayments addPayment deletePayment =
R.foldDyn id initPayments $ R.leftmost
[ (:) <$> addPayment
, R.ffor deletePayment (\p -> filter ((/= (_payment_id p)) . _payment_id))
]
getPaymentCategories
:: forall t m. MonadWidget t m
=> [PaymentCategory]
-> Event t PaymentCategory -- add payment category
-> Dynamic t [Payment] -- payments
-> Event t Payment -- delete payment
-> m (Dynamic t [PaymentCategory])
getPaymentCategories initPaymentCategories addPaymentCategory payments deletePayment =
R.foldDyn id initPaymentCategories $ R.leftmost
[ (:) <$> addPaymentCategory
, R.ffor deletePaymentName (\name -> filter ((/=) (T.toLower name) . _paymentCategory_name))
]
where
deletePaymentName =
R.attachWithMaybe
(\ps p ->
if any (\p2 -> _payment_id p2 /= _payment_id p && lowerName p2 == lowerName p) ps then
Nothing
else
Just (_payment_name p))
(R.current payments)
deletePayment
lowerName = T.toLower . _payment_name
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)
))
|