4
4
* External dependencies
5
5
*/
6
6
import React from 'react' ;
7
+ import PropTypes from 'prop-types' ;
7
8
import closest from 'component-closest' ;
8
9
import { connect } from 'react-redux' ;
9
- import { last , map , range , uniq } from 'lodash' ;
10
10
import { localize } from 'i18n-calypso' ;
11
+ import { find , isEqual } from 'lodash' ;
11
12
12
13
/**
13
14
* Internal dependencies
@@ -16,8 +17,13 @@ import SelectDropdown from 'components/select-dropdown';
16
17
import DropdownItem from 'components/select-dropdown/item' ;
17
18
import DropdownLabel from 'components/select-dropdown/label' ;
18
19
import DropdownSeparator from 'components/select-dropdown/separator' ;
19
- import tableRows from './table-rows' ;
20
20
import { recordGoogleEvent } from 'state/analytics/actions' ;
21
+ import { setApp , setDate } from 'state/ui/billing-transactions/actions' ;
22
+ import {
23
+ getBillingTransactionAppFilterValues ,
24
+ getBillingTransactionDateFilterValues ,
25
+ getBillingTransactionFilters ,
26
+ } from 'state/selectors' ;
21
27
22
28
class TransactionsHeader extends React . Component {
23
29
state = {
@@ -41,17 +47,20 @@ class TransactionsHeader extends React.Component {
41
47
this . props . recordGoogleEvent ( 'Me' , 'Clicked on ' + action ) ;
42
48
} ;
43
49
44
- getDatePopoverItemClickHandler ( analyticsEvent , filter ) {
50
+ getDatePopoverItemClickHandler ( analyticsEvent , date ) {
45
51
return ( ) => {
52
+ const { transactionType } = this . props ;
46
53
this . recordClickEvent ( 'Date Popover Item: ' + analyticsEvent ) ;
47
- this . handlePickerSelection ( filter ) ;
54
+ this . props . setDate ( transactionType , date . month , date . operator ) ;
55
+ this . setState ( { activePopover : '' } ) ;
48
56
} ;
49
57
}
50
58
51
- getAppPopoverItemClickHandler ( analyticsEvent , filter ) {
59
+ getAppPopoverItemClickHandler ( analyticsEvent , app ) {
52
60
return ( ) => {
53
61
this . recordClickEvent ( 'App Popover Item: ' + analyticsEvent ) ;
54
- this . handlePickerSelection ( filter ) ;
62
+ this . props . setApp ( this . props . transactionType , app ) ;
63
+ this . setState ( { activePopover : '' } ) ;
55
64
} ;
56
65
}
57
66
@@ -89,32 +98,30 @@ class TransactionsHeader extends React.Component {
89
98
) ;
90
99
}
91
100
92
- setFilter ( filter ) {
93
- this . setState ( { activePopover : '' } ) ;
94
- this . props . onNewFilter ( filter ) ;
95
- }
96
-
97
101
renderDatePopover ( ) {
98
- const previousMonths = range ( 6 ) . map ( function ( n ) {
99
- return this . props . moment ( ) . subtract ( n , 'months' ) ;
100
- } , this ) ;
102
+ const { dateFilters , filter , translate } = this . props ;
103
+ const selectedFilter = find ( dateFilters , { value : filter . date } ) ;
104
+ const selectedText = selectedFilter ? selectedFilter . title : translate ( 'Date' ) ;
101
105
102
106
return (
103
107
< SelectDropdown
104
- selectedText = { this . props . translate ( 'Date' ) }
108
+ selectedText = { selectedText }
105
109
onClick = { this . handleAppsPopoverLinkClick }
106
110
className = "billing-history__transactions-header-select-dropdown"
107
111
>
108
- < DropdownLabel > { this . props . translate ( 'Recent Transactions' ) } </ DropdownLabel >
109
- { this . renderDatePicker ( '5 Newest' , this . props . translate ( '5 Newest' ) , {
110
- newest : 5 ,
111
- } ) }
112
- { this . renderDatePicker ( '10 Newest' , this . props . translate ( '10 Newest' ) , {
113
- newest : 10 ,
114
- } ) }
112
+ < DropdownLabel > { translate ( 'Recent Transactions' ) } </ DropdownLabel >
113
+ { this . renderDatePicker (
114
+ 'Newest' ,
115
+ translate ( 'Newest' ) ,
116
+ {
117
+ month : null ,
118
+ operator : null ,
119
+ } ,
120
+ null
121
+ ) }
115
122
< DropdownSeparator />
116
- < DropdownLabel > { this . props . translate ( 'By Month' ) } </ DropdownLabel >
117
- { previousMonths . map ( function ( month , index ) {
123
+ < DropdownLabel > { translate ( 'By Month' ) } </ DropdownLabel >
124
+ { dateFilters . map ( function ( { count , title , value } , index ) {
118
125
let analyticsEvent = 'Current Month' ;
119
126
120
127
if ( 1 === index ) {
@@ -123,17 +130,8 @@ class TransactionsHeader extends React.Component {
123
130
analyticsEvent = index + ' Months Before' ;
124
131
}
125
132
126
- return this . renderDatePicker (
127
- month . format ( 'MMM YYYY' ) ,
128
- month . format ( 'MMM YYYY' ) ,
129
- { month : month } ,
130
- analyticsEvent
131
- ) ;
133
+ return this . renderDatePicker ( index , title , value , count , analyticsEvent ) ;
132
134
} , this ) }
133
-
134
- { this . renderDatePicker ( 'Older' , this . props . translate ( 'Older' ) , {
135
- before : last ( previousMonths ) ,
136
- } ) }
137
135
</ SelectDropdown >
138
136
) ;
139
137
}
@@ -149,85 +147,77 @@ class TransactionsHeader extends React.Component {
149
147
this . setState ( { activePopover : activePopover } ) ;
150
148
}
151
149
152
- renderDatePicker ( titleKey , titleTranslated , date , analyticsEvent ) {
153
- const filter = { date } ;
154
- const currentDate = this . props . filter . date || { } ;
155
- let isSelected ;
156
-
157
- if ( date . newest ) {
158
- isSelected = date . newest === currentDate . newest ;
159
- } else if ( date . month && currentDate . month ) {
160
- isSelected = date . month . isSame ( currentDate . month , 'month' ) ;
161
- } else if ( date . before ) {
162
- isSelected = Boolean ( currentDate . before ) ;
163
- } else {
164
- isSelected = false ;
165
- }
166
-
150
+ renderDatePicker ( titleKey , titleTranslated , value , count , analyticsEvent ) {
151
+ const currentDate = this . props . filter . date ;
152
+ const isSelected = isEqual ( currentDate , value ) ;
167
153
analyticsEvent = 'undefined' === typeof analyticsEvent ? titleKey : analyticsEvent ;
168
154
169
155
return (
170
156
< DropdownItem
171
157
key = { titleKey }
172
158
selected = { isSelected }
173
- onClick = { this . getDatePopoverItemClickHandler ( analyticsEvent , filter ) }
174
- count = { date . newest ? null : this . getFilterCount ( filter ) }
159
+ onClick = { this . getDatePopoverItemClickHandler ( analyticsEvent , value ) }
160
+ count = { count }
175
161
>
176
162
{ titleTranslated }
177
163
</ DropdownItem >
178
164
) ;
179
165
}
180
166
181
- handlePickerSelection ( filter ) {
182
- this . setFilter ( filter ) ;
183
- this . setState ( { searchValue : '' } ) ;
184
- }
185
-
186
- getFilterCount ( filter ) {
187
- if ( ! this . props . transactions ) {
188
- return ;
189
- }
190
-
191
- return tableRows . filter ( this . props . transactions , filter ) . length ;
192
- }
193
-
194
167
renderAppsPopover ( ) {
168
+ const { appFilters, filter, translate } = this . props ;
169
+ const selectedFilter = find ( appFilters , { value : filter . app } ) ;
170
+ const selectedText = selectedFilter ? selectedFilter . title : translate ( 'All Apps' ) ;
171
+
195
172
return (
196
173
< SelectDropdown
197
- selectedText = { this . props . translate ( 'All Apps' ) }
174
+ selectedText = { selectedText }
198
175
onClick = { this . handleAppsPopoverLinkClick }
199
176
className = "billing-history__transactions-header-select-dropdown"
200
177
>
201
- < DropdownLabel > { this . props . translate ( 'App Name' ) } </ DropdownLabel >
202
- { this . renderAppPicker ( this . props . translate ( 'All Apps' ) , 'all' ) }
203
- { this . getApps ( ) . map ( function ( app ) {
204
- return this . renderAppPicker ( app , app , 'Specific App' ) ;
178
+ < DropdownLabel > { translate ( 'App Name' ) } </ DropdownLabel >
179
+ { this . renderAppPicker ( translate ( 'All Apps' ) , 'all' ) }
180
+ { appFilters . map ( function ( { title , value , count } ) {
181
+ return this . renderAppPicker ( title , value , count , 'Specific App' ) ;
205
182
} , this ) }
206
183
</ SelectDropdown >
207
184
) ;
208
185
}
209
186
210
- getApps ( ) {
211
- return uniq ( map ( this . props . transactions , 'service' ) ) ;
212
- }
213
-
214
- renderAppPicker ( title , app , analyticsEvent ) {
215
- const filter = { app } ;
187
+ renderAppPicker ( title , app , count , analyticsEvent ) {
216
188
const selected = app === this . props . filter . app ;
217
189
218
190
return (
219
191
< DropdownItem
220
192
key = { app }
221
193
selected = { selected }
222
- onClick = { this . getAppPopoverItemClickHandler ( analyticsEvent , filter ) }
223
- count = { this . getFilterCount ( filter ) }
194
+ onClick = { this . getAppPopoverItemClickHandler ( analyticsEvent , app ) }
195
+ count = { count }
224
196
>
225
197
{ title }
226
198
</ DropdownItem >
227
199
) ;
228
200
}
229
201
}
230
202
231
- export default connect ( null , {
232
- recordGoogleEvent,
233
- } ) ( localize ( TransactionsHeader ) ) ;
203
+ TransactionsHeader . propTypes = {
204
+ //connected props
205
+ appFilters : PropTypes . array . isRequired ,
206
+ dateFilters : PropTypes . array . isRequired ,
207
+ filter : PropTypes . object . isRequired ,
208
+ //own props
209
+ transactionType : PropTypes . string . isRequired ,
210
+ } ;
211
+
212
+ export default connect (
213
+ ( state , { transactionType } ) => ( {
214
+ appFilters : getBillingTransactionAppFilterValues ( state , transactionType ) ,
215
+ dateFilters : getBillingTransactionDateFilterValues ( state , transactionType ) ,
216
+ filter : getBillingTransactionFilters ( state , transactionType ) ,
217
+ } ) ,
218
+ {
219
+ recordGoogleEvent,
220
+ setApp,
221
+ setDate,
222
+ }
223
+ ) ( localize ( TransactionsHeader ) ) ;
0 commit comments