Skip to content

Commit

Permalink
Merge pull request #30608 from paultsimura/fix/26282-contact-methods-…
Browse files Browse the repository at this point in the history
…go-back
  • Loading branch information
dangrous authored Nov 7, 2023
2 parents 5df95a1 + 5b17374 commit da2c79e
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import {ValueOf} from 'type-fest';
import CONST from './CONST';

/**
* This is a file containing constants for all of the routes we want to be able to go to
* This is a file containing constants for all the routes we want to be able to go to
*/

/**
* This is a file containing constants for all of the routes we want to be able to go to
* Returns the URL with an encoded URI component for the backTo param which can be added to the end of URLs
* @param backTo
* @returns
* Builds a URL with an encoded URI component for the `backTo` param which can be added to the end of URLs
*/
function getUrlWithBackToParam(url: string, backTo?: string): string {
const backToParam = backTo ? `${url.includes('?') ? '&' : '?'}backTo=${encodeURIComponent(backTo)}` : '';
Expand Down Expand Up @@ -111,7 +108,10 @@ export default {
route: 'settings/profile/personal-details/address/country',
getRoute: (country: string, backTo?: string) => getUrlWithBackToParam(`settings/profile/personal-details/address/country?country=${country}`, backTo),
},
SETTINGS_CONTACT_METHODS: 'settings/profile/contact-methods',
SETTINGS_CONTACT_METHODS: {
route: 'settings/profile/contact-methods',
getRoute: (backTo?: string) => getUrlWithBackToParam('settings/profile/contact-methods', backTo),
},
SETTINGS_CONTACT_METHOD_DETAILS: {
route: 'settings/profile/contact-methods/:contactMethod/details',
getRoute: (contactMethod: string) => `settings/profile/contact-methods/${encodeURIComponent(contactMethod)}/details`,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ConnectBankAccountButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const defaultProps = {
};

function ConnectBankAccountButton(props) {
const activeRoute = Navigation.getActiveRoute().replace(/\?.*/, '');
const activeRoute = Navigation.getActiveRouteWithoutParams();
return props.network.isOffline ? (
<View style={props.style}>
<Text>{`${props.translate('common.youAppearToBeOffline')} ${props.translate('common.thisFeatureRequiresInternet')}`}</Text>
Expand Down
2 changes: 1 addition & 1 deletion src/components/CountrySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function CountrySelector({errorText, value: countryCode, onInputChange, forwarde
descriptionTextStyle={countryTitleDescStyle}
description={translate('common.country')}
onPress={() => {
const activeRoute = Navigation.getActiveRoute().replace(/\?.*/, '');
const activeRoute = Navigation.getActiveRouteWithoutParams();
Navigation.navigate(ROUTES.SETTINGS_PERSONAL_DETAILS_ADDRESS_COUNTRY.getRoute(countryCode, activeRoute));
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/MoneyRequestConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function MoneyRequestConfirmationList(props) {
*/
const navigateToReportOrUserDetail = (option) => {
if (option.accountID) {
const activeRoute = Navigation.getActiveRoute().replace(/\?.*/, '');
const activeRoute = Navigation.getActiveRouteWithoutParams();

Navigation.navigate(ROUTES.PROFILE.getRoute(option.accountID, activeRoute));
} else if (option.reportID) {
Expand Down
13 changes: 11 additions & 2 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function navigate(route = ROUTES.HOME, type) {

/**
* @param {String} fallbackRoute - Fallback route if pop/goBack action should, but is not possible within RHP
* @param {Bool} shouldEnforceFallback - Enforces navigation to fallback route
* @param {Bool} shouldPopToTop - Should we navigate to LHN on back press
* @param {Boolean} shouldEnforceFallback - Enforces navigation to fallback route
* @param {Boolean} shouldPopToTop - Should we navigate to LHN on back press
*/
function goBack(fallbackRoute, shouldEnforceFallback = false, shouldPopToTop = false) {
if (!canNavigate('goBack')) {
Expand Down Expand Up @@ -207,6 +207,14 @@ function getActiveRoute() {
return '';
}

/**
* Returns the current active route without the URL params
* @returns {String}
*/
function getActiveRouteWithoutParams() {
return getActiveRoute().replace(/\?.*/, '');
}

/** Returns the active route name from a state event from the navigationRef
* @param {Object} event
* @returns {String | undefined}
Expand Down Expand Up @@ -270,6 +278,7 @@ export default {
dismissModal,
isActiveRoute,
getActiveRoute,
getActiveRouteWithoutParams,
goBack,
isNavigationReady,
setIsNavigationReady,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/linkingConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default {
exact: true,
},
Settings_ContactMethods: {
path: ROUTES.SETTINGS_CONTACT_METHODS,
path: ROUTES.SETTINGS_CONTACT_METHODS.route,
exact: true,
},
Settings_ContactMethodDetails: {
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function deleteContactMethod(contactMethod, loginList) {
},
{optimisticData, successData, failureData},
);
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS);
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route);
}

/**
Expand Down Expand Up @@ -328,7 +328,7 @@ function addNewContactMethodAndNavigate(contactMethod) {
];

API.write('AddNewContactMethod', {partnerUserID: contactMethod}, {optimisticData, successData, failureData});
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS);
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route);
}

/**
Expand Down Expand Up @@ -755,7 +755,7 @@ function setContactMethodAsDefault(newDefaultContactMethod) {
},
];
API.write('SetContactMethodAsDefault', {partnerUserID: newDefaultContactMethod}, {optimisticData, successData, failureData});
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS);
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pages/EditRequestPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function EditRequestPage({betas, report, route, parentReport, policyCategories,
});
}}
onNavigateToCurrency={() => {
const activeRoute = encodeURIComponent(Navigation.getActiveRoute().replace(/\?.*/, ''));
const activeRoute = encodeURIComponent(Navigation.getActiveRouteWithoutParams());
Navigation.navigate(ROUTES.EDIT_CURRENCY_REQUEST.getRoute(report.reportID, defaultCurrency, activeRoute));
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/EditSplitBillPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function EditSplitBillPage({route, transaction, draftTransaction}) {
});
}}
onNavigateToCurrency={() => {
const activeRoute = encodeURIComponent(Navigation.getActiveRoute().replace(/\?.*/, ''));
const activeRoute = encodeURIComponent(Navigation.getActiveRouteWithoutParams());
Navigation.navigate(ROUTES.EDIT_SPLIT_BILL_CURRENCY.getRoute(reportID, reportActionID, defaultCurrency, activeRoute));
}}
/>
Expand Down
5 changes: 3 additions & 2 deletions src/pages/TeachersUnite/ImTeacherUpdateEmailPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultProps = {};

function ImTeacherUpdateEmailPage() {
const {translate} = useLocalize();
const activeRoute = Navigation.getActiveRouteWithoutParams();

return (
<ScreenWrapper testID={ImTeacherUpdateEmailPage.displayName}>
Expand All @@ -31,7 +32,7 @@ function ImTeacherUpdateEmailPage() {
title={translate('teachersUnitePage.updateYourEmail')}
subtitle={translate('teachersUnitePage.schoolMailAsDefault')}
linkKey="teachersUnitePage.contactMethods"
onLinkPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onLinkPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS.getRoute(activeRoute))}
iconWidth={variables.signInLogoWidthLargeScreen}
iconHeight={variables.lhnLogoWidth}
/>
Expand All @@ -40,7 +41,7 @@ function ImTeacherUpdateEmailPage() {
success
accessibilityLabel={translate('teachersUnitePage.updateEmail')}
text={translate('teachersUnitePage.updateEmail')}
onPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)}
onPress={() => Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS.getRoute(activeRoute))}
/>
</FixedFooter>
</ScreenWrapper>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/steps/NewRequestAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function NewRequestAmountPage({route, iou, report, selectedTab}) {
}

// Remove query from the route and encode it.
const activeRoute = encodeURIComponent(Navigation.getActiveRoute().replace(/\?.*/, ''));
const activeRoute = encodeURIComponent(Navigation.getActiveRouteWithoutParams());
Navigation.navigate(ROUTES.MONEY_REQUEST_CURRENCY.getRoute(iouType, reportID, currency, activeRoute));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ContactMethodDetailsPage extends Component {
// Navigate to methods page on successful magic code verification
// validatedDate property is responsible to decide the status of the magic code verification
if (!prevValidatedDate && validatedDate) {
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS);
Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route);
}
}

Expand Down Expand Up @@ -236,8 +236,8 @@ class ContactMethodDetailsPage extends Component {
<FullPageNotFoundView
shouldShow
linkKey="contacts.goBackContactMethods"
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS)}
onLinkPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS)}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route)}
onLinkPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route)}
/>
</ScreenWrapper>
);
Expand All @@ -255,7 +255,7 @@ class ContactMethodDetailsPage extends Component {
>
<HeaderWithBackButton
title={formattedContactMethod}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS)}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route)}
/>
<ScrollView keyboardShouldPersistTaps="handled">
<ConfirmModal
Expand Down
3 changes: 2 additions & 1 deletion src/pages/settings/Profile/Contacts/ContactMethodsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const defaultProps = {

function ContactMethodsPage(props) {
const loginNames = _.keys(props.loginList);
const navigateBackTo = lodashGet(props.route, 'params.backTo', ROUTES.SETTINGS_PROFILE);

// Sort the login names by placing the one corresponding to the default contact method as the first item before displaying the contact methods.
// The default contact method is determined by checking against the session email (the current login).
Expand Down Expand Up @@ -116,7 +117,7 @@ function ContactMethodsPage(props) {
>
<HeaderWithBackButton
title={props.translate('contacts.contactMethods')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
onBackButtonPress={() => Navigation.goBack(navigateBackTo)}
/>
<ScrollView contentContainerStyle={styles.flexGrow1}>
<View style={[styles.ph5, styles.mv3, styles.flexRow, styles.flexWrap]}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function NewContactMethodPage(props) {
>
<HeaderWithBackButton
title={props.translate('contacts.newContactMethod')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS)}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_CONTACT_METHODS.route)}
/>
<FormProvider
formID={ONYXKEYS.FORMS.NEW_CONTACT_METHOD_FORM}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Profile/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function ProfilePage(props) {
{
description: props.translate('contacts.contactMethod'),
title: props.formatPhoneNumber(lodashGet(currentUserDetails, 'login', '')),
pageRoute: ROUTES.SETTINGS_CONTACT_METHODS,
pageRoute: ROUTES.SETTINGS_CONTACT_METHODS.route,
brickRoadIndicator: contactMethodBrickRoadIndicator,
},
...(Permissions.canUseCustomStatus(props.betas)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceInitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function WorkspaceInitialPage(props) {
icon: Expensicons.Bank,
action: () =>
policy.outputCurrency === CONST.CURRENCY.USD
? singleExecution(waitForNavigate(() => ReimbursementAccount.navigateToBankAccountRoute(policy.id, Navigation.getActiveRoute().replace(/\?.*/, ''))))()
? singleExecution(waitForNavigate(() => ReimbursementAccount.navigateToBankAccountRoute(policy.id, Navigation.getActiveRouteWithoutParams())))()
: setIsCurrencyModalOpen(true),
brickRoadIndicator: !_.isEmpty(props.reimbursementAccount.errors) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : '',
},
Expand Down

0 comments on commit da2c79e

Please sign in to comment.