Skip to content
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
8 changes: 8 additions & 0 deletions src/hooks/useActivePolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ONYXKEYS from '@src/ONYXKEYS';
import useOnyx from './useOnyx';
import usePolicy from './usePolicy';

export default function useActivePolicy() {
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: false});
return usePolicy(activePolicyID);
}
16 changes: 4 additions & 12 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,12 +1203,6 @@ Onyx.connectWithoutView({
},
});

let activePolicyID: OnyxEntry<string>;
Onyx.connectWithoutView({
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyID = value),
});

let reportAttributesDerivedValue: ReportAttributesDerivedValue['reports'];
Onyx.connectWithoutView({
key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES,
Expand Down Expand Up @@ -11172,6 +11166,7 @@ function createDraftTransactionAndNavigateToParticipantSelector(
actionName: IOUAction,
reportActionID: string | undefined,
introSelected: OnyxEntry<IntroSelected>,
activePolicy: OnyxEntry<Policy>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the guideline Avoid Broad Object Dependencies:

When components and utilities require large objects instead of the specific data they use, if any part of those objects changes, then unrelated components must re-render and forward props they do not conceptually depend on.

since only the ID is needed, the param to this should be activePolicyID rather than activePolicy

isRestrictedToPreferredPolicy = false,
preferredPolicyID?: string,
): void {
Expand Down Expand Up @@ -11215,23 +11210,20 @@ function createDraftTransactionAndNavigateToParticipantSelector(
const filteredPolicies = Object.values(allPolicies ?? {}).filter((policy) => shouldShowPolicy(policy, false, currentUserEmail));

if (actionName === CONST.IOU.ACTION.CATEGORIZE) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const activePolicy = getPolicy(activePolicyID);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roryabraham We pass active policy to use in here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because getPolicy also be deprecated now, so in this PR we pass activePolicy to remove getPolicy call

if (activePolicy && shouldRestrictUserBillableActions(activePolicy.id)) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(activePolicy.id));
return;
}

if (shouldShowPolicy(activePolicy, false, currentUserEmail)) {
const policyExpenseReportID = getPolicyExpenseChat(currentUserAccountID, activePolicyID)?.reportID;
if (activePolicy && shouldShowPolicy(activePolicy, false, currentUserEmail)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roryabraham we also need the whole activePolicy for shouldShowPolicy here

const policyExpenseReportID = getPolicyExpenseChat(currentUserAccountID, activePolicy.id)?.reportID;
setMoneyRequestParticipants(transactionID, [
{
selected: true,
accountID: 0,
isPolicyExpenseChat: true,
reportID: policyExpenseReportID,
policyID: activePolicyID,
policyID: activePolicy.id,
searchText: activePolicy?.name,
},
]);
Expand Down
22 changes: 20 additions & 2 deletions src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import RoomHeaderAvatars from '@components/RoomHeaderAvatars';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import {useSearchContext} from '@components/Search/SearchContext';
import useActivePolicy from '@hooks/useActivePolicy';
import useAncestors from '@hooks/useAncestors';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDeleteTransactions from '@hooks/useDeleteTransactions';
Expand Down Expand Up @@ -155,6 +156,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
const {translate, localeCompare, formatPhoneNumber} = useLocalize();
const {isOffline} = useNetwork();
const {isRestrictedToPreferredPolicy, preferredPolicyID} = usePreferredPolicy();
const activePolicy = useActivePolicy();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - only ID needed.

const styles = useThemeStyles();
const expensifyIcons = useMemoizedLazyExpensifyIcons(['Users', 'Gear', 'Send', 'Folder', 'UserPlus', 'Pencil', 'Checkmark', 'Building', 'Exit', 'Bug', 'Camera', 'Trashcan']);
const backTo = route.params.backTo;
Expand Down Expand Up @@ -440,6 +442,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
CONST.IOU.ACTION.SUBMIT,
actionableWhisperReportActionID,
introSelected,
activePolicy,
isRestrictedToPreferredPolicy,
preferredPolicyID,
);
Expand All @@ -453,7 +456,14 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
isAnonymousAction: false,
shouldShowRightIcon: true,
action: () => {
createDraftTransactionAndNavigateToParticipantSelector(iouTransactionID, actionReportID, CONST.IOU.ACTION.CATEGORIZE, actionableWhisperReportActionID, introSelected);
createDraftTransactionAndNavigateToParticipantSelector(
iouTransactionID,
actionReportID,
CONST.IOU.ACTION.CATEGORIZE,
actionableWhisperReportActionID,
introSelected,
activePolicy,
);
},
});
items.push({
Expand All @@ -463,7 +473,14 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
isAnonymousAction: false,
shouldShowRightIcon: true,
action: () => {
createDraftTransactionAndNavigateToParticipantSelector(iouTransactionID, actionReportID, CONST.IOU.ACTION.SHARE, actionableWhisperReportActionID, introSelected);
createDraftTransactionAndNavigateToParticipantSelector(
iouTransactionID,
actionReportID,
CONST.IOU.ACTION.SHARE,
actionableWhisperReportActionID,
introSelected,
activePolicy,
);
},
});
}
Expand Down Expand Up @@ -584,6 +601,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
isRestrictedToPreferredPolicy,
preferredPolicyID,
introSelected,
activePolicy,
parentReport,
]);

Expand Down
23 changes: 21 additions & 2 deletions src/pages/home/report/PureReportActionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {ShowContextMenuContext} from '@components/ShowContextMenuContext';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import UnreadActionIndicator from '@components/UnreadActionIndicator';
import useActivePolicy from '@hooks/useActivePolicy';
import useConfirmModal from '@hooks/useConfirmModal';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
Expand Down Expand Up @@ -358,6 +359,7 @@ type PureReportActionItemProps = {
actionName: IOUAction,
reportActionID: string,
introSelected: OnyxEntry<OnyxTypes.IntroSelected>,
activePolicy: OnyxEntry<OnyxTypes.Policy>,
isRestrictedToPreferredPolicy?: boolean,
preferredPolicyID?: string,
) => void;
Expand Down Expand Up @@ -517,6 +519,7 @@ function PureReportActionItem({
const [isEmojiPickerActive, setIsEmojiPickerActive] = useState<boolean | undefined>();
const [isPaymentMethodPopoverActive, setIsPaymentMethodPopoverActive] = useState<boolean | undefined>();
const {isRestrictedToPreferredPolicy, preferredPolicyID} = usePreferredPolicy();
const activePolicy = useActivePolicy();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again it seems like we might need only the ID, so we should provide a selector to useActivePolicy to get just the ID.

const shouldRenderViewBasedOnAction = useTableReportViewActionRenderConditionals(action);
const [isHidden, setIsHidden] = useState(false);
const [moderationDecision, setModerationDecision] = useState<OnyxTypes.DecisionName>(CONST.MODERATION.MODERATOR_DECISION_APPROVED);
Expand Down Expand Up @@ -877,6 +880,7 @@ function PureReportActionItem({
CONST.IOU.ACTION.SUBMIT,
action.reportActionID,
introSelected,
activePolicy,
isRestrictedToPreferredPolicy,
preferredPolicyID,
);
Expand All @@ -890,14 +894,28 @@ function PureReportActionItem({
text: 'actionableMentionTrackExpense.categorize',
key: `${action.reportActionID}-actionableMentionTrackExpense-categorize`,
onPress: () => {
createDraftTransactionAndNavigateToParticipantSelector(transactionID, reportActionReportID, CONST.IOU.ACTION.CATEGORIZE, action.reportActionID, introSelected);
createDraftTransactionAndNavigateToParticipantSelector(
transactionID,
reportActionReportID,
CONST.IOU.ACTION.CATEGORIZE,
action.reportActionID,
introSelected,
activePolicy,
);
},
},
{
text: 'actionableMentionTrackExpense.share',
key: `${action.reportActionID}-actionableMentionTrackExpense-share`,
onPress: () => {
createDraftTransactionAndNavigateToParticipantSelector(transactionID, reportActionReportID, CONST.IOU.ACTION.SHARE, action.reportActionID, introSelected);
createDraftTransactionAndNavigateToParticipantSelector(
transactionID,
reportActionReportID,
CONST.IOU.ACTION.SHARE,
action.reportActionID,
introSelected,
activePolicy,
);
},
},
);
Expand Down Expand Up @@ -1037,6 +1055,7 @@ function PureReportActionItem({
isOriginalReportArchived,
resolveActionableMentionWhisper,
introSelected,
activePolicy,
report,
originalReport,
]);
Expand Down
1 change: 1 addition & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ describe('actions/IOU', () => {
CONST.IOU.ACTION.CATEGORIZE,
reportActionableTrackExpense?.reportActionID,
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
undefined,
);
await waitForBatchedUpdates();

Expand Down
Loading
Loading