Skip to content
Open
5 changes: 5 additions & 0 deletions src/components/MoneyRequestHeaderSecondaryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useLocalize from '@hooks/useLocalize';
import useMoneyRequestPolicyTagsForReport from '@hooks/useMoneyRequestPolicyTagsForReport';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useParticipantsPolicyTags from '@hooks/useParticipantsPolicyTags';
import usePermissions from '@hooks/usePermissions';
import usePersonalPolicy from '@hooks/usePersonalPolicy';
import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses';
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
isPerDiemRequest,
} from '@libs/TransactionUtils';

import {getMoneyRequestParticipantsFromReport} from '@userActions/IOU/MoneyRequest';
import {dismissRejectUseExplanation} from '@userActions/IOU/RejectMoneyRequest';
import {setDeleteTransactionNavigateBackUrl} from '@userActions/Report';

Expand Down Expand Up @@ -224,6 +226,8 @@ function MoneyRequestHeaderSecondaryActions({reportID, onBackButtonPress}: Money
const targetPolicyTags = defaultPolicyTags ?? {};

const policyTagList = useMoneyRequestPolicyTagsForReport({report: activePolicyExpenseChat, currentUserAccountID: accountID});
const participants = getMoneyRequestParticipantsFromReport(activePolicyExpenseChat, accountID);
const participantsPolicyTags = useParticipantsPolicyTags(participants);

// Duplicate action throttle
const handleDuplicateReset = () => {
Expand Down Expand Up @@ -271,6 +275,7 @@ function MoneyRequestHeaderSecondaryActions({reportID, onBackButtonPress}: Money
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
});
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useBulkDuplicateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {bulkDuplicateExpenses} from '@libs/actions/IOU/Duplicate';
import {isTrackOnboardingChoice} from '@libs/OnboardingUtils';
import {getPolicyExpenseChat} from '@libs/ReportUtils';

import {getMoneyRequestParticipantsFromReport} from '@userActions/IOU/MoneyRequest';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report, Transaction} from '@src/types/onyx';
Expand All @@ -19,6 +21,7 @@ import useDelegateAccountID from './useDelegateAccountID';
import useLocalize from './useLocalize';
import useMoneyRequestPolicyTagsForReport from './useMoneyRequestPolicyTagsForReport';
import useOnyx from './useOnyx';
import useParticipantsPolicyTags from './useParticipantsPolicyTags';
import usePermissions from './usePermissions';

type UseBulkDuplicateActionParams = {
Expand Down Expand Up @@ -68,6 +71,8 @@ function useBulkDuplicateAction({selectedTransactionsKeys, allTransactions, allR

const activePolicyExpenseChat = getPolicyExpenseChat(accountID, defaultExpensePolicy?.id);
const policyTagList = useMoneyRequestPolicyTagsForReport({report: activePolicyExpenseChat, currentUserAccountID: accountID});
const participants = getMoneyRequestParticipantsFromReport(activePolicyExpenseChat, accountID);
const participantsPolicyTags = useParticipantsPolicyTags(participants);

const handleDuplicate = () => {
bulkDuplicateExpenses({
Expand All @@ -93,6 +98,7 @@ function useBulkDuplicateAction({selectedTransactionsKeys, allTransactions, allR
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
});

if (onAfterDuplicate) {
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useExpenseActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
} from '@libs/TransactionUtils';

import {getNavigationUrlOnMoneyRequestDelete} from '@userActions/IOU/DeleteMoneyRequest';
import {startMoneyRequest} from '@userActions/IOU/MoneyRequest';
import {getMoneyRequestParticipantsFromReport, startMoneyRequest} from '@userActions/IOU/MoneyRequest';
import {setDeleteTransactionNavigateBackUrl} from '@userActions/Report';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -71,6 +71,7 @@ import useLocalize from './useLocalize';
import useMoneyRequestPolicyTagsForReport from './useMoneyRequestPolicyTagsForReport';
import useOnyx from './useOnyx';
import useParentReportAction from './useParentReportAction';
import useParticipantsPolicyTags, {getPolicyTagsSelector} from './useParticipantsPolicyTags';
import usePermissions from './usePermissions';
import usePersonalPolicy from './usePersonalPolicy';
import useReportIsArchived from './useReportIsArchived';
Expand Down Expand Up @@ -243,6 +244,8 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
const targetPolicyTags = defaultExpensePolicy ? (allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${defaultExpensePolicy.id}`] ?? {}) : {};

const policyTagList = useMoneyRequestPolicyTagsForReport({report: activePolicyExpenseChat, currentUserAccountID: accountID});
const participants = getMoneyRequestParticipantsFromReport(activePolicyExpenseChat, accountID);
const participantsPolicyTags = useParticipantsPolicyTags(participants);

const duplicateExpenseTransaction = (transactionList: OnyxTypes.Transaction[]) => {
if (!transactionList.length) {
Expand Down Expand Up @@ -280,6 +283,7 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
});
}
};
Expand Down Expand Up @@ -417,6 +421,8 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic

const targetChatForDuplicate = isSourcePolicyValid ? chatReport : activePolicyExpenseChat;
const activePolicyCategories = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${targetPolicyForDuplicate?.id}`] ?? {};
const reportDuplicateParticipants = getMoneyRequestParticipantsFromReport(targetChatForDuplicate, currentUserPersonalDetails?.accountID);
const reportDuplicateParticipantsPolicyTags = getPolicyTagsSelector(reportDuplicateParticipants)(allPolicyTags);

duplicateReportAction({
sourceReport: moneyRequestReport,
Expand All @@ -441,6 +447,7 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
isTrackIntentUser,
delegateAccountID,
formatPhoneNumber,
participantsPolicyTags: reportDuplicateParticipantsPolicyTags,
});
},
},
Expand Down
63 changes: 63 additions & 0 deletions src/hooks/useMoneyRequestParticipantsPolicyTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Resolves the participants for a money request and derives the policy tags for each participant's policy.
* Combines `getMoneyRequestParticipantOptions` with `useParticipantsPolicyTags` so distance-request call sites can
* read reactive participant policy tags from Onyx instead of the deprecated `buildParticipantsPolicyTags` helper.
*/
import type {LocaleContextProps} from '@components/LocaleContextProvider';
Comment thread
jakubstec marked this conversation as resolved.

import {getMoneyRequestParticipantOptions} from '@libs/actions/IOU/MoneyRequest';
import type {OptionData} from '@libs/ReportUtils';

import type {ParticipantsPolicyTags, PersonalDetailsList, Policy, Report} from '@src/types/onyx';
import type {ReportAttributesDerivedValue} from '@src/types/onyx/DerivedValues';
import type {Participant} from '@src/types/onyx/IOU';

import type {OnyxEntry} from 'react-native-onyx';

import useParticipantsPolicyTags from './useParticipantsPolicyTags';

type UseMoneyRequestParticipantsPolicyTagsParams = {
currentUserAccountID: number;
report: OnyxEntry<Report>;
policy: OnyxEntry<Policy>;
personalDetails: OnyxEntry<PersonalDetailsList>;
conciergeReportID: string | undefined;
isArchived: boolean | undefined;
reportAttributesDerived: ReportAttributesDerivedValue['reports'] | undefined;
reportDraft: OnyxEntry<Report> | undefined;
translate: LocaleContextProps['translate'];
};

type UseMoneyRequestParticipantsPolicyTagsResult = {
participants: Array<Participant | OptionData>;
participantsPolicyTags: ParticipantsPolicyTags;
};

function useMoneyRequestParticipantsPolicyTags({
currentUserAccountID,
report,
policy,
personalDetails,
conciergeReportID,
isArchived,
reportAttributesDerived,
reportDraft,
translate,
}: UseMoneyRequestParticipantsPolicyTagsParams): UseMoneyRequestParticipantsPolicyTagsResult {
const participants = getMoneyRequestParticipantOptions(
currentUserAccountID,
report,
policy,
personalDetails,
conciergeReportID,
isArchived,
reportAttributesDerived,
reportDraft,
translate,
);
const participantsPolicyTags = useParticipantsPolicyTags(participants);

return {participants, participantsPolicyTags};
}

export default useMoneyRequestParticipantsPolicyTags;
1 change: 1 addition & 0 deletions src/hooks/useParticipantsPolicyTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ function useParticipantsPolicyTags(participants: ParticipantWithPolicyID[]): Par
}

export default useParticipantsPolicyTags;
export {getPolicyTagsSelector};
23 changes: 19 additions & 4 deletions src/libs/actions/IOU/Duplicate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {LocaleContextProps, LocalizedTranslate} from '@components/LocaleContextProvider';
import type {SelectedReports} from '@components/Search/types';

import {getPolicyTagsSelector} from '@hooks/useParticipantsPolicyTags';

import * as API from '@libs/API';
import type {MergeDuplicatesParams, ResolveDuplicatesParams} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
Expand Down Expand Up @@ -59,7 +61,7 @@ import type {PerDiemExpenseInformation} from './PerDiem';
import type {CreateDistanceRequestInformation} from './Split';
import type {CreateTrackExpenseParams} from './TrackExpense';

import {buildParticipantsPolicyTags, getAllReports, getAllTransactions} from '.';
import {getAllReports, getAllTransactions} from '.';
import {getCleanUpTransactionThreadReportOnyxData} from './DeleteMoneyRequest';
import {getMoneyRequestParticipantsFromReport} from './MoneyRequest';
import {submitPerDiemExpense} from './PerDiem';
Expand Down Expand Up @@ -662,6 +664,7 @@ function createExpenseByType({
recentWaypoints,
isTrackIntentUser,
formatPhoneNumber,
participantsPolicyTags,
}: {
transactionType: string;
params: RequestMoneyInformation;
Expand All @@ -676,6 +679,7 @@ function createExpenseByType({
recentWaypoints: OnyxEntry<OnyxTypes.RecentWaypoint[]>;
isTrackIntentUser: boolean | undefined;
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
participantsPolicyTags: OnyxTypes.ParticipantsPolicyTags;
}) {
switch (transactionType) {
case CONST.SEARCH.TRANSACTION_TYPE.DISTANCE: {
Expand Down Expand Up @@ -713,9 +717,7 @@ function createExpenseByType({
personalDetails,
recentWaypoints,
formatPhoneNumber,
// buildParticipantsPolicyTags is deprecated but still needed here until this call site is migrated to useOnyx (https://github.com/Expensify/App/issues/72721)
// eslint-disable-next-line @typescript-eslint/no-deprecated
participantsPolicyTags: buildParticipantsPolicyTags(participants),
participantsPolicyTags,
};
return createDistanceRequest(distanceParams);
}
Expand Down Expand Up @@ -769,6 +771,7 @@ type DuplicateExpenseTransactionParams = {
delegateAccountID: number | undefined;
policyTagList: OnyxTypes.PolicyTagLists;
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
participantsPolicyTags: OnyxTypes.ParticipantsPolicyTags;
};

function duplicateExpenseTransaction({
Expand Down Expand Up @@ -798,6 +801,7 @@ function duplicateExpenseTransaction({
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
}: DuplicateExpenseTransactionParams) {
if (!transaction) {
return;
Expand Down Expand Up @@ -912,6 +916,7 @@ function duplicateExpenseTransaction({
recentWaypoints,
isTrackIntentUser,
formatPhoneNumber,
participantsPolicyTags,
});
}

Expand Down Expand Up @@ -939,6 +944,7 @@ type DuplicateReportParams = {
isTrackIntentUser: boolean | undefined;
delegateAccountID: number | undefined;
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
participantsPolicyTags: OnyxTypes.ParticipantsPolicyTags;
};

function duplicateReport({
Expand All @@ -965,6 +971,7 @@ function duplicateReport({
isTrackIntentUser,
delegateAccountID,
formatPhoneNumber,
participantsPolicyTags,
}: DuplicateReportParams) {
if (!targetPolicy || !parentChatReport) {
return;
Expand Down Expand Up @@ -1075,6 +1082,7 @@ function duplicateReport({
recentWaypoints,
isTrackIntentUser,
formatPhoneNumber,
participantsPolicyTags,
});

if (result?.iouReport) {
Expand Down Expand Up @@ -1110,6 +1118,7 @@ type BulkDuplicateExpensesParams = {
delegateAccountID: number | undefined;
policyTagList: OnyxTypes.PolicyTagLists;
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
participantsPolicyTags: OnyxTypes.ParticipantsPolicyTags;
};

function bulkDuplicateExpenses({
Expand All @@ -1135,6 +1144,7 @@ function bulkDuplicateExpenses({
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
}: BulkDuplicateExpensesParams) {
const transactionsToDuplicate = transactionIDs.map((id) => allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]).filter((t): t is OnyxTypes.Transaction => !!t);

Expand Down Expand Up @@ -1234,6 +1244,7 @@ function bulkDuplicateExpenses({
delegateAccountID,
policyTagList,
formatPhoneNumber,
participantsPolicyTags,
});

if (result?.iouReport) {
Expand Down Expand Up @@ -1351,6 +1362,9 @@ function bulkDuplicateReports({
const targetPolicyCategories = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${targetPolicy?.id}`] ?? {};
const targetPolicyTags = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${targetPolicy?.id}`] ?? {};

const participants = getMoneyRequestParticipantsFromReport(parentChatReport, currentUserAccountID);
const participantsPolicyTags = getPolicyTagsSelector(participants)(allPolicyTags);

duplicateReport({
sourceReport: report,
sourceReportTransactions: reportTransactions,
Expand All @@ -1375,6 +1389,7 @@ function bulkDuplicateReports({
isTrackIntentUser,
delegateAccountID,
formatPhoneNumber,
participantsPolicyTags,
});
}

Expand Down
23 changes: 1 addition & 22 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Attendee, Participant} from '@src/types/onyx/IOU';
import type {Attendee} from '@src/types/onyx/IOU';

import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';

Expand Down Expand Up @@ -201,24 +201,6 @@ function getSearchQueryByHash(): Record<string, string> {
function getPolicyTags(): OnyxCollection<OnyxTypes.PolicyTagLists> {
return allPolicyTags;
}

/**
* @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access.
* TODO: remove `buildParticipantsPolicyTags` from this file (https://github.com/Expensify/App/issues/72721)
* All usages of this function should be replaced with params passed to the functions or useOnyx hook in React components.
*/
function buildParticipantsPolicyTags(participants: Participant[]): OnyxTypes.ParticipantsPolicyTags {
return participants.reduce<OnyxTypes.ParticipantsPolicyTags>((acc, participant) => {
if (participant.policyID) {
const tags = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${participant.policyID}`];
if (tags) {
acc[participant.policyID] = tags;
}
}
return acc;
}, {});
}

export {
getAllPersonalDetails,
getAllTransactions,
Expand All @@ -232,8 +214,5 @@ export {
getRecentAttendees,
getAllSnapshots,
getSearchQueryByHash,
// TODO: Replace buildParticipantsPolicyTags (https://github.com/Expensify/App/issues/72721) with useOnyx hook
// eslint-disable-next-line @typescript-eslint/no-deprecated
buildParticipantsPolicyTags,
getPolicyTags,
};
Loading
Loading