File size: 32,203 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 | import config from '@automattic/calypso-config';
import { Button, Card, Dialog, FormInputValidation, FormLabel } from '@automattic/components';
import { canBeTranslated, getLanguage, isLocaleVariant } from '@automattic/i18n-utils';
import languages from '@automattic/languages';
import { ExternalLink } from '@wordpress/components';
import debugFactory from 'debug';
import { fixMe, localize } from 'i18n-calypso';
import { debounce, flowRight as compose, get, map, size } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import CSSTransition from 'react-transition-group/CSSTransition';
import TransitionGroup from 'react-transition-group/TransitionGroup';
import QueryUserSettings from 'calypso/components/data/query-user-settings';
import FormButton from 'calypso/components/forms/form-button';
import FormButtonsBar from 'calypso/components/forms/form-buttons-bar';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormRadio from 'calypso/components/forms/form-radio';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import FormTextInput from 'calypso/components/forms/form-text-input';
import InlineSupportLink from 'calypso/components/inline-support-link';
import LanguagePicker from 'calypso/components/language-picker';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import SectionHeader from 'calypso/components/section-header';
import SitesDropdown from 'calypso/components/sites-dropdown';
import { withGeoLocation } from 'calypso/data/geo/with-geolocation';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import { onboardingUrl } from 'calypso/lib/paths';
import { protectForm } from 'calypso/lib/protect-form';
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
import { clearStore } from 'calypso/lib/user/store';
import wpcom from 'calypso/lib/wp';
import AccountEmailField from 'calypso/me/account/account-email-field';
import { withDefaultInterface } from 'calypso/me/account/with-default-interface';
import { EmailVerificationBannerV2 } from 'calypso/me/email-verification-banner';
import ReauthRequired from 'calypso/me/reauth-required';
import { recordGoogleEvent, recordTracksEvent } from 'calypso/state/analytics/actions';
import {
isCurrentUserEmailVerified,
getCurrentUserDate,
getCurrentUserDisplayName,
getCurrentUserName,
getCurrentUserVisibleSiteCount,
} from 'calypso/state/current-user/selectors';
import { errorNotice, removeNotice, successNotice } from 'calypso/state/notices/actions';
import canDisplayCommunityTranslator from 'calypso/state/selectors/can-display-community-translator';
import getUnsavedUserSettings from 'calypso/state/selectors/get-unsaved-user-settings';
import getUserSettings from 'calypso/state/selectors/get-user-settings';
import isRequestingMissingSites from 'calypso/state/selectors/is-requesting-missing-sites';
import { isA8cTeamMember } from 'calypso/state/teams/selectors';
import {
clearUnsavedUserSettings,
removeUnsavedUserSetting,
setUserSetting,
} from 'calypso/state/user-settings/actions';
import { isFetchingUserSettings } from 'calypso/state/user-settings/selectors';
import { saveUnsavedUserSettings } from 'calypso/state/user-settings/thunks';
import AccountSettingsCloseLink from './close-link';
import ToggleLandingPageSettings from './toggle-landing-page';
import ToggleUseCommunityTranslator from './toggle-use-community-translator';
import './style.scss';
export const noticeId = 'me-settings-notice';
const noticeOptions = {
id: noticeId,
};
/**
* Debug instance
*/
const debug = debugFactory( 'calypso:me:account' );
const ALLOWED_USERNAME_CHARACTERS_REGEX = /^[a-z0-9]+$/;
const USERNAME_MIN_LENGTH = 4;
const ACCOUNT_FORM_NAME = 'account';
const INTERFACE_FORM_NAME = 'interface';
const ACCOUNT_FIELDS = [ 'user_login', 'user_email', 'user_URL', 'primary_site_ID' ];
const INTERFACE_FIELDS = [
'locale_variant',
'language',
'i18n_empathy_mode',
'use_fallback_for_incomplete_languages',
'enable_translator',
'calypso_preferences',
];
class Account extends Component {
constructor( props ) {
super( props );
this.props.removeUnsavedUserSetting( 'user_login' );
}
state = {
redirect: false,
showConfirmUsernameForm: false,
submittingForm: false,
formsSubmitting: {},
usernameAction: 'new',
validationResult: false,
accountSubmitDisable: false,
};
componentDidUpdate() {
if ( ! this.hasUnsavedUserSettings( ACCOUNT_FIELDS.concat( INTERFACE_FIELDS ) ) ) {
this.props.markSaved();
}
}
componentDidMount() {
const params = new URLSearchParams( window.location.search );
if ( params.get( 'usernameChangeSuccess' ) === 'true' ) {
this.props.successNotice( this.props.translate( 'Username changed successfully!' ), {
duration: 5000,
} );
const currentUrl = new URL( window.location.href );
currentUrl.searchParams.delete( 'usernameChangeSuccess' );
window.history.replaceState( {}, '', currentUrl.toString() );
}
}
componentWillUnmount() {
debug( this.constructor.displayName + ' component is unmounting.' );
// Silently clean up unsavedSettings before unmounting
this.props.clearUnsavedUserSettings();
}
getDisabledState( formName ) {
return formName ? this.state.formsSubmitting[ formName ] : this.state.submittingForm;
}
getUserSetting( settingName ) {
return (
get( this.props.unsavedUserSettings, settingName ) ??
this.getUserOriginalSetting( settingName )
);
}
getUserOriginalSetting( settingName ) {
return get( this.props.userSettings, settingName );
}
hasUnsavedUserSetting( settingName ) {
return this.props.unsavedUserSettings.hasOwnProperty( settingName );
}
hasUnsavedUserSettings( settingNames ) {
return settingNames.reduce(
( acc, settingName ) => this.hasUnsavedUserSetting( settingName ) || acc,
false
);
}
updateUserSetting( settingName, value ) {
this.props.setUserSetting( settingName, value );
}
updateUserSettingInput = ( event ) => {
this.updateUserSetting( event.target.name, event.target.value );
};
updateUserSettingCheckbox = ( event ) => {
this.updateUserSetting( event.target.name, event.target.checked );
};
updateCommunityTranslatorSetting = ( event ) => {
const { name, checked } = event.target;
this.updateUserSetting( name, checked );
const redirect = '/me/account';
this.setState( { redirect } );
this.saveInterfaceSettings( event );
};
updateLanguage = ( event ) => {
const { value, empathyMode, useFallbackForIncompleteLanguages } = event.target;
this.updateUserSetting( 'language', value );
if ( typeof empathyMode !== 'undefined' ) {
this.updateUserSetting( 'i18n_empathy_mode', empathyMode );
}
if ( typeof useFallbackForIncompleteLanguages !== 'undefined' ) {
this.updateUserSetting(
'use_fallback_for_incomplete_languages',
useFallbackForIncompleteLanguages
);
}
const localeVariantSelected = isLocaleVariant( value ) ? value : '';
const originalSlug =
this.getUserSetting( 'locale_variant' ) || this.getUserSetting( 'language' ) || '';
const languageHasChanged = originalSlug !== value;
const formHasChanged = languageHasChanged;
if ( formHasChanged ) {
this.props.markChanged();
}
const redirect = formHasChanged ? '/me/account' : false;
// store any selected locale variant so we can test it against those with no GP translation sets
this.setState( { redirect, localeVariantSelected } );
if ( formHasChanged ) {
this.props.recordTracksEvent( 'calypso_user_language_switch', {
new_language: value,
previous_language:
this.getUserOriginalSetting( 'locale_variant' ) ||
this.getUserOriginalSetting( 'language' ),
country_code: this.props.geo?.country_short,
} );
this.saveInterfaceSettings( event );
}
};
updateUserLoginConfirm = ( event ) => {
this.setState( { userLoginConfirm: event.target.value } );
};
validateUsername = debounce( async () => {
const { currentUserName, translate } = this.props;
const username = this.getUserSetting( 'user_login' );
debug( 'Validating username ' + username );
if ( username === currentUserName ) {
this.setState( { validationResult: false } );
return;
}
if ( username.length < USERNAME_MIN_LENGTH ) {
this.setState( {
validationResult: {
error: 'invalid_input',
message: translate( 'Usernames must be at least 4 characters.' ),
},
} );
return;
}
if ( ! ALLOWED_USERNAME_CHARACTERS_REGEX.test( username ) ) {
this.setState( {
validationResult: {
error: 'invalid_input',
message: translate( 'Usernames can only contain lowercase letters (a-z) and numbers.' ),
},
} );
return;
}
try {
const { success, allowed_actions } = await wpcom.req.get(
`/me/username/validate/${ username }`
);
this.setState( {
validationResult: { success, allowed_actions, validatedUsername: username },
} );
} catch ( error ) {
this.setState( { validationResult: error } );
}
}, 600 );
hasEmailValidationError() {
return !! this.state.emailValidationError;
}
shouldDisplayCommunityTranslator() {
const locale = this.getUserSetting( 'language' );
// disable for locales
if ( ! locale || ! canBeTranslated( locale ) ) {
return false;
}
// disable for locale variants with no official GP translation sets
if (
this.state.localeVariantSelected &&
! canBeTranslated( this.state.localeVariantSelected )
) {
return false;
}
// if the user hasn't yet selected a language, and the locale variants has no official GP translation set
if (
typeof this.state.localeVariantSelected !== 'string' &&
! canBeTranslated( this.getUserSetting( 'locale_variant' ) )
) {
return false;
}
return true;
}
thankTranslationContributors() {
if ( ! this.shouldDisplayCommunityTranslator() ) {
return;
}
const locale = this.getUserSetting( 'language' );
const language = getLanguage( locale );
if ( ! language ) {
return;
}
const { translate } = this.props;
const url = 'https://translate.wordpress.com/translators/?contributor_locale=' + locale;
const thanksLabel = fixMe( {
text: 'Thanks to {{a}}all our community members who helped translate to {{language/}}{{/a}}',
newCopy: translate(
'Thanks to {{a}}all our community members who helped translate to {{language/}}{{/a}}',
{
components: {
a: <ExternalLink children={ null } href={ url } />,
language: <span>{ language.name }</span>,
},
}
),
oldCopy: translate(
'Thanks to {{a}}all our community members who helped translate to {{language/}}{{/a}}!',
{
components: {
a: <ExternalLink children={ null } href={ url } />,
language: <span>{ language.name }</span>,
},
}
),
} );
return <FormSettingExplanation> { thanksLabel }</FormSettingExplanation>;
}
handleRadioChange = ( event ) => {
const { name, value } = event.currentTarget;
this.setState( { [ name ]: value } );
};
/**
* We handle the username (user_login) change manually through an onChange handler
* so that we can also run a debounced validation on the username.
* @param {Object} event Event from onChange of user_login input
*/
handleUsernameChange = ( event ) => {
const value = event.currentTarget.value;
if ( value === this.getUserOriginalSetting( 'user_login' ) ) {
this.cancelUsernameChange();
return;
}
this.validateUsername();
this.updateUserSetting( 'user_login', value );
this.setState( { usernameAction: null } );
};
recordClickEvent = ( action ) => {
this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action );
};
getClickHandler( action, callback ) {
return () => {
this.recordClickEvent( action );
if ( callback ) {
callback();
}
};
}
getFocusHandler( action ) {
return () => this.props.recordGoogleEvent( 'Me', 'Focused on ' + action );
}
handleUsernameChangeBlogRadio = ( event ) => {
this.props.recordGoogleEvent(
'Me',
'Clicked Username Change Blog Action radio',
'checked',
event.target.value
);
};
cancelUsernameChange = () => {
this.setState( {
userLoginConfirm: null,
usernameAction: null,
} );
this.clearUsernameValidation();
this.props.removeUnsavedUserSetting( 'user_login' );
const { user_login, ...otherUnsavedSettings } = this.props.unsavedUserSettings;
if ( ! Object.keys( otherUnsavedSettings ).length ) {
this.props.markSaved();
}
};
toggleConfirmUsernameForm = () => {
this.setState( { showConfirmUsernameForm: ! this.state.showConfirmUsernameForm } );
};
submitUsernameForm = async () => {
const username = this.getUserSetting( 'user_login' );
const action = this.state.usernameAction ? this.state.usernameAction : 'none';
this.setState( { submittingForm: true, showConfirmUsernameForm: false } );
try {
await wpcom.req.post( '/me/username', { username, action } );
this.setState( { submittingForm: false } );
this.props.markSaved();
// We reload here to refresh cookies, user object, and user settings.
// @TODO: Do not require reload here.
const currentUrl = new URL( window.location.href );
currentUrl.searchParams.set( 'usernameChangeSuccess', 'true' );
window.location.href = currentUrl.toString();
} catch ( error ) {
this.setState( { submittingForm: false, validationResult: error } );
this.props.errorNotice( error.message );
}
};
isUsernameValid() {
return this.state.validationResult?.success === true;
}
getUsernameValidationFailureMessage() {
return this.state.validationResult?.message ?? null;
}
getAllowedActions() {
return this.state.validationResult?.allowed_actions ?? {};
}
getValidatedUsername() {
return this.state.validationResult?.validatedUsername ?? null;
}
clearUsernameValidation() {
this.setState( { validationResult: false } );
}
onSiteSelect = ( siteId ) => {
if ( siteId ) {
this.updateUserSetting( 'primary_site_ID', siteId );
}
};
renderJoinDate() {
const { currentUserDate, translate, moment } = this.props;
const dateMoment = moment( currentUserDate );
return (
<span>
{ translate( 'Joined %(month)s %(year)s', {
args: {
month: dateMoment.format( 'MMMM' ),
year: dateMoment.format( 'YYYY' ),
},
} ) }
</span>
);
}
renderUsernameValidation() {
const { translate } = this.props;
const isUsernameValid = this.isUsernameValid();
const usernameValidationFailureMessage = this.getUsernameValidationFailureMessage();
if ( ! this.hasUnsavedUserSetting( 'user_login' ) ) {
return null;
}
if ( ! isUsernameValid && null === usernameValidationFailureMessage ) {
return null;
}
return (
<FormInputValidation isError={ ! isUsernameValid }>
{ isUsernameValid ? translate( 'Nice username!' ) : usernameValidationFailureMessage }
</FormInputValidation>
);
}
renderUsernameDescription() {
const { translate, isAutomattician, isEmailVerified } = this.props;
if ( ! isEmailVerified ) {
return (
<span>{ translate( 'Username can be changed once your email address is verified.' ) }</span>
);
}
if ( isAutomattician ) {
return <span>{ translate( 'Automatticians cannot change their username.' ) }</span>;
}
return this.renderJoinDate();
}
renderPrimarySite() {
const { requestingMissingSites, translate, visibleSiteCount } = this.props;
if ( ! visibleSiteCount ) {
return (
<Button
href={ onboardingUrl() + '?ref=me-account-settings' }
onClick={ this.getClickHandler( 'Primary Site Add New WordPress Button' ) }
>
{ translate( 'Add New Site' ) }
</Button>
);
}
const primarySiteId = this.getUserSetting( 'primary_site_ID' );
return (
<SitesDropdown
key={ primarySiteId }
isPlaceholder={ ! primarySiteId || requestingMissingSites }
selectedSiteId={ primarySiteId }
onSiteSelect={ this.onSiteSelect }
disabled={ this.state.submittingForm }
/>
);
}
shouldDisableAccountSubmitButton() {
return (
this.state.accountSubmitDisable ||
! this.hasUnsavedUserSettings( ACCOUNT_FIELDS ) ||
this.getDisabledState( ACCOUNT_FORM_NAME ) ||
this.hasEmailValidationError()
);
}
shouldDisableInterfaceSubmitButton() {
return (
! this.hasUnsavedUserSettings( INTERFACE_FIELDS ) ||
this.getDisabledState( INTERFACE_FORM_NAME )
);
}
handleSubmitError( error, formName = '' ) {
debug( 'Error saving settings: ' + JSON.stringify( error ) );
if ( error.message ) {
this.props.errorNotice( error.message, noticeOptions );
} else {
this.props.errorNotice(
this.props.translate( 'There was a problem saving your changes.' ),
noticeOptions
);
}
this.setState( {
submittingForm: false,
formsSubmitting: {
...this.state.formsSubmitting,
...( formName && { [ formName ]: false } ),
},
} );
}
isSubmittingForm( formName ) {
return formName ? this.state.formsSubmitting[ formName ] : this.state.submittingForm;
}
async handleSubmitSuccess( response, formName = '' ) {
if ( ! this.hasUnsavedUserSettings( ACCOUNT_FIELDS.concat( INTERFACE_FIELDS ) ) ) {
this.props.markSaved();
}
if ( this.state.redirect ) {
await clearStore();
// Sometimes changes in settings require a url refresh to update the UI.
// For example when the user changes the language.
window.location = this.state.redirect + '?updated=success';
return;
}
// Determine success message based on what has been updated.
const newEmail = response.new_user_email;
const moreThanEmailChanged = Object.keys( response )?.find(
( item ) => ! [ 'new_user_email', 'user_email', 'user_email_change_pending' ].includes( item )
);
// Default case.
let successMessage = this.props.translate( 'Settings saved successfully!' );
if ( newEmail && moreThanEmailChanged ) {
// Email and other settings changed.
successMessage = this.props.translate(
'Settings saved successfully!{{br/}}We sent an email to %(email)s. Please check your inbox to verify your email.',
{
args: {
email: newEmail || '',
},
components: {
br: <br />,
},
}
);
} else if ( newEmail ) {
// Only email changed.
successMessage = this.props.translate(
'We sent an email to %(email)s. Please check your inbox to verify your email.',
{
args: {
email: newEmail || '',
},
}
);
}
this.setState(
{
submittingForm: false,
formsSubmitting: {
...this.state.formsSubmitting,
...( formName && { [ formName ]: false } ),
},
},
() => {
this.props.successNotice( successMessage, noticeOptions );
}
);
debug( 'Settings saved successfully ' + JSON.stringify( response ) );
}
async submitForm( event, fields, formName = '' ) {
event?.preventDefault && event.preventDefault();
debug( 'Submitting form' );
this.setState( {
submittingForm: true,
formsSubmitting: {
...this.state.formsSubmitting,
...( formName && { [ formName ]: true } ),
},
} );
try {
const response = await this.props.saveUnsavedUserSettings( fields );
this.handleSubmitSuccess( response, formName );
} catch ( error ) {
this.handleSubmitError( error, formName );
}
}
/*
* These form fields are displayed when there is not a username change in progress.
*/
renderAccountFields() {
const { translate } = this.props;
return (
<div className="account__settings-form" key="settingsForm">
<AccountEmailField
emailInputId="user_email"
emailInputName="user_email"
emailValidationHandler={ ( isEmailValid ) =>
this.setState( { emailValidationError: ! isEmailValid } )
}
isEmailControlDisabled={ this.getDisabledState( ACCOUNT_FORM_NAME ) }
onFocus={ this.getFocusHandler( 'Email Address Field' ) }
unsavedUserSettings={ this.props.unsavedUserSettings }
userSettings={ this.props.userSettings }
/>
<FormFieldset>
<FormLabel htmlFor="primary_site_ID">{ translate( 'Primary site' ) }</FormLabel>
{ this.renderPrimarySite() }
<FormSettingExplanation>
{ translate( "Choose the default site dashboard you'll see at login." ) }
</FormSettingExplanation>
</FormFieldset>
<FormButton
isSubmitting={ this.isSubmittingForm( ACCOUNT_FORM_NAME ) }
disabled={ this.shouldDisableAccountSubmitButton() }
onClick={ this.handleSubmitButtonClick }
>
{ this.isSubmittingForm( ACCOUNT_FORM_NAME )
? translate( 'Saving…' )
: translate( 'Save account settings' ) }
</FormButton>
</div>
);
}
renderBlogActionFields() {
const { translate } = this.props;
const actions = this.getAllowedActions();
/*
* If there are no actions or if there is only one action,
* which we assume is the 'none' action, we ignore the actions.
*/
if ( size( actions ) <= 1 ) {
return;
}
return (
<FormFieldset>
<FormLabel>{ translate( 'Would you like a matching blog address too?' ) }</FormLabel>
{
// message is translated in the API
map( actions, ( message, key ) => (
<FormLabel key={ key }>
<FormRadio
name="usernameAction"
onChange={ this.handleRadioChange }
onClick={ this.handleUsernameChangeBlogRadio }
value={ key }
checked={ key === this.state.usernameAction }
label={ message }
/>
</FormLabel>
) )
}
</FormFieldset>
);
}
renderConfirmUsernameDialog() {
const { currentUserDisplayName, currentUserName, translate } = this.props;
const buttons = [
{ action: 'cancel', label: translate( 'Cancel' ), onClick: this.toggleConfirmUsernameForm },
{
action: 'confirm',
label: translate( 'Change username' ),
isPrimary: true,
additionalClassNames: 'is-scary',
onClick: this.submitUsernameForm,
},
];
return (
<Dialog
isVisible={ this.state.showConfirmUsernameForm }
additionalClassNames="account__confirm-username-dialog"
buttons={ buttons }
showCloseIcon
onClose={ this.toggleConfirmUsernameForm }
>
<FormLabel>{ translate( 'Confirm username change' ) }</FormLabel>
<p>
{ translate(
'You are about to change your username, {{strong}}%(username)s{{/strong}}. ' +
'Once changed, you will not be able to revert it.',
{
args: {
username: currentUserName,
},
components: {
strong: <strong />,
},
}
) }{ ' ' }
{ translate(
'Changing your username will also affect your Gravatar profile and IntenseDebate profile addresses.'
) }
</p>
<p>
{ translate(
'If you just want to change your display name, {{strong}}%(displayName)s{{/strong}}, ' +
'you can do so under {{myProfileLink}}My Profile{{/myProfileLink}}.',
{
args: {
displayName: currentUserDisplayName,
},
components: {
myProfileLink: (
<a
href="/me"
onClick={ this.getClickHandler(
'My Profile Link in Username Change',
this.props.markSaved
) }
/>
),
strong: <strong />,
},
}
) }
</p>
</Dialog>
);
}
/*
* These form fields are displayed when a username change is in progress.
*/
renderUsernameFields() {
const { translate } = this.props;
const isSaveButtonDisabled =
this.getUserSetting( 'user_login' ) !== this.state.userLoginConfirm ||
! this.isUsernameValid() ||
this.state.submittingForm;
const usernameMatch =
this.getUserSetting( 'user_login' ) === this.state.userLoginConfirm &&
this.state.userLoginConfirm.length > 0;
const usernameValidationFailureMessage = this.getUsernameValidationFailureMessage();
const isError = ! usernameMatch || usernameValidationFailureMessage;
let validationMessage = translate( 'Please re-enter your new username to confirm it.' );
if ( usernameMatch ) {
validationMessage = usernameValidationFailureMessage
? usernameValidationFailureMessage
: translate( 'Thanks for confirming your new username!' );
}
return (
<div className="account__username-form" key="usernameForm">
<FormFieldset>
<FormLabel htmlFor="username_confirm">{ translate( 'Confirm new username' ) }</FormLabel>
<FormTextInput
autoCapitalize="off"
autoComplete="off"
autoCorrect="off"
id="username_confirm"
name="username_confirm"
onFocus={ this.getFocusHandler( 'Username Confirm Field' ) }
value={ this.state.userLoginConfirm ?? '' }
onChange={ this.updateUserLoginConfirm }
isValid={ usernameMatch }
isError={ isError }
/>
<FormInputValidation isError={ isError }>{ validationMessage }</FormInputValidation>
</FormFieldset>
{ this.renderBlogActionFields() }
{ this.renderConfirmUsernameDialog() }
<FormButtonsBar>
<FormButton
disabled={ isSaveButtonDisabled }
type="button"
onClick={ this.getClickHandler(
'Change Username Button',
this.toggleConfirmUsernameForm
) }
>
{ translate( 'Change username' ) }
</FormButton>
<FormButton
isPrimary={ false }
type="button"
onClick={ this.getClickHandler(
'Cancel Username Change Button',
this.cancelUsernameChange
) }
>
{ translate( 'Cancel' ) }
</FormButton>
</FormButtonsBar>
</div>
);
}
saveAccountSettings = ( event ) => {
this.submitForm( event, ACCOUNT_FIELDS, ACCOUNT_FORM_NAME );
};
saveInterfaceSettings = ( event ) => {
this.submitForm( event, INTERFACE_FIELDS, INTERFACE_FORM_NAME );
};
render() {
const { isFetching, markChanged, translate } = this.props;
// Is a username change in progress?
const renderUsernameForm = this.hasUnsavedUserSetting( 'user_login' );
return (
<Main wideLayout className="account">
<QueryUserSettings />
<PageViewTracker path="/me/account" title="Me > Account Settings" />
<ReauthRequired twoStepAuthorization={ twoStepAuthorization } />
<NavigationHeader
navigationItems={ [] }
title={ translate( 'Account settings' ) }
subtitle={ translate(
'Adjust your account information and interface settings. {{learnMoreLink}}Learn more{{/learnMoreLink}}.',
{
components: {
learnMoreLink: (
<InlineSupportLink supportContext="account-settings" showIcon={ false } />
),
},
}
) }
/>
<EmailVerificationBannerV2
setIsBusy={ ( isBusy ) => {
this.state.accountSubmitDisable = isBusy;
} }
/>
<SectionHeader label={ translate( 'Account Information' ) } />
<Card className="account__settings">
<form onChange={ markChanged } onSubmit={ this.saveAccountSettings }>
<FormFieldset>
<FormLabel htmlFor="user_login">{ translate( 'Username' ) }</FormLabel>
<FormTextInput
autoCapitalize="off"
autoComplete="off"
autoCorrect="off"
className="account__username"
disabled={
this.getDisabledState( ACCOUNT_FORM_NAME ) ||
! this.getUserSetting( 'user_login_can_be_changed' )
}
id="user_login"
name="user_login"
onFocus={ this.getFocusHandler( 'Username Field' ) }
onChange={ this.handleUsernameChange }
value={ this.getUserSetting( 'user_login' ) || '' }
isValid={ renderUsernameForm && this.isUsernameValid() }
isError={
renderUsernameForm && null !== this.getUsernameValidationFailureMessage()
}
/>
{ renderUsernameForm ? (
this.renderUsernameValidation()
) : (
<FormSettingExplanation>
{ this.renderUsernameDescription() }
</FormSettingExplanation>
) }
</FormFieldset>
{ /* This is how we animate showing/hiding the form field sections */ }
<TransitionGroup>
<CSSTransition
key={ renderUsernameForm ? 'username' : 'account' }
classNames="account__username-form-toggle"
timeout={ { enter: 500, exit: 10 } }
>
{ renderUsernameForm ? this.renderUsernameFields() : this.renderAccountFields() }
</CSSTransition>
</TransitionGroup>
</form>
</Card>
<SectionHeader label={ translate( 'Interface settings' ) } />
<Card className="account__settings">
<form onSubmit={ this.saveInterfaceSettings }>
<FormFieldset>
<FormLabel id="account__language" htmlFor="language">
{ translate( 'Interface language' ) }
</FormLabel>
<LanguagePicker
disabled={ this.getDisabledState( INTERFACE_FORM_NAME ) }
isLoading={ isFetching }
languages={ languages }
onClick={ this.getClickHandler( 'Interface Language Field' ) }
valueKey="langSlug"
value={
this.getUserSetting( 'locale_variant' ) || this.getUserSetting( 'language' ) || ''
}
empathyMode={ this.getUserSetting( 'i18n_empathy_mode' ) }
useFallbackForIncompleteLanguages={ this.getUserSetting(
'use_fallback_for_incomplete_languages'
) }
onChange={ this.updateLanguage }
/>
<FormSettingExplanation>
{ translate(
'This is the language of the interface you see across WordPress.com as a whole.'
) }
</FormSettingExplanation>
{ this.thankTranslationContributors() }
</FormFieldset>
{ this.props.canDisplayCommunityTranslator && (
<FormFieldset className="account__settings-admin-home">
<FormLabel id="account__default_landing_page">
{ translate( 'Community translator' ) }
</FormLabel>
<ToggleUseCommunityTranslator />
</FormFieldset>
) }
<FormFieldset className="account__settings-admin-home">
<FormLabel id="account__default_landing_page">
{ translate( 'Default landing page' ) }
</FormLabel>
<ToggleLandingPageSettings defaultInterface={ this.props.defaultInterface } />
<FormSettingExplanation>
{ fixMe( {
text: "Select what you'll see by default when visiting WordPress.com",
newCopy: translate(
"Select what you'll see by default when visiting WordPress.com"
),
oldCopy: translate(
'When you type https://www.wordpress.com in your browser, this is the page you land on.'
),
} ) }
</FormSettingExplanation>
</FormFieldset>
<FormFieldset>
<FormLabel id="account__color_scheme" htmlFor="color_scheme">
{ translate( 'Dashboard color scheme' ) }
</FormLabel>
<FormSettingExplanation>
{ translate(
'You can now set the color scheme on your individual site by visiting Users → Profile from your site dashboard.'
) }
</FormSettingExplanation>
</FormFieldset>
</form>
</Card>
{ config.isEnabled( 'me/account-close' ) && <AccountSettingsCloseLink /> }
</Main>
);
}
}
export default compose(
localize,
withLocalizedMoment,
withGeoLocation,
protectForm,
withDefaultInterface,
connect(
( state ) => ( {
canDisplayCommunityTranslator: canDisplayCommunityTranslator( state ),
currentUserDate: getCurrentUserDate( state ),
currentUserDisplayName: getCurrentUserDisplayName( state ),
currentUserName: getCurrentUserName( state ),
isFetching: isFetchingUserSettings( state ),
requestingMissingSites: isRequestingMissingSites( state ),
userSettings: getUserSettings( state ),
unsavedUserSettings: getUnsavedUserSettings( state ),
visibleSiteCount: getCurrentUserVisibleSiteCount( state ),
isEmailVerified: isCurrentUserEmailVerified( state ),
isAutomattician: isA8cTeamMember( state ),
} ),
{
clearUnsavedUserSettings,
errorNotice,
removeNotice,
recordGoogleEvent,
recordTracksEvent,
removeUnsavedUserSetting,
saveUnsavedUserSettings,
setUserSetting,
successNotice,
}
)
)( Account );
|