Skip to content

Commit

Permalink
move emailToShow to component state and control with hooks, fixes iss…
Browse files Browse the repository at this point in the history
…ue with input jumping to pending value if you type in old email value.
  • Loading branch information
Addison-Stavlo committed Sep 20, 2024
1 parent d763759 commit f907459
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions client/me/account/account-email-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ const AccountEmailField = ( {
const isEmailChangePending = useSelector( isPendingEmailChange );
const isEmailVerifified = useSelector( isCurrentUserEmailVerified );
const inputRef = useRef< FormTextInput >( null );

// TODO - likely move this to redux state so we can set it from the dialog modal.
const [ isLockedInput, setIsLockedInput ] = useState( true );

const [ emailSettingToShow, setEmailSettingToShow ] = useState( 'user_email' );
const [ emailInvalidReason, setEmailInvalidReason ] = useState< AccountEmailValidationReason >(
EMAIL_VALIDATION_REASON_IS_VALID
);
Expand All @@ -193,12 +191,25 @@ const AccountEmailField = ( {
};
}, [ dispatch ] );

const emailSettingToShow =
isEmailChangePending &&
! unsavedUserSettings?.user_email &&
unsavedUserSettings?.user_email !== ''
? 'new_user_email'
: 'user_email';
// If the isEmailChangePending updates to true, show the new email address field. This may
// happen just after initial load, as the selector may return null at first. Or this may happen
// after the user saves the form with an updated email address.
useEffect( () => {
if ( isEmailChangePending ) {
setEmailSettingToShow( 'new_user_email' );
}
}, [ isEmailChangePending ] );

// Once the user starts editing, ensure we show the user_email field since that is the one being
// updated in unsavedUserSettings. We don't want this to reset when the unsaved change renders falsy, as we want to
// both support the case of the empty string as well as when the unsaved settings are reset for
// having the same value as the saved settigns (user types in current user_email and unsaved
// variation gets reset to null).
useEffect( () => {
if ( unsavedUserSettings.user_email ) {
setEmailSettingToShow( 'user_email' );
}
}, [ unsavedUserSettings.user_email, setEmailSettingToShow ] );

const emailAddress = getUserSetting( {
settingName: emailSettingToShow,
Expand Down

0 comments on commit f907459

Please sign in to comment.