Skip to content

Commit 7cf44c6

Browse files
committed
refactor(mfa): derive validate code submitting state from machine
1 parent cfc1273 commit 7cf44c6

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/components/MultifactorAuthentication/machine/snapshotToState.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type MfaState = MfaContext & {
1616
/** Whether the machine currently accepts a request for a fresh magic-code email. */
1717
canResendValidateCode: boolean;
1818

19+
/** Whether the submitted magic code is currently being validated. */
20+
isValidateCodeFormSubmitting: boolean;
21+
1922
/** Whether the magic-code screen currently shows the inline invalid-code error. */
2023
showsInvalidCodeError: boolean;
2124
};
@@ -41,6 +44,11 @@ function snapshotToState(snapshot: MfaSnapshot): MfaState {
4144
...snapshot.context,
4245
modalState: getModalState(snapshot),
4346
canResendValidateCode: snapshot.can({type: 'RESEND_VALIDATE_CODE'}),
47+
isValidateCodeFormSubmitting: snapshot.matches({
48+
[MFA_STATE.OPEN]: {
49+
[MFA_STATE.MAGIC_CODE]: MFA_STATE.REQUESTING_REGISTRATION_CHALLENGE,
50+
},
51+
}),
4452
showsInvalidCodeError: snapshot.matches({
4553
[MFA_STATE.OPEN]: {
4654
[MFA_STATE.MAGIC_CODE]: {

src/pages/MultifactorAuthentication/ValidateCodePage.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function MultifactorAuthenticationValidateCodePage() {
5252
const [formError, setFormError] = useState<FormError>({});
5353
const [canShowError, setCanShowError] = useState<boolean>(false);
5454
const {requestCancel, submitValidateCode, resendValidateCode, notifyValidateCodeChanged, state} = useMultifactorAuthenticationInternal();
55-
const {showsInvalidCodeError, isCancelConfirmVisible, canResendValidateCode} = state;
55+
const {showsInvalidCodeError, isCancelConfirmVisible, canResendValidateCode, isValidateCodeFormSubmitting} = state;
5656

5757
// Refs
5858
const inputRef = useRef<MagicCodeInputHandle>(null);
@@ -61,8 +61,6 @@ function MultifactorAuthenticationValidateCodePage() {
6161

6262
// Derived state
6363
const hasAccountError = !!account && !isEmptyObject(account?.errors);
64-
// The MFA registration challenge always uses VALIDATE_CODE_FORM, even when the account has 2FA enabled.
65-
const isValidateCodeFormSubmitting = !!account?.isLoading && account.loadingForm === CONST.FORMS.VALIDATE_CODE_FORM;
6664
const shouldDisableResendCode = isOffline || !canResendValidateCode;
6765
const validateCodeActionError = getLatestErrorField(validateActionCode, 'actionVerified');
6866
const hasValidateCodeActionError = !isEmptyObject(validateCodeActionError);
@@ -154,7 +152,7 @@ function MultifactorAuthenticationValidateCodePage() {
154152
*/
155153
const validateAndSubmitForm = () => {
156154
// Check if already loading
157-
if (account?.isLoading) {
155+
if (isValidateCodeFormSubmitting) {
158156
return;
159157
}
160158

tests/unit/components/MultifactorAuthentication/machine/validateCodeTransition.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ describe('MFA magic code and registration decision', () => {
151151

152152
const result = actor.getSnapshot();
153153
expect(result.matches({[MFA_STATE.OPEN]: {[MFA_STATE.MAGIC_CODE]: MFA_STATE.REQUESTING_REGISTRATION_CHALLENGE}})).toBe(true);
154+
expect(snapshotToState(result).isValidateCodeFormSubmitting).toBe(true);
154155
expect(result.context.validateCode).toBe(MFA_TEST_VALIDATE_CODE);
155156
expect(result.context.registrationChallenge).toBeUndefined();
156157
expect(requestRegistrationChallengeMock).toHaveBeenCalledWith(MFA_TEST_VALIDATE_CODE);
@@ -168,6 +169,7 @@ describe('MFA magic code and registration decision', () => {
168169

169170
const result = actor.getSnapshot();
170171
expect(result.matches({[MFA_STATE.OPEN]: {[MFA_STATE.MAGIC_CODE]: MFA_STATE.REQUESTING_REGISTRATION_CHALLENGE}})).toBe(false);
172+
expect(snapshotToState(result).isValidateCodeFormSubmitting).toBe(false);
171173
expect(result.context.registrationChallenge).toBe(MFA_TEST_REGISTRATION_CHALLENGE);
172174
expect(result.context.error).toBeUndefined();
173175

0 commit comments

Comments
 (0)