Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display not found page when deleting the workspace in OD #55508

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libs/API/parameters/OpenWorkspaceViewParams.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type OpenWorkspaceViewParams = {
policyID: string;
policyID: string | undefined;
};

export default OpenWorkspaceViewParams;
2 changes: 1 addition & 1 deletion src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ function verifyIdentityForBankAccount(bankAccountID: number, onfidoData: OnfidoD
API.write(WRITE_COMMANDS.VERIFY_IDENTITY_FOR_BANK_ACCOUNT, parameters, getVBBADataForOnyx());
}

function openWorkspaceView(policyID: string) {
function openWorkspaceView(policyID: string | undefined) {
API.read(
READ_COMMANDS.OPEN_WORKSPACE_VIEW,
{
Expand Down
13 changes: 7 additions & 6 deletions src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import usePrevious from '@hooks/usePrevious';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
import {confirmReadyToOpenApp} from '@libs/actions/App';
import {isConnectionInProgress} from '@libs/actions/connections';
import {clearErrors, openPolicyInitialPage, removeWorkspace, updateGeneralSettings} from '@libs/actions/Policy/Policy';
import {navigateToBankAccountRoute} from '@libs/actions/ReimbursementAccount';
Expand All @@ -60,7 +61,6 @@ import {
} from '@libs/PolicyUtils';
import {getDefaultWorkspaceAvatar, getIcons, getPolicyExpenseChat, getReportName, getReportOfflinePendingActionAndErrors} from '@libs/ReportUtils';
import type {FullScreenNavigatorParamList} from '@navigation/types';
import {confirmReadyToOpenApp} from '@userActions/App';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -400,17 +400,18 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, route}: Workspac
const prevPolicy = usePrevious(policy);

const shouldShowPolicy = useMemo(() => checkIfShouldShowPolicy(policy, isOffline, currentUserLogin), [policy, isOffline, currentUserLogin]);
const prevShouldShowPolicy = useMemo(() => checkIfShouldShowPolicy(prevPolicy, isOffline, currentUserLogin), [prevPolicy, isOffline, currentUserLogin]);
// We check shouldShowPolicy and prevShouldShowPolicy to prevent the NotFound view from showing right after we delete the workspace
const isPendingDelete = isPendingDeletePolicy(policy);
const prevIsPendingDelete = isPendingDeletePolicy(prevPolicy);
// We check isPendingDelete and prevIsPendingDelete to prevent the NotFound view from showing right after we delete the workspace
// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage = isEmptyObject(policy) || (!shouldShowPolicy && !prevShouldShowPolicy);
const shouldShowNotFoundPage = !shouldShowPolicy && (!isPendingDelete || prevIsPendingDelete);

useEffect(() => {
if (isEmptyObject(prevPolicy) || isPendingDeletePolicy(prevPolicy) || !isPendingDeletePolicy(policy)) {
if (isEmptyObject(prevPolicy) || prevIsPendingDelete || !isPendingDelete) {
return;
}
goBackFromInvalidPolicy();
}, [policy, prevPolicy]);
}, [isPendingDelete, policy, prevIsPendingDelete, prevPolicy]);

// We are checking if the user can access the route.
// If user can't access the route, we are dismissing any modals that are open when the NotFound view is shown
Expand Down
21 changes: 12 additions & 9 deletions src/pages/workspace/WorkspacePageWithSections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton';
import type HeaderWithBackButtonProps from '@components/HeaderWithBackButton/types';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollViewWithContext from '@components/ScrollViewWithContext';
import useNetwork from '@hooks/useNetwork';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {openWorkspaceView} from '@libs/actions/BankAccounts';
import BankAccount from '@libs/models/BankAccount';
import Navigation from '@libs/Navigation/Navigation';
import {isPolicyAdmin, shouldShowPolicy as shouldShowPolicyUtil} from '@libs/PolicyUtils';
import {isPendingDeletePolicy, isPolicyAdmin, shouldShowPolicy as shouldShowPolicyUtil} from '@libs/PolicyUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
Expand All @@ -34,7 +35,7 @@ type WorkspacePageWithSectionsProps = WithPolicyAndFullscreenLoadingProps &
headerText: string;

/** Main content of the page */
children: ((hasVBA: boolean, policyID: string, isUsingECard: boolean) => ReactNode) | ReactNode;
children: ((hasVBA: boolean, policyID: string | undefined, isUsingECard: boolean) => ReactNode) | ReactNode;

/** Content to be added as fixed footer */
footer?: ReactNode;
Expand Down Expand Up @@ -86,7 +87,7 @@ type WorkspacePageWithSectionsProps = WithPolicyAndFullscreenLoadingProps &
isLoading?: boolean;
};

function fetchData(policyID: string, skipVBBACal?: boolean) {
function fetchData(policyID: string | undefined, skipVBBACal?: boolean) {
if (skipVBBACal) {
return;
}
Expand Down Expand Up @@ -122,7 +123,8 @@ function WorkspacePageWithSections({
threeDotsAnchorPosition,
}: WorkspacePageWithSectionsProps) {
const styles = useThemeStyles();
const policyID = route.params?.policyID ?? `${CONST.DEFAULT_NUMBER_ID}`;
const policyID = route.params?.policyID;
const {isOffline} = useNetwork({onReconnect: () => fetchData(policyID, shouldSkipVBBACall)});

const [user] = useOnyx(ONYXKEYS.USER);
const [reimbursementAccount = CONST.REIMBURSEMENT_ACCOUNT.DEFAULT_DATA] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
Expand All @@ -149,18 +151,19 @@ function WorkspacePageWithSections({
}, [policyID, shouldSkipVBBACall]),
);

const shouldShowPolicy = useMemo(() => shouldShowPolicyUtil(policy, false, currentUserLogin), [policy, currentUserLogin]);
const prevShouldShowPolicy = useMemo(() => shouldShowPolicyUtil(prevPolicy, false, currentUserLogin), [prevPolicy, currentUserLogin]);
const shouldShowPolicy = useMemo(() => shouldShowPolicyUtil(policy, isOffline, currentUserLogin), [policy, isOffline, currentUserLogin]);
const isPendingDelete = isPendingDeletePolicy(policy);
const prevIsPendingDelete = isPendingDeletePolicy(prevPolicy);
const shouldShow = useMemo(() => {
// If the policy object doesn't exist or contains only error data, we shouldn't display it.
if (((isEmptyObject(policy) || (Object.keys(policy).length === 1 && !isEmptyObject(policy.errors))) && isEmptyObject(policyDraft)) || shouldShowNotFoundPage) {
return true;
}

// We check shouldShowPolicy and prevShouldShowPolicy to prevent the NotFound view from showing right after we delete the workspace
return (!isEmptyObject(policy) && !isPolicyAdmin(policy) && !shouldShowNonAdmin) || (!shouldShowPolicy && !prevShouldShowPolicy);
// We check isPendingDelete and prevIsPendingDelete to prevent the NotFound view from showing right after we delete the workspace
return (!isEmptyObject(policy) && !isPolicyAdmin(policy) && !shouldShowNonAdmin) || (!shouldShowPolicy && (!isPendingDelete || prevIsPendingDelete));
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [policy, shouldShowNonAdmin, shouldShowPolicy, prevShouldShowPolicy]);
}, [policy, shouldShowNonAdmin, shouldShowPolicy]);

return (
<ScreenWrapper
Expand Down
3 changes: 2 additions & 1 deletion src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {FullScreenNavigatorParamList, RootStackParamList, State} from '@libs/Navigation/types';
import Parser from '@libs/Parser';
import {getUserFriendlyWorkspaceType, getWorkspaceAccountID, isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyOwner} from '@libs/PolicyUtils';
import {getUserFriendlyWorkspaceType, getWorkspaceAccountID, goBackFromInvalidPolicy, isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyOwner} from '@libs/PolicyUtils';
import {getDefaultWorkspaceAvatar} from '@libs/ReportUtils';
import StringUtils from '@libs/StringUtils';
import {getFullSizeAvatar} from '@libs/UserUtils';
Expand Down Expand Up @@ -174,6 +174,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, route}: Workspac
Navigation.setParams({policyID: undefined}, topmostBottomTabRoute?.key);
}
}
goBackFromInvalidPolicy();
}, [policy?.id, policyName, activeWorkspaceID, setActiveWorkspaceID]);

return (
Expand Down
Loading