From 96024c55e6d8eee93e1d1290784294e99c5fe74e Mon Sep 17 00:00:00 2001 From: Hans Date: Thu, 22 Jun 2023 06:46:40 +0700 Subject: [PATCH] fix: infinite loading with invalid account route --- src/libs/ValidationUtils.js | 11 +++++++++++ src/pages/ProfilePage.js | 6 ++++-- tests/unit/ValidationUtilsTest.js | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/libs/ValidationUtils.js b/src/libs/ValidationUtils.js index 4778669840a8..0fdc65bc7a82 100644 --- a/src/libs/ValidationUtils.js +++ b/src/libs/ValidationUtils.js @@ -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, @@ -481,4 +491,5 @@ export { isValidLegalName, doesContainReservedWord, isNumeric, + isValidAccountRoute, }; diff --git a/src/pages/ProfilePage.js b/src/pages/ProfilePage.js index 2b69c541c44d..6b59315bbc3c 100755 --- a/src/pages/ProfilePage.js +++ b/src/pages/ProfilePage.js @@ -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({ @@ -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', ''); diff --git a/tests/unit/ValidationUtilsTest.js b/tests/unit/ValidationUtilsTest.js index 48ad9f20b028..31735c15243c 100644 --- a/tests/unit/ValidationUtilsTest.js +++ b/tests/unit/ValidationUtilsTest.js @@ -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); + }); + }); });