Skip to content

Commit 821bc1d

Browse files
committed
split email options for wpcom and jetpack. add toggle to switch entire section on or off
1 parent 347323e commit 821bc1d

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

client/me/notification-settings/wpcom-settings/index.jsx

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Card, LoadingPlaceholder } from '@automattic/components';
2+
import { ToggleControl } from '@wordpress/components';
23
import { localize } from 'i18n-calypso';
34
import { get } from 'lodash';
45
import { Component } from 'react';
@@ -9,8 +10,15 @@ import NavigationHeader from 'calypso/components/navigation-header';
910
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
1011
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
1112
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';
1422
import hasJetpackSites from 'calypso/state/selectors/has-jetpack-sites';
1523
import Navigation from '../navigation';
1624
import SubscriptionManagementBackButton from '../subscription-management-back-button';
@@ -19,7 +27,7 @@ import EmailCategory from './email-category';
1927
import './style.scss';
2028

2129
/**
22-
* Module variables
30+
* Module variables for wpcom
2331
*/
2432
const options = {
2533
marketing: 'marketing',
@@ -31,6 +39,12 @@ const options = {
3139
reports: 'reports',
3240
news_developer: 'news_developer',
3341
scheduled_updates: 'scheduled_updates',
42+
};
43+
44+
/**
45+
* Module variables for jetpack
46+
*/
47+
const jetpackOptions = {
3448
jetpack_marketing: 'jetpack_marketing',
3549
jetpack_research: 'jetpack_research',
3650
jetpack_promotion: 'jetpack_promotion',
@@ -47,6 +61,24 @@ class WPCOMNotifications extends Component {
4761
this.props.fetchSettings();
4862
}
4963

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+
5082
renderWpcomPreferences = () => {
5183
const { settings, translate } = this.props;
5284

@@ -63,6 +95,19 @@ class WPCOMNotifications extends Component {
6395
{ this.props.translate( 'Email from WordPress.com' ) }
6496
</FormSectionHeading>
6597

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+
66111
<EmailCategory
67112
name={ options.marketing }
68113
isEnabled={ get( settings, options.marketing ) }
@@ -148,39 +193,52 @@ class WPCOMNotifications extends Component {
148193
) }
149194
</p>
150195

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+
151209
<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 ) }
154212
title={ translate( 'Suggestions' ) }
155213
description={ translate( 'Tips for getting the most out of Jetpack.' ) }
156214
/>
157215

158216
<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 ) }
161219
title={ translate( 'Research' ) }
162220
description={ translate(
163221
'Opportunities to participate in Jetpack research and surveys.'
164222
) }
165223
/>
166224

167225
<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 ) }
170228
title={ translate( 'Promotions' ) }
171229
description={ translate( 'Sales and promotions for Jetpack products and services.' ) }
172230
/>
173231

174232
<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 ) }
177235
title={ translate( 'Newsletter' ) }
178236
description={ translate( 'Jetpack news, announcements, and product spotlights.' ) }
179237
/>
180238

181239
<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 ) }
184242
title={ translate( 'Reports' ) }
185243
description={ translate( 'Jetpack security and performance reports.' ) }
186244
/>
@@ -224,6 +282,7 @@ export default connect(
224282
( state ) => ( {
225283
settings: getNotificationSettings( state, 'wpcom' ),
226284
hasJetpackSites: hasJetpackSites( state ),
285+
isFetching: isFetchingNotificationsSettings( state ),
227286
} ),
228-
{ fetchSettings }
287+
{ fetchSettings, toggleWPcomEmailSetting, saveSettings }
229288
)( localize( WPCOMNotifications ) );
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
.wpcom-settings__notification-settings-emailcategory {
22
margin-bottom: 20px;
3-
}
3+
}
4+
5+
.wpcom-settings__notification-settings-emailsection-toggle {
6+
margin-bottom: 40px;
7+
8+
.components-toggle-control__label {
9+
font-size: 16px;
10+
font-weight: 600;
11+
}
12+
}

0 commit comments

Comments
 (0)