Skip to content

Commit

Permalink
fix: stale isLoadingReportData Onyx value
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictb committed Oct 29, 2024
1 parent da44573 commit 63029f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import type {ParamListBase, PartialState, RouterConfigOptions, StackNavigationState} from '@react-navigation/native';
import {StackRouter} from '@react-navigation/native';
import Onyx from 'react-native-onyx';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import * as PolicyUtils from '@libs/PolicyUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import type {FullScreenNavigatorRouterOptions} from './types';

type StackState = StackNavigationState<ParamListBase> | PartialState<StackNavigationState<ParamListBase>>;

const isAtLeastOneInState = (state: StackState, screenName: string): boolean => state.routes.some((route) => route.name === screenName);

let isLoadingReportData = true;
Onyx.connect({
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => (isLoadingReportData = value ?? false),
});

function adaptStateIfNecessary(state: StackState) {
const isNarrowLayout = getIsNarrowLayout();
const workspaceCentralPane = state.routes.at(-1);
Expand All @@ -17,7 +26,6 @@ function adaptStateIfNecessary(state: StackState) {
? workspaceCentralPane.params.policyID
: undefined;
const policy = PolicyUtils.getPolicy(policyID ?? '');
const isLoadingReportData = PolicyUtils.getIsLoadingReportData();
const isPolicyAccessible = PolicyUtils.isPolicyAccessible(policy);

// There should always be WORKSPACE.INITIAL screen in the state to make sure go back works properly if we deeplinkg to a subpage of settings.
Expand Down
16 changes: 9 additions & 7 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,15 @@ function getTopMostCentralPaneRouteFromRootState() {
}

function removeScreenFromNavigationState(screen: Screen) {
navigationRef.dispatch((state) => {
const routes = state.routes?.filter((item) => item.name !== screen);

return CommonActions.reset({
...state,
routes,
index: routes.length < state.routes.length ? state.index - 1 : state.index,
isNavigationReady().then(() => {
navigationRef.dispatch((state) => {
const routes = state.routes?.filter((item) => item.name !== screen);

return CommonActions.reset({
...state,
routes,
index: routes.length < state.routes.length ? state.index - 1 : state.index,
});
});
});
}
Expand Down
12 changes: 0 additions & 12 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ Onyx.connect({
callback: (value) => (activePolicyId = value),
});

let isLoadingReportData = true;
Onyx.connect({
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => (isLoadingReportData = value ?? false),
});

/**
* Filter out the active policies, which will exclude policies with pending deletion
* These are policies that we can use to create reports with in NewDot.
Expand Down Expand Up @@ -1075,10 +1068,6 @@ function isPolicyAccessible(policy: OnyxEntry<Policy>): boolean {
return !isEmptyObject(policy) && (Object.keys(policy).length !== 1 || isEmptyObject(policy.errors)) && !!policy?.id;
}

function getIsLoadingReportData() {
return isLoadingReportData;
}

export {
canEditTaxRate,
extractPolicyIDFromPath,
Expand Down Expand Up @@ -1197,7 +1186,6 @@ export {
getAllPoliciesLength,
getActivePolicy,
isPolicyAccessible,
getIsLoadingReportData,
};

export type {MemberEmailsToAccountIDs};
1 change: 1 addition & 0 deletions src/pages/workspace/AccessOrNotFoundWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function AccessOrNotFoundWrapper({
}, [pendingField, isOffline, isFeatureEnabled]);

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (isLoadingReportData || !isPolicyNotAccessible) {
return;
}
Expand Down

0 comments on commit 63029f1

Please sign in to comment.