Skip to content

Fix: In 2FA magic code page, after refresh briefly error message is displayed #55332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import * as User from '@userActions/User';
import {isMobileSafari} from '@libs/Browser';
import {getLatestErrorField, getLatestErrorMessage} from '@libs/ErrorUtils';
import {isValidValidateCode} from '@libs/ValidationUtils';
import {clearValidateCodeActionError} from '@userActions/User';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -101,6 +101,7 @@ function BaseValidateCodeForm({
const shouldDisableResendValidateCode = !!isOffline || account?.isLoading;
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const [timeRemaining, setTimeRemaining] = useState(CONST.REQUEST_CODE_DELAY as number);
const [canShowError, setCanShowError] = useState<boolean>(false);

const timerRef = useRef<NodeJS.Timeout>();

Expand Down Expand Up @@ -131,7 +132,7 @@ function BaseValidateCodeForm({
}

// Keyboard won't show if we focus the input with a delay, so we need to focus immediately.
if (!Browser.isMobileSafari()) {
if (!isMobileSafari()) {
focusTimeoutRef.current = setTimeout(() => {
inputValidateCodeRef.current?.focusLastSelected();
}, CONST.ANIMATED_TRANSITION);
Expand Down Expand Up @@ -185,7 +186,7 @@ function BaseValidateCodeForm({

if (!isEmptyObject(validateError)) {
clearError();
User.clearValidateCodeActionError('actionVerified');
clearValidateCodeActionError('actionVerified');
}
},
[validateError, clearError],
Expand All @@ -195,12 +196,13 @@ function BaseValidateCodeForm({
* Check that all the form fields are valid, then trigger the submit callback
*/
const validateAndSubmitForm = useCallback(() => {
setCanShowError(true);
if (!validateCode.trim()) {
setFormError({validateCode: 'validateCodeForm.error.pleaseFillMagicCode'});
return;
}

if (!ValidationUtils.isValidValidateCode(validateCode)) {
if (!isValidValidateCode(validateCode)) {
setFormError({validateCode: 'validateCodeForm.error.incorrectMagicCode'});
return;
}
Expand All @@ -218,8 +220,17 @@ function BaseValidateCodeForm({
name="validateCode"
value={validateCode}
onChangeText={onTextInput}
errorText={formError?.validateCode ? translate(formError?.validateCode) : ErrorUtils.getLatestErrorMessage(account ?? {})}
hasError={!isEmptyObject(validateError)}
errorText={
canShowError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please define the new variable for errorText within useMemo?

? (() => {
if (formError?.validateCode) {
return translate(formError?.validateCode);
}
return getLatestErrorMessage(account ?? {});
})()
: ''
}
hasError={canShowError ? !isEmptyObject(validateError) : false}
onFulfill={validateAndSubmitForm}
autoFocus={false}
/>
Expand All @@ -231,9 +242,9 @@ function BaseValidateCodeForm({
)}
<OfflineWithFeedback
pendingAction={validateCodeAction?.pendingFields?.validateCodeSent}
errors={ErrorUtils.getLatestErrorField(validateCodeAction, 'actionVerified')}
errors={getLatestErrorField(validateCodeAction, 'actionVerified')}
errorRowStyles={[styles.mt2]}
onClose={() => User.clearValidateCodeActionError('actionVerified')}
onClose={() => clearValidateCodeActionError('actionVerified')}
>
{!shouldShowTimer && (
<View style={[styles.mt5, styles.dFlex, styles.flexColumn, styles.alignItemsStart]}>
Expand Down Expand Up @@ -263,7 +274,7 @@ function BaseValidateCodeForm({
<OfflineWithFeedback
shouldDisplayErrorAbove
pendingAction={validatePendingAction}
errors={validateError}
errors={canShowError ? validateError : undefined}
errorRowStyles={[styles.mt2]}
onClose={() => clearError()}
style={buttonStyles}
Expand Down
Loading