-
Notifications
You must be signed in to change notification settings - Fork 44
/
CategoriesPage.jsx
187 lines (171 loc) · 5.46 KB
/
CategoriesPage.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
import React, { Component, Fragment } from 'react'
import { withRouter } from 'react-router'
import { connect } from 'react-redux'
import { translate, withBreakpoints } from 'cozy-ui/transpiled/react'
import Loading from 'components/Loading'
import { Padded } from 'components/Spacing'
import {
resetFilterByDoc,
addFilterByPeriod,
getFilteringDoc,
getTransactionsFilteredByAccount
} from 'ducks/filters'
import { getDefaultedSettingsFromCollection } from 'ducks/settings/helpers'
import Categories from 'ducks/categories/Categories'
import { flowRight as compose, sortBy, some, includes } from 'lodash'
import CategoriesHeader from 'ducks/categories/CategoriesHeader'
import { queryConnect } from 'cozy-client'
import { withClient } from 'cozy-client'
import {
accountsConn,
settingsConn,
transactionsConn,
groupsConn
} from 'doctypes'
import { isCollectionLoading, hasBeenLoaded } from 'ducks/client/utils'
import BarTheme from 'ducks/bar/BarTheme'
import { getCategoriesData } from 'ducks/categories/selectors'
import maxBy from 'lodash/maxBy'
import { getDate } from 'ducks/transactions/helpers'
const isCategoryDataEmpty = categoryData => {
return categoryData[0] && isNaN(categoryData[0].percentage)
}
class CategoriesPage extends Component {
componentDidMount() {
const { filteringDoc, resetFilterByDoc } = this.props
if (
filteringDoc &&
includes(['Reimbursements', 'health_reimbursements'], filteringDoc._id)
) {
resetFilterByDoc()
}
this.checkToChangeFilter()
}
componentDidUpdate(prevProps) {
if (
!prevProps.transactions.lastUpdate &&
this.props.transactions.lastUpdate
) {
this.checkToChangeFilter()
}
}
checkToChangeFilter() {
// If we do not have any data to show, change the period filter
// to the latest period available for the current account
if (isCategoryDataEmpty(this.props.categories)) {
const transactions = this.props.filteredTransactionsByAccount
if (transactions && transactions.length > 0) {
const maxDate = getDate(maxBy(transactions, getDate))
this.props.addFilterByPeriod(maxDate.slice(0, 7))
}
}
}
selectCategory = (selectedCategory, subcategory) => {
if (subcategory) {
this.props.router.push(`/categories/${selectedCategory}/${subcategory}`)
} else if (selectedCategory) {
this.props.router.push(`/categories/${selectedCategory}`)
} else {
this.props.router.push('/categories')
}
}
onWithIncomeToggle = checked => {
const { client } = this.props
const settings = this.getSettings()
settings.showIncomeCategory = checked
client.save(settings)
}
getSettings = () => {
const { settings: settingsCollection } = this.props
return getDefaultedSettingsFromCollection(settingsCollection)
}
render() {
const {
t,
categories: categoriesProps,
transactions,
router,
accounts,
settings
} = this.props
const isFetching = some(
[accounts, transactions, settings],
col => isCollectionLoading(col) && !hasBeenLoaded(col)
)
const { showIncomeCategory } = this.getSettings()
const selectedCategoryName = router.params.categoryName
const categories = showIncomeCategory
? categoriesProps
: categoriesProps.filter(category => category.name !== 'incomeCat')
const breadcrumbItems = [{ name: t('Categories.title.general') }]
if (selectedCategoryName) {
breadcrumbItems[0].onClick = () => router.push('/categories')
breadcrumbItems.push({
name: t(`Data.categories.${selectedCategoryName}`)
})
}
const selectedCategory = categories.find(
category => category.name === selectedCategoryName
)
const sortedCategories = sortBy(categories, cat =>
Math.abs(cat.amount)
).reverse()
const hasAccount = accounts.data.length > 0
return (
<Fragment>
<BarTheme theme="primary" />
<CategoriesHeader
breadcrumbItems={breadcrumbItems}
selectedCategory={selectedCategory}
withIncome={showIncomeCategory}
onWithIncomeToggle={this.onWithIncomeToggle}
categories={sortedCategories}
isFetching={isFetching}
hasAccount={hasAccount}
/>
{hasAccount &&
(isFetching ? (
<Padded className="u-pt-0">
<Loading loadingType="categories" />
</Padded>
) : (
<Categories
categories={sortedCategories}
selectedCategory={selectedCategory}
selectedCategoryName={selectedCategoryName}
selectCategory={this.selectCategory}
withIncome={showIncomeCategory}
filterWithInCome={this.filterWithInCome}
/>
))}
</Fragment>
)
}
}
const mapDispatchToProps = dispatch => ({
resetFilterByDoc: () => dispatch(resetFilterByDoc()),
addFilterByPeriod: period => dispatch(addFilterByPeriod(period))
})
const mapStateToProps = (state, ownProps) => {
return {
categories: getCategoriesData(state, ownProps),
filteringDoc: getFilteringDoc(state),
filteredTransactionsByAccount: getTransactionsFilteredByAccount(state)
}
}
export default compose(
withRouter,
withBreakpoints(),
translate(),
withClient,
queryConnect({
accounts: accountsConn,
transactions: transactionsConn,
settings: settingsConn,
groups: groupsConn
}),
connect(
mapStateToProps,
mapDispatchToProps
)
)(CategoriesPage)