Skip to content

Commit 8d27026

Browse files
authored
Merge pull request Expensify#81990 from shubham1206agra/refactor-validateSecondaryLogin
[NoQA] Refactor: isolate validateSecondaryLogin from Onyx.connect ONYXKEYS.COLLECTION.POLICY
2 parents e32727c + d7003cb commit 8d27026

3 files changed

Lines changed: 6 additions & 86 deletions

File tree

src/libs/actions/User.ts

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import CONST from '@src/CONST';
4949
import ONYXKEYS from '@src/ONYXKEYS';
5050
import ROUTES from '@src/ROUTES';
5151
import type {ExpenseRuleForm, MerchantRuleForm} from '@src/types/form';
52-
import type {AppReview, BlockedFromConcierge, CustomStatusDraft, ExpenseRule, LoginList, Policy} from '@src/types/onyx';
52+
import type {AppReview, BlockedFromConcierge, CustomStatusDraft, ExpenseRule, Policy} from '@src/types/onyx';
5353
import type Login from '@src/types/onyx/Login';
5454
import type {Errors} from '@src/types/onyx/OnyxCommon';
5555
import type {AnyOnyxServerUpdate, OnyxServerUpdate, OnyxUpdateEvent} from '@src/types/onyx/OnyxUpdatesFromServer';
@@ -511,14 +511,7 @@ function requestValidateCodeAction() {
511511
/**
512512
* Validates a secondary login / contact method
513513
*/
514-
function validateSecondaryLogin(
515-
currentUserPersonalDetails: OnyxEntry<OnyxPersonalDetails>,
516-
loginList: OnyxEntry<LoginList>,
517-
contactMethod: string,
518-
validateCode: string,
519-
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
520-
shouldResetActionCode?: boolean,
521-
) {
514+
function validateSecondaryLogin(contactMethod: string, validateCode: string, formatPhoneNumber: LocaleContextProps['formatPhoneNumber'], shouldResetActionCode?: boolean) {
522515
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT>> = [
523516
{
524517
onyxMethod: Onyx.METHOD.MERGE,
@@ -544,16 +537,7 @@ function validateSecondaryLogin(
544537
},
545538
},
546539
];
547-
const successData: Array<
548-
OnyxUpdate<
549-
| typeof ONYXKEYS.LOGIN_LIST
550-
| typeof ONYXKEYS.ACCOUNT
551-
| typeof ONYXKEYS.SESSION
552-
| typeof ONYXKEYS.PERSONAL_DETAILS_LIST
553-
| typeof ONYXKEYS.COLLECTION.POLICY
554-
| typeof ONYXKEYS.VALIDATE_ACTION_CODE
555-
>
556-
> = [
540+
const successData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT | typeof ONYXKEYS.VALIDATE_ACTION_CODE>> = [
557541
{
558542
onyxMethod: Onyx.METHOD.MERGE,
559543
key: ONYXKEYS.LOGIN_LIST,
@@ -578,70 +562,6 @@ function validateSecondaryLogin(
578562
},
579563
},
580564
];
581-
// If the primary login isn't validated yet, set the secondary login as the primary login
582-
if (!loginList?.[currentEmail].validatedDate) {
583-
successData.push(
584-
...[
585-
{
586-
onyxMethod: Onyx.METHOD.MERGE,
587-
key: ONYXKEYS.ACCOUNT,
588-
value: {
589-
primaryLogin: contactMethod,
590-
},
591-
},
592-
{
593-
onyxMethod: Onyx.METHOD.MERGE,
594-
key: ONYXKEYS.SESSION,
595-
value: {
596-
email: contactMethod,
597-
},
598-
},
599-
{
600-
onyxMethod: Onyx.METHOD.MERGE,
601-
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
602-
value: {
603-
[currentUserAccountID]: {
604-
login: contactMethod,
605-
displayName: PersonalDetailsUtils.createDisplayName(contactMethod, currentUserPersonalDetails, formatPhoneNumber),
606-
},
607-
},
608-
},
609-
],
610-
);
611-
612-
for (const policy of Object.values(allPolicies ?? {})) {
613-
if (!policy) {
614-
continue;
615-
}
616-
617-
let optimisticPolicyDataValue;
618-
619-
if (policy.employeeList) {
620-
const currentEmployee = policy.employeeList[currentEmail];
621-
optimisticPolicyDataValue = {
622-
employeeList: {
623-
[currentEmail]: null,
624-
[contactMethod]: currentEmployee,
625-
},
626-
};
627-
}
628-
629-
if (policy.ownerAccountID === currentUserAccountID) {
630-
optimisticPolicyDataValue = {
631-
...optimisticPolicyDataValue,
632-
owner: contactMethod,
633-
};
634-
}
635-
636-
if (optimisticPolicyDataValue) {
637-
successData.push({
638-
onyxMethod: Onyx.METHOD.MERGE,
639-
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
640-
value: optimisticPolicyDataValue,
641-
});
642-
}
643-
}
644-
}
645565

646566
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT | typeof ONYXKEYS.VALIDATE_ACTION_CODE>> = [
647567
{

src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
349349
{isValidateCodeFormVisible && !!loginData && !loginData.validatedDate && (
350350
<ValidateCodeActionForm
351351
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
352-
handleSubmitForm={(validateCode) => validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber)}
352+
handleSubmitForm={(validateCode) => validateSecondaryLogin(contactMethod, validateCode, formatPhoneNumber)}
353353
validateError={!isEmptyObject(validateLoginError) ? validateLoginError : getLatestErrorField(loginData, 'validateCodeSent')}
354354
clearError={() => {
355355
// When removing unverified contact methods, the ValidateCodeActionForm unmounts and triggers clearError.

src/pages/settings/VerifyAccountPageBase.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ function VerifyAccountPageBase({navigateBackTo, navigateForwardTo, handleClose,
4242

4343
const handleSubmitForm = useCallback(
4444
(validateCode: string) => {
45-
validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber, true);
45+
validateSecondaryLogin(contactMethod, validateCode, formatPhoneNumber, true);
4646
},
47-
[currentUserPersonalDetails, loginList, contactMethod, formatPhoneNumber],
47+
[contactMethod, formatPhoneNumber],
4848
);
4949

5050
const handleCloseWithFallback = useCallback(() => {

0 commit comments

Comments
 (0)