File size: 14,280 Bytes
1e92f2d |
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
import { Card, FormLabel } from '@automattic/components';
import { getNumericFirstDayOfWeek, withLocale } from '@automattic/i18n-utils';
import {
Button,
CheckboxControl,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { localize } from 'i18n-calypso';
import { flowRight as compose } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import QueryReaderTeams from 'calypso/components/data/query-reader-teams';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormLegend from 'calypso/components/forms/form-legend';
import FormSectionHeading from 'calypso/components/forms/form-section-heading';
import FormSelect from 'calypso/components/forms/form-select';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import { protectForm } from 'calypso/lib/protect-form';
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
import withFormBase from 'calypso/me/form-base/with-form-base';
import Navigation from 'calypso/me/notification-settings/navigation';
import ReauthRequired from 'calypso/me/reauth-required';
import { useSiteSubscriptions } from 'calypso/reader/following/use-site-subscriptions';
import { isAutomatticTeamMember } from 'calypso/reader/lib/teams';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { getReaderTeams } from 'calypso/state/teams/selectors';
import SubscriptionManagementBackButton from '../subscription-management-back-button';
class NotificationSubscriptions extends Component {
state = {
showConfirmModal: false,
};
checkboxNameToActionMap = {
subscription_delivery_jabber_default: 'Notification delivery by Jabber',
subscription_delivery_email_blocked: 'Block All Email Updates',
p2_disable_autofollow_on_comment: 'Enable auto-follow P2 upon comment',
};
handleClickEvent( action ) {
return () => this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action );
}
handleFocusEvent( action ) {
return () => this.props.recordGoogleEvent( 'Me', 'Focused on ' + action );
}
handleCheckboxEvent( action, invert = false ) {
return ( checked ) => {
const optionValue = invert ? ! checked : checked;
// Create a synthetic event object that matches what updateSetting expects
const syntheticEvent = {
currentTarget: {
name: action,
value: optionValue,
},
};
this.props.toggleSetting( syntheticEvent );
const actionLabel = this.checkboxNameToActionMap[ action ];
this.props.recordGoogleEvent(
'Me',
`Clicked ${ actionLabel } checkbox`,
'checked',
+optionValue
);
};
}
handleSubmit = ( event ) => {
event.preventDefault();
const isBlockingEmails = this.props.getSetting( 'subscription_delivery_email_blocked' );
const { hasSubscriptions } = this.props;
if ( isBlockingEmails && hasSubscriptions ) {
this.setState( { showConfirmModal: true } );
return;
}
this.props.submitForm( event );
};
handleSubmitButtonClick = ( event ) => {
this.props.recordGoogleEvent( 'Me', 'Clicked on Save Notification Settings Button' );
this.handleSubmit( event );
};
handleModalCancel = () => {
// Create a synthetic event object that matches what updateSetting expects
const syntheticEvent = {
currentTarget: {
name: 'subscription_delivery_email_blocked',
value: false,
},
};
this.props.updateSetting( syntheticEvent );
this.setState( { showConfirmModal: false } );
};
handleModalConfirm = () => {
// Create a synthetic event object that matches what updateSetting expects
const syntheticEvent = {
preventDefault: () => {},
};
this.props.submitForm( syntheticEvent );
this.setState( { showConfirmModal: false } );
};
getDeliveryHourLabel( hour ) {
return this.props.translate( '%(fromHour)s - %(toHour)s', {
context: 'Hour range between which subscriptions are delivered',
args: {
fromHour: this.props.moment().hour( hour ).minute( 0 ).format( 'LT' ),
toHour: this.props
.moment()
.hour( hour + 2 )
.minute( 0 )
.format( 'LT' ),
},
} );
}
renderLocalizedWeekdayOptions() {
const { translate, locale } = this.props;
const startOfWeek = getNumericFirstDayOfWeek( locale );
const weekDays = [
{ value: '1', label: translate( 'Monday' ) },
{ value: '2', label: translate( 'Tuesday' ) },
{ value: '3', label: translate( 'Wednesday' ) },
{ value: '4', label: translate( 'Thursday' ) },
{ value: '5', label: translate( 'Friday' ) },
{ value: '6', label: translate( 'Saturday' ) },
{ value: '0', label: translate( 'Sunday' ) },
];
// Rotate the array based on startOfWeek
const rotatedWeekdays = [
...weekDays.slice( startOfWeek - 1 ),
...weekDays.slice( 0, startOfWeek - 1 ),
];
return (
<>
{ rotatedWeekdays.map( ( { value, label } ) => (
<option key={ value } value={ value }>
{ label }
</option>
) ) }
</>
);
}
render() {
const { teams } = this.props;
const isAutomattician = isAutomatticTeamMember( teams );
return (
<Main wideLayout className="reader-subscriptions__notifications-settings">
<QueryReaderTeams />
<PageViewTracker
path="/me/notifications/subscriptions"
title="Me > Notifications > Subscriptions Delivery"
/>
<ReauthRequired twoStepAuthorization={ twoStepAuthorization } />
<SubscriptionManagementBackButton />
<NavigationHeader
navigationItems={ [] }
title={ this.props.translate( 'Notification Settings' ) }
/>
<Navigation path={ this.props.path } />
<Card className="reader-subscriptions__notification-settings">
<form
id="notification-settings"
onChange={ this.props.markChanged }
onSubmit={ this.handleSubmit }
>
<FormSectionHeading>
{ this.props.translate( 'Subscription settings' ) }
</FormSectionHeading>
<p>
{ this.props.translate(
'To manage individual site subscriptions, {{readerLink}}go to the Reader{{/readerLink}}.',
{
components: {
readerLink: (
<a
href="/reader/subscriptions"
onClick={ this.handleClickEvent( 'Edit Subscriptions in Reader Link' ) }
/>
),
},
}
) }
</p>
<FormFieldset>
<FormLabel htmlFor="subscription_delivery_email_default">
{ this.props.translate( 'Default email delivery' ) }
</FormLabel>
<FormSelect
disabled={ this.props.getDisabledState() }
id="subscription_delivery_email_default"
name="subscription_delivery_email_default"
onChange={ this.props.updateSetting }
onFocus={ this.handleFocusEvent( 'Default Email Delivery' ) }
value={ this.props.getSetting( 'subscription_delivery_email_default' ) }
>
<option value="never">{ this.props.translate( 'Never send email' ) }</option>
<option value="instantly">
{ this.props.translate( 'Send email instantly' ) }
</option>
<option value="daily">{ this.props.translate( 'Send email daily' ) }</option>
<option value="weekly">{ this.props.translate( 'Send email every week' ) }</option>
</FormSelect>
</FormFieldset>
<FormFieldset>
<FormLabel htmlFor="subscription_delivery_mail_option">
{ this.props.translate( 'Email delivery format' ) }
</FormLabel>
<FormSelect
disabled={ this.props.getDisabledState() }
id="subscription_delivery_mail_option"
name="subscription_delivery_mail_option"
onChange={ this.props.updateSetting }
onFocus={ this.handleFocusEvent( 'Email delivery format' ) }
value={ this.props.getSetting( 'subscription_delivery_mail_option' ) }
>
<option value="html">{ this.props.translate( 'Visual (HTML)' ) }</option>
<option value="text">{ this.props.translate( 'Plain text' ) }</option>
</FormSelect>
</FormFieldset>
<FormFieldset>
<FormLabel htmlFor="subscription_delivery_day">
{ this.props.translate( 'Email delivery window' ) }
</FormLabel>
<FormSelect
disabled={ this.props.getDisabledState() }
className="reader-subscriptions__delivery-window"
id="subscription_delivery_day"
name="subscription_delivery_day"
onChange={ this.props.updateSetting }
onFocus={ this.handleFocusEvent( 'Email delivery window day' ) }
value={ this.props.getSetting( 'subscription_delivery_day' ) }
>
{ this.renderLocalizedWeekdayOptions() }
</FormSelect>
<FormSelect
disabled={ this.props.getDisabledState() }
id="subscription_delivery_hour"
name="subscription_delivery_hour"
onChange={ this.props.updateSetting }
onFocus={ this.handleFocusEvent( 'Email Delivery Window Time' ) }
value={ this.props.getSetting( 'subscription_delivery_hour' ) }
>
<option value="0">{ this.getDeliveryHourLabel( 0 ) }</option>
<option value="2">{ this.getDeliveryHourLabel( 2 ) }</option>
<option value="4">{ this.getDeliveryHourLabel( 4 ) }</option>
<option value="6">{ this.getDeliveryHourLabel( 6 ) }</option>
<option value="8">{ this.getDeliveryHourLabel( 8 ) }</option>
<option value="10">{ this.getDeliveryHourLabel( 10 ) }</option>
<option value="12">{ this.getDeliveryHourLabel( 12 ) }</option>
<option value="14">{ this.getDeliveryHourLabel( 14 ) }</option>
<option value="16">{ this.getDeliveryHourLabel( 16 ) }</option>
<option value="18">{ this.getDeliveryHourLabel( 18 ) }</option>
<option value="20">{ this.getDeliveryHourLabel( 20 ) }</option>
<option value="22">{ this.getDeliveryHourLabel( 22 ) }</option>
</FormSelect>
<FormSettingExplanation>
{ this.props.translate(
'When choosing daily or weekly email delivery, which time of day would you prefer?'
) }
</FormSettingExplanation>
</FormFieldset>
<FormFieldset>
<FormLegend>{ this.props.translate( 'Jabber subscription delivery' ) }</FormLegend>
<CheckboxControl
checked={ this.props.getSetting( 'subscription_delivery_jabber_default' ) }
disabled={ this.props.getDisabledState() }
id="subscription_delivery_jabber_default"
name="subscription_delivery_jabber_default"
onChange={ this.handleCheckboxEvent( 'subscription_delivery_jabber_default' ) }
label={
<span>
{ this.props.translate( 'Receive subscription updates via instant message.' ) }{ ' ' }
<InlineSupportLink
supportContext="jabber-subscription-updates"
showIcon={ false }
/>
</span>
}
/>
</FormFieldset>
<FormFieldset>
<FormLegend>{ this.props.translate( 'Pause emails' ) }</FormLegend>
<CheckboxControl
checked={ this.props.getSetting( 'subscription_delivery_email_blocked' ) }
disabled={ this.props.getDisabledState() }
id="subscription_delivery_email_blocked"
name="subscription_delivery_email_blocked"
onChange={ this.handleCheckboxEvent( 'subscription_delivery_email_blocked' ) }
label={ this.props.translate(
'Pause all email updates from sites you’re subscribed to on WordPress.com'
) }
/>
<FormSettingExplanation>
{ this.props.translate(
'Newsletters are sent via WordPress.com. If you pause emails, you will not receive newsletters from the sites you are subscribed to.'
) }
</FormSettingExplanation>
</FormFieldset>
{ isAutomattician && (
<FormFieldset>
<FormLegend>Auto-follow P2 posts (Automatticians only)</FormLegend>
<CheckboxControl
checked={ ! this.props.getSetting( 'p2_disable_autofollow_on_comment' ) }
disabled={ this.props.getDisabledState() }
id="p2_disable_autofollow_on_comment"
name="p2_disable_autofollow_on_comment"
onChange={ this.handleCheckboxEvent( 'p2_disable_autofollow_on_comment', true ) }
label={ this.props.translate(
'Automatically subscribe to P2 post notifications when you leave a comment.'
) }
/>
</FormFieldset>
) }
<Button
accessibleWhenDisabled
variant="primary"
showTooltip={ ! this.props.hasUnsavedUserSettings }
label={ this.props.translate( 'No unsaved changes' ) }
disabled={ this.props.isUpdatingUserSettings || ! this.props.hasUnsavedUserSettings }
isBusy={ this.props.isUpdatingUserSettings }
onClick={ this.handleSubmitButtonClick }
>
{ this.props.translate( 'Save notification settings' ) }
</Button>
</form>
</Card>
<ConfirmDialog
isOpen={ this.state.showConfirmModal }
onConfirm={ () => this.handleModalConfirm() }
onCancel={ () => this.handleModalCancel() }
confirmButtonText={ this.props.translate( 'Confirm' ) }
style={ { maxWidth: '480px' } }
>
{ this.props.translate(
"You have active newsletter subscriptions. Pausing emails means you won't receive any newsletter updates. Are you sure you want to continue?"
) }
</ConfirmDialog>
</Main>
);
}
}
const mapStateToProps = ( state ) => ( {
teams: getReaderTeams( state ),
} );
const mapDispatchToProps = {
recordGoogleEvent,
};
const NotificationSubscriptionsWithHooks = ( props ) => {
const { hasNonSelfSubscriptions } = useSiteSubscriptions();
return <NotificationSubscriptions hasSubscriptions={ hasNonSelfSubscriptions } { ...props } />;
};
export default compose(
connect( mapStateToProps, mapDispatchToProps ),
localize,
protectForm,
withLocale,
withLocalizedMoment,
withFormBase
)( NotificationSubscriptionsWithHooks );
|