Skip to content

Commit

Permalink
Add popToTop method to Navigation.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Dec 11, 2024
1 parent 686dde6 commit d7d0ff3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 25 deletions.
30 changes: 14 additions & 16 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,10 @@ type GoBackOptions = {
* In that case we want to goUp to a country picker with any params so we don't compare them.
*/
compareParams?: boolean;

/**
* Specifies whether goBack should pop to top when invoked.
* Additionaly, to execute popToTop, set the value of the global variable ShouldPopAllStateOnUP to true using the setShouldPopAllStateOnUP function.
*/
shouldPopToTop?: boolean;
};

const defaultGoBackOptions: Required<GoBackOptions> = {
compareParams: true,
shouldPopToTop: false,
};

/**
Expand Down Expand Up @@ -274,21 +267,13 @@ function goUp(fallbackRoute: Route, options?: GoBackOptions) {

/**
* @param fallbackRoute - Fallback route if pop/goBack action should, but is not possible within RHP
* @param options - Optional configuration that affects navigation logic, e.g. whether goBack should popToTop.
* @param options - Optional configuration that affects navigation logic.
*/
function goBack(fallbackRoute?: Route, options?: GoBackOptions) {
if (!canNavigate('goBack')) {
return;
}

if (options?.shouldPopToTop) {
if (shouldPopAllStateOnUP) {
shouldPopAllStateOnUP = false;
navigationRef.current?.dispatch(StackActions.popToTop());
return;
}
}

if (fallbackRoute) {
goUp(fallbackRoute, options);
return;
Expand Down Expand Up @@ -485,6 +470,18 @@ const dismissModalWithReport = (report: OnyxEntry<Report>) => {
isNavigationReady().then(() => navigateToReportWithPolicyCheck({report}));
};

/**
* Returns to the first screen in the stack, dismissing all the others, only if the global variable shouldPopAllStateOnUP is set to true.
*/
function popToTop() {
if (!shouldPopAllStateOnUP) {
return;
}

shouldPopAllStateOnUP = false;
navigationRef.current?.dispatch(StackActions.popToTop());
}

export default {
setShouldPopAllStateOnUP,
navigate,
Expand All @@ -508,6 +505,7 @@ export default {
closeRHPFlow,
setNavigationActionToMicrotaskQueue,
navigateToReportWithPolicyCheck,
popToTop,
};

export {navigationRef};
2 changes: 1 addition & 1 deletion src/libs/navigateAfterJoinRequest/index.desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ROUTES from '@src/ROUTES';

const navigateAfterJoinRequest = () => {
// @TODO: Check if this method works the same as on the main branch
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
if (getIsSmallScreenWidth()) {
Navigation.navigate(ROUTES.SETTINGS);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/navigateAfterJoinRequest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ROUTES from '@src/ROUTES';

const navigateAfterJoinRequest = () => {
// @TODO: Check if this method works the same as on the main branch
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
Navigation.navigate(ROUTES.SETTINGS);
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES);
};
Expand Down
2 changes: 1 addition & 1 deletion src/libs/navigateAfterJoinRequest/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ROUTES from '@src/ROUTES';

const navigateAfterJoinRequest = () => {
// @TODO: Check if this method works the same as on the main branch
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
if (getIsSmallScreenWidth()) {
Navigation.navigate(ROUTES.SETTINGS);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function AddBankAccount() {
PaymentMethods.continueSetup(onSuccessFallbackRoute);
return;
}
Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true});
Navigation.goBack(ROUTES.SETTINGS_WALLET);
};

const handleBackButtonPress = () => {
Expand All @@ -63,7 +63,7 @@ function AddBankAccount() {
if (screenIndex === 0) {
BankAccounts.clearPersonalBankAccount();
Wallet.updateCurrentStep(null);
Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true});
Navigation.goBack(ROUTES.SETTINGS_WALLET);
return;
}
prevScreen();
Expand Down
2 changes: 1 addition & 1 deletion src/pages/EnablePayments/EnablePayments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function EnablePaymentsPage() {
>
<HeaderWithBackButton
title={translate('personalInfoStep.personalInfo')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET, {shouldPopToTop: true})}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
/>
<FailedKYC />
</ScreenWrapper>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
return;
}
// @TODO: Check if this method works the same as on the main branch
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
}, [isInNarrowPaneModal]);

let headerView = (
Expand Down Expand Up @@ -592,7 +592,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
if (Navigation.getTopmostReportId() === prevOnyxReportID) {
Navigation.setShouldPopAllStateOnUP(true);
// @TODO: Check if this method works the same as on the main branch
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
}
if (prevReport?.parentReportID) {
// Prevent navigation to the IOU/Expense Report if it is pending deletion.
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceJoinUserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function WorkspaceJoinUserPage({route, policy}: WorkspaceJoinUserPageProps) {
Navigation.isNavigationReady().then(() => {
// @TODO: Check if this method works the same as on the main branch
// NOTE: It probably doesn't need any params. When this method is called, shouldPopAllStateOnUP is always false
Navigation.goBack(undefined, {shouldPopToTop: true});
Navigation.popToTop();
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID ?? '-1'));
});
return;
Expand Down

0 comments on commit d7d0ff3

Please sign in to comment.