1
1
import { Card , LoadingPlaceholder } from '@automattic/components' ;
2
+ import { ToggleControl } from '@wordpress/components' ;
2
3
import { localize } from 'i18n-calypso' ;
3
4
import { get } from 'lodash' ;
4
5
import { Component } from 'react' ;
@@ -9,8 +10,15 @@ import NavigationHeader from 'calypso/components/navigation-header';
9
10
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker' ;
10
11
import twoStepAuthorization from 'calypso/lib/two-step-authorization' ;
11
12
import ReauthRequired from 'calypso/me/reauth-required' ;
12
- import { fetchSettings } from 'calypso/state/notification-settings/actions' ;
13
- import { getNotificationSettings } from 'calypso/state/notification-settings/selectors' ;
13
+ import {
14
+ fetchSettings ,
15
+ toggleWPcomEmailSetting ,
16
+ saveSettings ,
17
+ } from 'calypso/state/notification-settings/actions' ;
18
+ import {
19
+ getNotificationSettings ,
20
+ isFetchingNotificationsSettings ,
21
+ } from 'calypso/state/notification-settings/selectors' ;
14
22
import hasJetpackSites from 'calypso/state/selectors/has-jetpack-sites' ;
15
23
import Navigation from '../navigation' ;
16
24
import SubscriptionManagementBackButton from '../subscription-management-back-button' ;
@@ -19,7 +27,7 @@ import EmailCategory from './email-category';
19
27
import './style.scss' ;
20
28
21
29
/**
22
- * Module variables
30
+ * Module variables for wpcom
23
31
*/
24
32
const options = {
25
33
marketing : 'marketing' ,
@@ -31,6 +39,12 @@ const options = {
31
39
reports : 'reports' ,
32
40
news_developer : 'news_developer' ,
33
41
scheduled_updates : 'scheduled_updates' ,
42
+ } ;
43
+
44
+ /**
45
+ * Module variables for jetpack
46
+ */
47
+ const jetpackOptions = {
34
48
jetpack_marketing : 'jetpack_marketing' ,
35
49
jetpack_research : 'jetpack_research' ,
36
50
jetpack_promotion : 'jetpack_promotion' ,
@@ -47,6 +61,24 @@ class WPCOMNotifications extends Component {
47
61
this . props . fetchSettings ( ) ;
48
62
}
49
63
64
+ unsubscribeFromAll = ( productOptions ) => ( toggleValue ) => {
65
+ const toggledSettings = { } ;
66
+ Object . keys ( productOptions ) . forEach ( ( option ) => {
67
+ if ( this . props . settings [ option ] !== toggleValue ) {
68
+ toggledSettings [ option ] = toggleValue ;
69
+ this . props . toggleWPcomEmailSetting ( option ) ;
70
+ }
71
+ } ) ;
72
+
73
+ this . props . saveSettings ( 'wpcom' , toggledSettings ) ;
74
+ } ;
75
+
76
+ toggleShouldBeOff = ( productOptions ) => {
77
+ return Object . keys ( productOptions ) . some (
78
+ ( key ) => key in this . props . settings && this . props . settings [ key ] === true
79
+ ) ;
80
+ } ;
81
+
50
82
renderWpcomPreferences = ( ) => {
51
83
const { settings, translate } = this . props ;
52
84
@@ -63,6 +95,19 @@ class WPCOMNotifications extends Component {
63
95
{ this . props . translate ( 'Email from WordPress.com' ) }
64
96
</ FormSectionHeading >
65
97
98
+ < ToggleControl
99
+ __nextHasNoMarginBottom
100
+ checked = { this . toggleShouldBeOff ( options ) }
101
+ className = "wpcom-settings__notification-settings-emailsection-toggle"
102
+ label = {
103
+ this . toggleShouldBeOff ( options )
104
+ ? this . props . translate ( 'Unsubscribe from all' )
105
+ : this . props . translate ( 'Subscribe to all' )
106
+ }
107
+ onChange = { this . unsubscribeFromAll ( options ) }
108
+ disabled = { this . props . isFetching }
109
+ />
110
+
66
111
< EmailCategory
67
112
name = { options . marketing }
68
113
isEnabled = { get ( settings , options . marketing ) }
@@ -148,39 +193,52 @@ class WPCOMNotifications extends Component {
148
193
) }
149
194
</ p >
150
195
196
+ < ToggleControl
197
+ __nextHasNoMarginBottom
198
+ checked = { this . toggleShouldBeOff ( jetpackOptions ) }
199
+ className = "wpcom-settings__notification-settings-emailsection-toggle"
200
+ label = {
201
+ this . toggleShouldBeOff ( jetpackOptions )
202
+ ? this . props . translate ( 'Unsubscribe from all' )
203
+ : this . props . translate ( 'Subscribe to all' )
204
+ }
205
+ onChange = { this . unsubscribeFromAll ( jetpackOptions ) }
206
+ disabled = { this . props . isFetching }
207
+ />
208
+
151
209
< EmailCategory
152
- name = { options . jetpack_marketing }
153
- isEnabled = { get ( settings , options . jetpack_marketing ) }
210
+ name = { jetpackOptions . jetpack_marketing }
211
+ isEnabled = { get ( settings , jetpackOptions . jetpack_marketing ) }
154
212
title = { translate ( 'Suggestions' ) }
155
213
description = { translate ( 'Tips for getting the most out of Jetpack.' ) }
156
214
/>
157
215
158
216
< EmailCategory
159
- name = { options . jetpack_research }
160
- isEnabled = { get ( settings , options . jetpack_research ) }
217
+ name = { jetpackOptions . jetpack_research }
218
+ isEnabled = { get ( settings , jetpackOptions . jetpack_research ) }
161
219
title = { translate ( 'Research' ) }
162
220
description = { translate (
163
221
'Opportunities to participate in Jetpack research and surveys.'
164
222
) }
165
223
/>
166
224
167
225
< EmailCategory
168
- name = { options . jetpack_promotion }
169
- isEnabled = { get ( settings , options . jetpack_promotion ) }
226
+ name = { jetpackOptions . jetpack_promotion }
227
+ isEnabled = { get ( settings , jetpackOptions . jetpack_promotion ) }
170
228
title = { translate ( 'Promotions' ) }
171
229
description = { translate ( 'Sales and promotions for Jetpack products and services.' ) }
172
230
/>
173
231
174
232
< EmailCategory
175
- name = { options . jetpack_news }
176
- isEnabled = { get ( settings , options . jetpack_news ) }
233
+ name = { jetpackOptions . jetpack_news }
234
+ isEnabled = { get ( settings , jetpackOptions . jetpack_news ) }
177
235
title = { translate ( 'Newsletter' ) }
178
236
description = { translate ( 'Jetpack news, announcements, and product spotlights.' ) }
179
237
/>
180
238
181
239
< EmailCategory
182
- name = { options . jetpack_reports }
183
- isEnabled = { get ( settings , options . jetpack_reports ) }
240
+ name = { jetpackOptions . jetpack_reports }
241
+ isEnabled = { get ( settings , jetpackOptions . jetpack_reports ) }
184
242
title = { translate ( 'Reports' ) }
185
243
description = { translate ( 'Jetpack security and performance reports.' ) }
186
244
/>
@@ -224,6 +282,7 @@ export default connect(
224
282
( state ) => ( {
225
283
settings : getNotificationSettings ( state , 'wpcom' ) ,
226
284
hasJetpackSites : hasJetpackSites ( state ) ,
285
+ isFetching : isFetchingNotificationsSettings ( state ) ,
227
286
} ) ,
228
- { fetchSettings }
287
+ { fetchSettings, toggleWPcomEmailSetting , saveSettings }
229
288
) ( localize ( WPCOMNotifications ) ) ;
0 commit comments