Skip to content

Commit

Permalink
Merge pull request Expensify#21286 from hungvu193/fix-20854
Browse files Browse the repository at this point in the history
fix: infinite loading with invalid account route
  • Loading branch information
arosiclair authored Jun 22, 2023
2 parents 10270ba + 96024c5 commit e58bb76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ function isNumeric(value) {
return /^\d*$/.test(value);
}

/**
* Checks that the provided accountID is a number and bigger than 0.
*
* @param {Number} accountID
* @returns {Boolean}
*/
function isValidAccountRoute(accountID) {
return CONST.REGEX.NUMBER.test(accountID) && accountID > 0;
}

export {
meetsMinimumAgeRequirement,
meetsMaximumAgeRequirement,
Expand Down Expand Up @@ -481,4 +491,5 @@ export {
isValidLegalName,
doesContainReservedWord,
isNumeric,
isValidAccountRoute,
};
6 changes: 4 additions & 2 deletions src/pages/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import BlockingView from '../components/BlockingViews/BlockingView';
import * as Illustrations from '../components/Icon/Illustrations';
import variables from '../styles/variables';
import ROUTES from '../ROUTES';
import * as ValidationUtils from '../libs/ValidationUtils';

const matchType = PropTypes.shape({
params: PropTypes.shape({
Expand Down Expand Up @@ -95,12 +96,13 @@ function ProfilePage(props) {

// eslint-disable-next-line rulesdir/prefer-early-return
useEffect(() => {
if (accountID > 0) {
if (ValidationUtils.isValidAccountRoute(accountID)) {
PersonalDetails.openPublicProfilePage(accountID);
}
}, [accountID]);

const details = lodashGet(props.personalDetails, accountID, {});
const details = lodashGet(props.personalDetails, accountID, {isLoading: ValidationUtils.isValidAccountRoute(accountID)});

const displayName = details.displayName ? details.displayName : props.translate('common.hidden');
const avatar = lodashGet(details, 'avatar', UserUtils.getDefaultAvatar());
const originalFileName = lodashGet(details, 'originalFileName', '');
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/ValidationUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,17 @@ describe('ValidationUtils', () => {
expect(ValidationUtils.isValidWebsite('www.expensify😄.com')).toBe(false);
});
});

describe('ValidateAccountRoute', () => {
test('Valid account route', () => {
expect(ValidationUtils.isValidAccountRoute(123123)).toBe(true);
expect(ValidationUtils.isValidAccountRoute(5612)).toBe(true);
});

test('Invalid account route', () => {
expect(ValidationUtils.isValidAccountRoute(undefined)).toBe(false);
expect(ValidationUtils.isValidAccountRoute(0)).toBe(false);
expect(ValidationUtils.isValidAccountRoute('123aaa')).toBe(false);
});
});
});

0 comments on commit e58bb76

Please sign in to comment.