Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
86 changes: 3 additions & 83 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ExpenseRuleForm, MerchantRuleForm} from '@src/types/form';
import type {AppReview, BlockedFromConcierge, CustomStatusDraft, ExpenseRule, LoginList, Policy} from '@src/types/onyx';
import type {AppReview, BlockedFromConcierge, CustomStatusDraft, ExpenseRule, Policy} from '@src/types/onyx';
import type Login from '@src/types/onyx/Login';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import type {AnyOnyxServerUpdate, OnyxServerUpdate, OnyxUpdateEvent} from '@src/types/onyx/OnyxUpdatesFromServer';
Expand All @@ -66,7 +66,7 @@

let currentUserAccountID = -1;
let currentEmail = '';
Onyx.connect({

Check warning on line 69 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
Expand All @@ -75,7 +75,7 @@
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 78 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand Down Expand Up @@ -511,14 +511,7 @@
/**
* Validates a secondary login / contact method
*/
function validateSecondaryLogin(
currentUserPersonalDetails: OnyxEntry<OnyxPersonalDetails>,
loginList: OnyxEntry<LoginList>,
contactMethod: string,
validateCode: string,
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
shouldResetActionCode?: boolean,
) {
function validateSecondaryLogin(contactMethod: string, validateCode: string, formatPhoneNumber: LocaleContextProps['formatPhoneNumber'], shouldResetActionCode?: boolean) {
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -544,16 +537,7 @@
},
},
];
const successData: Array<
OnyxUpdate<
| typeof ONYXKEYS.LOGIN_LIST
| typeof ONYXKEYS.ACCOUNT
| typeof ONYXKEYS.SESSION
| typeof ONYXKEYS.PERSONAL_DETAILS_LIST
| typeof ONYXKEYS.COLLECTION.POLICY
| typeof ONYXKEYS.VALIDATE_ACTION_CODE
>
> = [
const successData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT | typeof ONYXKEYS.VALIDATE_ACTION_CODE>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.LOGIN_LIST,
Expand All @@ -578,70 +562,6 @@
},
},
];
// If the primary login isn't validated yet, set the secondary login as the primary login
if (!loginList?.[currentEmail].validatedDate) {
successData.push(
...[
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
primaryLogin: contactMethod,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {
email: contactMethod,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
login: contactMethod,
displayName: PersonalDetailsUtils.createDisplayName(contactMethod, currentUserPersonalDetails, formatPhoneNumber),
},
},
},
],
);

for (const policy of Object.values(allPolicies ?? {})) {
if (!policy) {
continue;
}

let optimisticPolicyDataValue;

if (policy.employeeList) {
const currentEmployee = policy.employeeList[currentEmail];
optimisticPolicyDataValue = {
employeeList: {
[currentEmail]: null,
[contactMethod]: currentEmployee,
},
};
}

if (policy.ownerAccountID === currentUserAccountID) {
optimisticPolicyDataValue = {
...optimisticPolicyDataValue,
owner: contactMethod,
};
}

if (optimisticPolicyDataValue) {
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
value: optimisticPolicyDataValue,
});
}
}
}

const failureData: Array<OnyxUpdate<typeof ONYXKEYS.LOGIN_LIST | typeof ONYXKEYS.ACCOUNT | typeof ONYXKEYS.VALIDATE_ACTION_CODE>> = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
{isValidateCodeFormVisible && !!loginData && !loginData.validatedDate && (
<ValidateCodeActionForm
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
handleSubmitForm={(validateCode) => validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber)}
handleSubmitForm={(validateCode) => validateSecondaryLogin(contactMethod, validateCode, formatPhoneNumber)}
validateError={!isEmptyObject(validateLoginError) ? validateLoginError : getLatestErrorField(loginData, 'validateCodeSent')}
clearError={() => {
// When removing unverified contact methods, the ValidateCodeActionForm unmounts and triggers clearError.
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/VerifyAccountPageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function VerifyAccountPageBase({navigateBackTo, navigateForwardTo, handleClose,

const handleSubmitForm = useCallback(
(validateCode: string) => {
validateSecondaryLogin(currentUserPersonalDetails, loginList, contactMethod, validateCode, formatPhoneNumber, true);
validateSecondaryLogin(contactMethod, validateCode, formatPhoneNumber, true);
},
[currentUserPersonalDetails, loginList, contactMethod, formatPhoneNumber],
[contactMethod, formatPhoneNumber],
);

const handleCloseWithFallback = useCallback(() => {
Expand Down
Loading