-
Notifications
You must be signed in to change notification settings - Fork 44
/
TransactionsPage.jsx
337 lines (293 loc) · 9.43 KB
/
TransactionsPage.jsx
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import React, { Component } from 'react'
import { withRouter } from 'react-router'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { isMobileApp } from 'cozy-device-helper'
import { translate, withBreakpoints } from 'cozy-ui/transpiled/react'
import { flowRight as compose, isEqual, findIndex, uniq, maxBy } from 'lodash'
import { getFilteringDoc } from 'ducks/filters'
import { Padded } from 'components/Spacing'
import {
getTransactionsFilteredByAccount,
getFilteredTransactions
} from 'ducks/filters'
import { getCategoryIdFromName } from 'ducks/categories/categoriesMap'
import { getDate, getDisplayDate } from 'ducks/transactions/helpers'
import { queryConnect } from 'cozy-client'
import Loading from 'components/Loading'
import Delayed from 'components/Delayed'
import { TransactionList } from 'ducks/transactions/Transactions.jsx'
import styles from 'ducks/transactions/TransactionsPage.styl'
import {
ACCOUNT_DOCTYPE,
accountsConn,
groupsConn,
triggersConn,
transactionsConn
} from 'doctypes'
import TransactionHeader from 'ducks/transactions/TransactionHeader'
import { isCollectionLoading, hasBeenLoaded } from 'ducks/client/utils'
import { findNearestMonth } from 'ducks/transactions/helpers'
import { getChartTransactions } from 'ducks/chart/selectors'
import BarTheme from 'ducks/bar/BarTheme'
import TransactionActionsProvider from 'ducks/transactions/TransactionActionsProvider'
export const STEP_INFINITE_SCROLL = 30
export const MIN_NB_TRANSACTIONS_SHOWN = 30
const SCROLL_THRESOLD_TO_ACTIVATE_TOP_INFINITE_SCROLL = 150
const getMonth = date => date.slice(0, 7)
const FakeTransactions = () => <Padded>{null}</Padded>
class TransactionsPage extends Component {
constructor(props) {
super(props)
this.renderTransactions = this.renderTransactions.bind(this)
this.handleDecreaseLimitMin = this.handleDecreaseLimitMin.bind(this)
this.handleIncreaseLimitMax = this.handleIncreaseLimitMax.bind(this)
this.handleChangeMonth = this.handleChangeMonth.bind(this)
this.handleChangeTopmostTransaction = this.handleChangeTopmostTransaction.bind(
this
)
this.checkToActivateTopInfiniteScroll = this.checkToActivateTopInfiniteScroll.bind(
this
)
this.state = {
limitMin: 0,
limitMax: STEP_INFINITE_SCROLL,
infiniteScrollTop: false
}
}
setCurrentMonthFollowingMostRecentTransaction() {
const transactions = this.props.filteredTransactions
if (!transactions || transactions.length === 0) {
return
}
const mostRecentTransaction = maxBy(transactions, getDisplayDate)
if (!mostRecentTransaction) {
return
}
const mostRecentMonth = getMonth(getDisplayDate(mostRecentTransaction))
this.handleChangeMonth(mostRecentMonth)
}
componentDidUpdate(prevProps) {
if (!isEqual(this.props.filteringDoc, prevProps.filteringDoc)) {
this.setCurrentMonthFollowingMostRecentTransaction()
} else if (
isCollectionLoading(prevProps.transactions) &&
!isCollectionLoading(this.props.transactions)
) {
this.setCurrentMonthFollowingMostRecentTransaction()
}
}
handleChangeTopmostTransaction(transaction) {
this.setState({
currentMonth: getMonth(getDisplayDate(transaction))
})
}
handleIncreaseLimitMax() {
if (this.increasing) {
return
}
this.increasing = true
this.setState(
{
limitMax: this.state.limitMax + STEP_INFINITE_SCROLL
},
() => {
this.increasing = false
}
)
}
handleDecreaseLimitMin(amount = STEP_INFINITE_SCROLL) {
if (!this.increasing) {
return
}
this.increasing = true
const transactions = this.props.filteredTransactions
let goal = Math.max(this.state.limitMin - amount, 0)
// try not have a cut on the same day
while (
goal > 0 &&
getDate(transactions[goal]) === getDate(transactions[goal - 1])
) {
goal--
}
this.setState(
{
limitMin: goal
},
() => {
this.increasing = false
}
)
}
/**
* Updates this.state after
* - month change request from children
* - filtering doc change
*
* - Updates limitMin/limitMax slicing the transactions array so that
* we see only transactions for the selected month
* - Updates currentMonth to be `month`
*/
handleChangeMonth(month) {
const transactions = this.props.filteredTransactions
const findMonthIndex = month =>
findIndex(transactions, t => getDisplayDate(t).indexOf(month) === 0)
let limitMin = findMonthIndex(month)
if (limitMin == -1) {
const monthsWithOperations = uniq(
transactions.map(x => getMonth(getDisplayDate(x)))
).sort()
const nearestMonth = findNearestMonth(
month,
this.state.currentMonth,
monthsWithOperations
)
if (nearestMonth) {
month = nearestMonth
limitMin = findMonthIndex(month)
} else {
month = monthsWithOperations[0]
limitMin = 0
}
}
this.setState(
{
limitMin: limitMin,
limitMax: limitMin + MIN_NB_TRANSACTIONS_SHOWN,
currentMonth: month,
infiniteScrollTop: false
},
() => {
// need to scroll past the LoadMore button
if (isMobileApp()) {
const LoadMoreBtn = document.querySelector('.js-LoadMore')
const padding = 15
const scrollTo = LoadMoreBtn
? LoadMoreBtn.getBoundingClientRect().bottom +
window.scrollY -
padding
: 0
window.scrollTo(0, scrollTo)
}
}
)
}
checkToActivateTopInfiniteScroll(getScrollInfo) {
const scrollInfo = getScrollInfo()
if (scrollInfo.scroll > SCROLL_THRESOLD_TO_ACTIVATE_TOP_INFINITE_SCROLL) {
this.setState({ infiniteScrollTop: true })
}
}
getTransactions = () => {
const {
filteredTransactions,
router: {
params: { subcategoryName }
}
} = this.props
const categoryId = subcategoryName
? getCategoryIdFromName(subcategoryName)
: null
return getChartTransactions(filteredTransactions, categoryId)
}
getFilteringOnAccount = () => {
const { filteringDoc } = this.props
return filteringDoc && filteringDoc._type === ACCOUNT_DOCTYPE
}
renderTransactions() {
const { limitMin, limitMax, infiniteScrollTop } = this.state
const { t, transactions: transactionCol } = this.props
const isFetching =
isCollectionLoading(transactionCol) && !hasBeenLoaded(transactionCol)
if (isFetching) {
return <Loading loadingType="movements" />
}
const transactions = this.getTransactions()
const isOnSubcategory = onSubcategory(this.props)
if (transactions.length === 0) {
return (
<Padded className="u-pt-0">
<p>{t('Transactions.no-movements')}</p>
</Padded>
)
}
return (
<Delayed delay={0} fallback={<FakeTransactions />}>
<TransactionList
showTriggerErrors={isOnSubcategory}
limitMin={limitMin}
limitMax={limitMax}
onReachTop={this.handleDecreaseLimitMin}
onReachBottom={this.handleIncreaseLimitMax}
infiniteScrollTop={infiniteScrollTop}
onChangeTopMostTransaction={this.handleChangeTopmostTransaction}
onScroll={this.checkToActivateTopInfiniteScroll}
transactions={transactions}
filteringOnAccount={this.getFilteringOnAccount()}
manualLoadMore={isMobileApp()}
/>
</Delayed>
)
}
render() {
const {
accounts,
breakpoints: { isMobile }
} = this.props
const areAccountsLoading =
isCollectionLoading(accounts) && !hasBeenLoaded(accounts)
const filteredTransactions = this.getTransactions()
const isOnSubcategory = onSubcategory(this.props)
const theme = 'primary'
return (
<TransactionActionsProvider>
<BarTheme theme={theme} />
<TransactionHeader
transactions={filteredTransactions}
handleChangeMonth={this.handleChangeMonth}
currentMonth={this.state.currentMonth}
showBackButton={this.props.showBackButton}
showBalance={isMobile && !areAccountsLoading && !isOnSubcategory}
/>
<div
className={`${styles.TransactionPage__transactions} js-scrolling-element`}
>
{this.renderTransactions()}
</div>
</TransactionActionsProvider>
)
}
}
const onSubcategory = ownProps => ownProps.router.params.subcategoryName
const mapStateToProps = (state, ownProps) => {
const filteredTransactions = onSubcategory(ownProps)
? getFilteredTransactions(state, ownProps)
: getTransactionsFilteredByAccount(state)
return {
filteringDoc: getFilteringDoc(state),
filteredTransactions: filteredTransactions
}
}
export const DumbTransactionsPage = TransactionsPage
export const UnpluggedTransactionsPage = compose(
withRouter,
translate(),
withBreakpoints()
)(TransactionsPage)
UnpluggedTransactionsPage.propTypes = {
filteredTransactions: PropTypes.array.isRequired
}
const ConnectedTransactionsPage = compose(
withRouter,
queryConnect({
accounts: accountsConn,
groups: groupsConn,
triggers: triggersConn,
transactions: transactionsConn
}),
connect(mapStateToProps)
)(UnpluggedTransactionsPage)
export const TransactionsPageWithBackButton = props => (
<ConnectedTransactionsPage {...props} showBackButton />
)
export default ConnectedTransactionsPage