Skip to content

Commit

Permalink
Merge pull request #49 from software-mansion-labs/@kosmydel/ideal-nav…
Browse files Browse the repository at this point in the history
…-issues-v7

ideal nav issues v7
  • Loading branch information
kosmydel authored Jan 31, 2024
2 parents b33e251 + 73c6c75 commit a1142bc
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/components/AttachmentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function AttachmentModal({
const deleteAndCloseModal = useCallback(() => {
IOU.detachReceipt(transaction?.transactionID);
setIsDeleteReceiptConfirmModalVisible(false);
Navigation.dismissModalWithReportID(report?.reportID ?? '');
Navigation.dismissModal(report?.reportID);
}, [transaction, report]);

const isValidFile = useCallback((fileObject: FileObject) => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/ReportHeaderSkeletonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function ReportHeaderSkeletonView({shouldAnimate = true, onBackButtonPress = ()

return (
<View style={[styles.appContentHeader]}>
<View style={[styles.appContentHeaderTitle, !isSmallScreenWidth && styles.pl5]}>
<View style={[styles.appContentHeaderTitle]}>
{isSmallScreenWidth && (
<PressableWithFeedback
onPress={onBackButtonPress}
Expand All @@ -48,18 +48,18 @@ function ReportHeaderSkeletonView({shouldAnimate = true, onBackButtonPress = ()
>
<Circle
cx="20"
cy="33"
cy="40"
r="20"
/>
<Rect
x="55"
y="20"
y="28"
width="30%"
height="8"
/>
<Rect
x="55"
y="40"
y="48"
width="40%"
height="8"
/>
Expand Down
18 changes: 10 additions & 8 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import linkingConfig from './linkingConfig';
import linkTo from './linkTo';
import navigationRef from './navigationRef';
import switchPolicyID from './switchPolicyID';
import type {State, StateOrRoute, switchPolicyIDParams} from './types';
import type {State, StateOrRoute, SwitchPolicyIDParams} from './types';

let resolveNavigationIsReadyPromise: () => void;
const navigationIsReadyPromise = new Promise<void>((resolve) => {
Expand Down Expand Up @@ -51,17 +51,20 @@ const getTopmostReportId = (state = navigationRef.getState()) => originalGetTopm
const getTopmostReportActionId = (state = navigationRef.getState()) => originalGetTopmostReportActionId(state);

// Re-exporting the dismissModal here to fill in default value for navigationRef. The dismissModal isn't defined in this file to avoid cyclic dependencies.
const dismissModal = (ref = navigationRef) => originalDismissModal(ref);
const dismissModal = (reportID?: string, ref = navigationRef) => {
if (!reportID) {
originalDismissModal(ref);
return;
}
const report = getReport(reportID);
originalDismissModalWithReport({reportID, ...report}, ref);
};

// Re-exporting the dismissModalWithReport here to fill in default value for navigationRef. The dismissModalWithReport isn't defined in this file to avoid cyclic dependencies.
// This method is needed because it allows to dismiss the modal and then open the report. Within this method is checked whether the report belongs to a specific workspace. Sometimes the report we want to check, hasn't been added to the Onyx yet.
// Then we can pass the report as a param without getting it from the Onyx.
const dismissModalWithReport = (report: Report | EmptyObject, ref = navigationRef) => originalDismissModalWithReport(report, ref);

const dismissModalWithReportID = (reportID: string, ref = navigationRef) => {
const report = getReport(reportID);
originalDismissModalWithReport({reportID, ...report}, ref);
};
/** Method for finding on which index in stack we are. */
function getActiveRouteIndex(stateOrRoute: StateOrRoute, index?: number): number | undefined {
if ('routes' in stateOrRoute && stateOrRoute.routes) {
Expand Down Expand Up @@ -323,7 +326,7 @@ function waitForProtectedRoutes() {
});
}

function navigateWithSwitchPolicyID(params: switchPolicyIDParams) {
function navigateWithSwitchPolicyID(params: SwitchPolicyIDParams) {
if (!canNavigate('navigateWithSwitchPolicyID')) {
return;
}
Expand All @@ -337,7 +340,6 @@ export default {
setParams,
dismissModal,
dismissModalWithReport,
dismissModalWithReportID,
isActiveRoute,
getActiveRoute,
getActiveRouteWithoutParams,
Expand Down
14 changes: 10 additions & 4 deletions src/libs/Navigation/switchPolicyID.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getActionFromState} from '@react-navigation/core';
import type {NavigationAction, NavigationContainerRef, NavigationState, PartialState} from '@react-navigation/native';
import {getPathFromState} from '@react-navigation/native';
import type {Writable} from 'type-fest';
import type {ValueOf, Writable} from 'type-fest';
import getIsSmallScreenWidth from '@libs/getIsSmallScreenWidth';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
Expand All @@ -11,7 +11,7 @@ import getStateFromPath from './getStateFromPath';
import getTopmostCentralPaneRoute from './getTopmostCentralPaneRoute';
import linkingConfig from './linkingConfig';
import TAB_TO_CENTRAL_PANE_MAPPING from './linkingConfig/TAB_TO_CENTRAL_PANE_MAPPING';
import type {NavigationRoot, RootStackParamList, StackNavigationAction, State, switchPolicyIDParams} from './types';
import type {NavigationRoot, RootStackParamList, StackNavigationAction, State, SwitchPolicyIDParams} from './types';

type ActionPayloadParams = {
screen?: string;
Expand Down Expand Up @@ -60,7 +60,7 @@ function getActionForBottomTabNavigator(action: StackNavigationAction, state: Na
};
}

export default function switchPolicyID(navigation: NavigationContainerRef<RootStackParamList> | null, {policyID, route}: switchPolicyIDParams) {
export default function switchPolicyID(navigation: NavigationContainerRef<RootStackParamList> | null, {policyID, route, isPolicyAdmin = false}: SwitchPolicyIDParams) {
if (!navigation) {
throw new Error("Couldn't find a navigation object. Is your component inside a screen in a navigator?");
}
Expand Down Expand Up @@ -110,15 +110,21 @@ export default function switchPolicyID(navigation: NavigationContainerRef<RootSt
const topmostCentralPaneRoute = getTopmostCentralPaneRoute(rootState);
let screen = topmostCentralPaneRoute?.name;
const params: CentralPaneRouteParams = {...topmostCentralPaneRoute?.params};
const isWorkspaceScreen = screen && Object.values(SCREENS.WORKSPACE).includes(screen as ValueOf<typeof SCREENS.WORKSPACE>);

// Only workspace settings screens have to store the policyID in the params.
// In other case, the policyID is read from the BottomTab params.
if (!screen?.startsWith('Workspace_')) {
if (!isWorkspaceScreen) {
delete params.policyID;
} else {
params.policyID = policyID;
}

// We need to redirect non admin users to overview screen, when switching workspace.
if (!isPolicyAdmin && isWorkspaceScreen && screen !== SCREENS.WORKSPACE.OVERVIEW) {
screen = SCREENS.WORKSPACE.OVERVIEW;
}

// If the user is on the home page and changes the current workspace, then should be displayed a report from the selected workspace.
// To achieve that, it's necessary to navigate without the reportID param.
if (checkIfActionPayloadNameIsEqual(actionForBottomTabNavigator, SCREENS.HOME)) {
Expand Down
5 changes: 3 additions & 2 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,10 @@ type CentralPaneName = keyof CentralPaneNavigatorParamList;

type FullScreenName = keyof SettingsCentralPaneNavigatorParamList;

type switchPolicyIDParams = {
type SwitchPolicyIDParams = {
policyID?: string;
route?: Routes;
isPolicyAdmin?: boolean;
};

export type {
Expand Down Expand Up @@ -519,5 +520,5 @@ export type {
ReimbursementAccountNavigatorParamList,
State,
WorkspaceSwitcherNavigatorParamList,
switchPolicyIDParams,
SwitchPolicyIDParams,
};
12 changes: 6 additions & 6 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ function createDistanceRequest(report, participant, comment, created, category,
},
onyxData,
);
Navigation.dismissModalWithReportID(isMoneyRequestReport ? report.reportID : chatReport.reportID);
Navigation.dismissModal(isMoneyRequestReport ? report.reportID : chatReport.reportID);
Report.notifyNewAction(chatReport.reportID, userAccountID);
}

Expand Down Expand Up @@ -1364,7 +1364,7 @@ function requestMoney(
onyxData,
);
resetMoneyRequestInfo();
Navigation.dismissModalWithReportID(activeReportID);
Navigation.dismissModal(activeReportID);
Report.notifyNewAction(activeReportID, payeeAccountID);
}

Expand Down Expand Up @@ -1806,7 +1806,7 @@ function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccou
);

resetMoneyRequestInfo();
Navigation.dismissModalWithReportID(splitData.chatReportID);
Navigation.dismissModal(splitData.chatReportID);
Report.notifyNewAction(splitData.chatReportID, currentUserAccountID);
}

Expand Down Expand Up @@ -2288,7 +2288,7 @@ function completeSplitBill(chatReportID, reportAction, updatedTransaction, sessi
},
{optimisticData, successData, failureData},
);
Navigation.dismissModalWithReportID(chatReportID);
Navigation.dismissModal(chatReportID);
Report.notifyNewAction(chatReportID, sessionAccountID);
}

Expand Down Expand Up @@ -3244,7 +3244,7 @@ function sendMoneyElsewhere(report, amount, currency, comment, managerID, recipi
API.write('SendMoneyElsewhere', params, {optimisticData, successData, failureData});

resetMoneyRequestInfo();
Navigation.dismissModalWithReportID(params.chatReportID);
Navigation.dismissModal(params.chatReportID);
Report.notifyNewAction(params.chatReportID, managerID);
}

Expand All @@ -3262,7 +3262,7 @@ function sendMoneyWithWallet(report, amount, currency, comment, managerID, recip
API.write('SendMoneyWithWallet', params, {optimisticData, successData, failureData});

resetMoneyRequestInfo();
Navigation.dismissModalWithReportID(params.chatReportID);
Navigation.dismissModal(params.chatReportID);
Report.notifyNewAction(params.chatReportID, managerID);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ function addPolicyReport(policyReport: ReportUtils.OptimisticChatReport) {
};

API.write(WRITE_COMMANDS.ADD_WORKSPACE_ROOM, parameters, {optimisticData, successData, failureData});
Navigation.dismissModalWithReportID(policyReport.reportID);
Navigation.dismissModal(policyReport.reportID);
}

/** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function createTaskAndNavigate(

API.write(WRITE_COMMANDS.CREATE_TASK, parameters, {optimisticData, successData, failureData});

Navigation.dismissModalWithReportID(parentReportID);
Navigation.dismissModal(parentReportID);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/TeachersUnite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function referTeachersUniteVolunteer(partnerUserID: string, firstName: string, l
};

API.write(WRITE_COMMANDS.REFER_TEACHERS_UNITE_VOLUNTEER, parameters, {optimisticData});
Navigation.dismissModalWithReportID(publicRoomReportID);
Navigation.dismissModal(publicRoomReportID);
}

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ function addSchoolPrincipal(firstName: string, partnerUserID: string, lastName:
};

API.write(WRITE_COMMANDS.ADD_SCHOOL_PRINCIPAL, parameters, {optimisticData, successData, failureData});
Navigation.dismissModalWithReportID(expenseChatReportID);
Navigation.dismissModal(expenseChatReportID);
}

export default {referTeachersUniteVolunteer, addSchoolPrincipal};
2 changes: 1 addition & 1 deletion src/pages/AddPersonalBankAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function AddPersonalBankAccountPage({personalBankAccount, plaidData}: AddPersona
const onSuccessFallbackRoute = personalBankAccount?.onSuccessFallbackRoute ?? '';

if (exitReportID) {
Navigation.dismissModalWithReportID(exitReportID);
Navigation.dismissModal(exitReportID);
} else if (shouldContinue && onSuccessFallbackRoute) {
PaymentMethods.continueSetup(onSuccessFallbackRoute);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/EditRequestDistancePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function EditRequestDistancePage({report, route, transaction, transactionBackup}
// When the loading goes from true to false, then we know the transaction has just been
// saved to the server. Check for errors. If there are no errors, then the modal can be closed.
if (prevIsLoading && !transaction.isLoading && !hasWaypointError.current) {
Navigation.dismissModalWithReportID(report.reportID);
Navigation.dismissModal(report.reportID);
}
}, [transaction, prevIsLoading, report]);

Expand All @@ -75,7 +75,7 @@ function EditRequestDistancePage({report, route, transaction, transactionBackup}
const oldAddresses = _.mapObject(oldWaypoints, (waypoint) => _.pick(waypoint, 'address'));
const addresses = _.mapObject(waypoints, (waypoint) => _.pick(waypoint, 'address'));
if (_.isEqual(oldAddresses, addresses)) {
Navigation.dismissModalWithReportID(report.reportID);
Navigation.dismissModal(report.reportID);
return;
}

Expand All @@ -84,7 +84,7 @@ function EditRequestDistancePage({report, route, transaction, transactionBackup}
// If the client is offline, then the modal can be closed as well (because there are no errors or other feedback to show them
// until they come online again and sync with the server).
if (isOffline) {
Navigation.dismissModalWithReportID(report.reportID);
Navigation.dismissModal(report.reportID);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/SearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function SearchPage({betas, personalDetails, reports, isSearchingForReports, nav
return;
}
if (option.reportID) {
Navigation.dismissModalWithReportID(option.reportID);
Navigation.dismissModal(option.reportID);
} else {
Report.navigateToAndOpenReport([option.login]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/SearchPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function SearchPage({betas, reports}) {

if (option.reportID) {
setSearchValue('');
Navigation.dismissModalWithReportID(option.reportID);
Navigation.dismissModal(option.reportID);
} else {
Report.navigateToAndOpenReport([option.login]);
}
Expand Down
9 changes: 2 additions & 7 deletions src/pages/ShareCodePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ class ShareCodePage extends React.Component {
shouldShowBackButton={isReport || this.props.isSmallScreenWidth}
/>

<ScrollView style={[this.props.themeStyles.flex1, this.props.themeStyles.mt3]}>
<View
style={[
this.props.themeStyles.shareCodePage,
this.props.isSmallScreenWidth ? this.props.themeStyles.workspaceSectionMobile : this.props.themeStyles.workspaceSection,
]}
>
<ScrollView style={[this.props.themeStyles.flex1]}>
<View style={[this.props.isSmallScreenWidth ? this.props.themeStyles.workspaceSectionMobile : this.props.themeStyles.workspaceSection, this.props.themeStyles.ph4]}>
<QRShareWithDownload
ref={this.qrCodeRef}
url={url}
Expand Down
17 changes: 7 additions & 10 deletions src/pages/WorkspaceSwitcherPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function WorkspaceSwitcherPage({policies}) {
[unreadStatusesForPolicies],
);

const selectPolicy = useCallback((option) => {
const policyID = option.policyID;
const selectPolicy = (option) => {
const {policyID, isPolicyAdmin} = option;

if (policyID) {
setSelectedOption(option);
Expand All @@ -121,13 +121,9 @@ function WorkspaceSwitcherPage({policies}) {
setActiveWorkspaceID(policyID);
Navigation.goBack();
if (policyID !== activeWorkspaceID) {
Navigation.navigateWithSwitchPolicyID({policyID});
Navigation.navigateWithSwitchPolicyID({policyID, isPolicyAdmin});
}
}, []);

const onChangeText = useCallback((newSearchTerm) => {
setSearchTerm(newSearchTerm);
}, []);
};

const usersWorkspaces = useMemo(
() =>
Expand All @@ -147,6 +143,7 @@ function WorkspaceSwitcherPage({policies}) {
],
boldStyle: hasUnreadData(policy.id),
keyForList: policy.id,
isPolicyAdmin: PolicyUtils.isPolicyAdmin(policy),
}))
.value(),
[policies, getIndicatorTypeForPolicy, hasUnreadData],
Expand Down Expand Up @@ -247,7 +244,7 @@ function WorkspaceSwitcherPage({policies}) {
sections={[usersWorkspacesSectionData]}
value={searchTerm}
shouldShowTextInput={usersWorkspaces.length >= CONST.WORKSPACE_SWITCHER.MINIMUM_WORKSPACES_TO_SHOW_SEARCH}
onChangeText={onChangeText}
onChangeText={(newSearchTerm) => setSearchTerm(newSearchTerm)}
selectedOptions={selectedOption ? [selectedOption] : []}
onSelectRow={selectPolicy}
shouldPreventDefaultFocusOnSelectRow
Expand All @@ -269,7 +266,7 @@ function WorkspaceSwitcherPage({policies}) {
)}
</>
),
[inputCallbackRef, onChangeText, searchTerm, selectPolicy, selectedOption, styles, theme.textSupporting, translate, usersWorkspaces.length, usersWorkspacesSectionData],
[inputCallbackRef, setSearchTerm, searchTerm, selectPolicy, selectedOption, styles, theme.textSupporting, translate, usersWorkspaces.length, usersWorkspacesSectionData],
);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/HeaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function HeaderView(props) {
dataSet={{dragArea: true}}
>
<View style={[styles.appContentHeader]}>
<View style={[styles.appContentHeaderTitle, !isSmallScreenWidth && !isLoading && styles.pl5]}>
<View style={[styles.appContentHeaderTitle, !isSmallScreenWidth && styles.pl5]}>
{isLoading ? (
<ReportHeaderSkeletonView onBackButtonPress={props.onNavigationMenuButtonClicked} />
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Preferences/ThemePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function ThemePage(props) {
title={translate('themePage.theme')}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack()}
onCloseButtonPress={() => Navigation.dismissModal(true)}
onCloseButtonPress={() => Navigation.dismissModal()}
/>

<Text style={[styles.mh5, styles.mv4]}>{translate('themePage.chooseThemeBelowOrSync')}</Text>
Expand Down
Loading

0 comments on commit a1142bc

Please sign in to comment.