Skip to content

Commit b54f94e

Browse files
authored
Merge pull request Expensify#82864 from Expensify/tgolen-refactorOnyxConnection-reportactions-2and3c
[PR 5 of 16] Remove global Onyx reference from getOriginalReportID
2 parents bbf6b35 + a4060a6 commit b54f94e

5 files changed

Lines changed: 43 additions & 17 deletions

File tree

src/components/ReportActionItem/MoneyRequestReceiptView.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import ROUTES from '@src/ROUTES';
5353
import type * as OnyxTypes from '@src/types/onyx';
5454
import type {TransactionPendingFieldsKey} from '@src/types/onyx/Transaction';
5555
import {isEmptyObject} from '@src/types/utils/EmptyObject';
56+
import useOriginalReportID from '@hooks/useOriginalReportID';
5657
import useCardFeedErrors from '@hooks/useCardFeedErrors';
5758
import {getBrokenConnectionUrlToFixPersonalCard} from '@libs/CardUtils';
5859
import ReportActionItemImage from './ReportActionItemImage';
@@ -117,6 +118,7 @@ function MoneyRequestReceiptView({
117118

118119
const [isLoading, setIsLoading] = useState(true);
119120
const parentReportAction = report?.parentReportActionID ? parentReportActions?.[report.parentReportActionID] : undefined;
121+
const originalReportID = useOriginalReportID(report?.reportID, parentReportAction);
120122
const {iouReport, chatReport: chatIOUReport, isChatIOUReportArchived} = useGetIOUReportFromReportAction(parentReportAction);
121123
const isTrackExpense = !mergeTransactionID && isTrackExpenseReportNew(report, parentReport, parentReportAction);
122124
const moneyRequestReport = parentReport;
@@ -279,24 +281,34 @@ function MoneyRequestReceiptView({
279281
return;
280282
}
281283
if (parentReportAction) {
282-
cleanUpMoneyRequest(transaction?.transactionID ?? linkedTransactionID, parentReportAction, report.reportID, iouReport, chatIOUReport, isChatIOUReportArchived, true);
284+
cleanUpMoneyRequest(
285+
transaction?.transactionID ?? linkedTransactionID,
286+
parentReportAction,
287+
report.reportID,
288+
iouReport,
289+
chatIOUReport,
290+
isChatIOUReportArchived,
291+
originalReportID,
292+
true,
293+
);
283294
return;
284295
}
285296
}
297+
286298
if (!transaction?.transactionID) {
287299
if (!linkedTransactionID) {
288300
return;
289301
}
290302
clearError(linkedTransactionID);
291-
clearAllRelatedReportActionErrors(report.reportID, parentReportAction);
303+
clearAllRelatedReportActionErrors(report.reportID, parentReportAction, originalReportID);
292304
return;
293305
}
294306
if (!isEmptyObject(transactionAndReportActionErrors)) {
295307
revert(transaction, getLastModifiedExpense(report?.reportID));
296308
}
297309
if (!isEmptyObject(errorsWithoutReportCreation)) {
298310
clearError(transaction.transactionID);
299-
clearAllRelatedReportActionErrors(report.reportID, parentReportAction);
311+
clearAllRelatedReportActionErrors(report.reportID, parentReportAction, originalReportID);
300312
}
301313
if (!isEmptyObject(reportCreationError)) {
302314
if (isInNarrowPaneModal) {

src/libs/actions/IOU/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8862,6 +8862,7 @@ function cleanUpMoneyRequest(
88628862
iouReport: OnyxEntry<OnyxTypes.Report>,
88638863
chatReport: OnyxEntry<OnyxTypes.Report>,
88648864
isChatIOUReportArchived: boolean | undefined,
8865+
originalReportID: string | undefined,
88658866
isSingleTransactionView = false,
88668867
) {
88678868
const {shouldDeleteTransactionThread, shouldDeleteIOUReport, updatedReportAction, updatedIOUReport, updatedReportPreviewAction, transactionThreadID, reportPreviewAction} =
@@ -9019,7 +9020,7 @@ function cleanUpMoneyRequest(
90199020
}
90209021

90219022
if (!shouldDeleteIOUReport) {
9022-
clearAllRelatedReportActionErrors(reportID, reportAction);
9023+
clearAllRelatedReportActionErrors(reportID, reportAction, originalReportID);
90239024
}
90249025

90259026
// First, update the reportActions to ensure related actions are not displayed.
@@ -9028,7 +9029,7 @@ function cleanUpMoneyRequest(
90289029
// eslint-disable-next-line @typescript-eslint/no-deprecated
90299030
InteractionManager.runAfterInteractions(() => {
90309031
if (shouldDeleteIOUReport) {
9031-
clearAllRelatedReportActionErrors(reportID, reportAction);
9032+
clearAllRelatedReportActionErrors(reportID, reportAction, originalReportID);
90329033
}
90339034
// After navigation, update the remaining data.
90349035
Onyx.update(onyxUpdates);

src/libs/actions/ReportActions.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ Onyx.connect({
2626
},
2727
});
2828

29-
function clearReportActionErrors(reportID: string, reportAction: ReportAction, keys?: string[]) {
30-
const originalReportID = getOriginalReportID(reportID, reportAction, undefined);
31-
29+
function clearReportActionErrors(reportID: string, reportAction: ReportAction, originalReportID: string | undefined, keys?: string[]) {
3230
if (!reportAction?.reportActionID) {
3331
return;
3432
}
@@ -80,27 +78,36 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k
8078
ignore: `undefined` means we want to check both parent and children report actions
8179
ignore: `parent` or `child` means we want to ignore checking parent or child report actions because they've been previously checked
8280
*/
83-
function clearAllRelatedReportActionErrors(reportID: string | undefined, reportAction: ReportAction | null | undefined, ignore?: IgnoreDirection, keys?: string[]) {
81+
function clearAllRelatedReportActionErrors(
82+
reportID: string | undefined,
83+
reportAction: ReportAction | null | undefined,
84+
originalReportID: string | undefined,
85+
ignore?: IgnoreDirection,
86+
keys?: string[],
87+
) {
8488
const errorKeys = keys ?? Object.keys(reportAction?.errors ?? {});
8589
if (!reportAction || errorKeys.length === 0 || !reportID) {
8690
return;
8791
}
8892

89-
clearReportActionErrors(reportID, reportAction, keys);
93+
clearReportActionErrors(reportID, reportAction, originalReportID, keys);
9094

9195
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
9296
if (report?.parentReportID && report?.parentReportActionID && ignore !== 'parent') {
9397
const parentReportAction = getReportAction(report.parentReportID, report.parentReportActionID);
9498
const parentErrorKeys = Object.keys(parentReportAction?.errors ?? {}).filter((err) => errorKeys.includes(err));
99+
const parentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`] ?? {};
100+
const parentOriginalReportID = getOriginalReportID(report.parentReportID, parentReportAction, parentReportActions);
95101

96-
clearAllRelatedReportActionErrors(report.parentReportID, parentReportAction, 'child', parentErrorKeys);
102+
clearAllRelatedReportActionErrors(report.parentReportID, parentReportAction, parentOriginalReportID, 'child', parentErrorKeys);
97103
}
98104

99105
if (reportAction.childReportID && ignore !== 'child') {
100106
const childActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportAction.childReportID}`] ?? {};
101107
for (const action of Object.values(childActions)) {
102108
const childErrorKeys = Object.keys(action.errors ?? {}).filter((err) => errorKeys.includes(err));
103-
clearAllRelatedReportActionErrors(reportAction.childReportID, action, 'parent', childErrorKeys);
109+
const childOriginalReportID = getOriginalReportID(reportAction.childReportID, action, childActions);
110+
clearAllRelatedReportActionErrors(reportAction.childReportID, action, childOriginalReportID, 'parent', childErrorKeys);
104111
}
105112
}
106113
}

src/pages/inbox/report/PureReportActionItem.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,13 @@ type PureReportActionItemProps = {
442442
clearError?: (transactionID: string) => void;
443443

444444
/** Function to clear all errors from a report action */
445-
clearAllRelatedReportActionErrors?: (reportID: string | undefined, reportAction: OnyxTypes.ReportAction | null | undefined, ignore?: IgnoreDirection, keys?: string[]) => void;
445+
clearAllRelatedReportActionErrors?: (
446+
reportID: string | undefined,
447+
reportAction: OnyxTypes.ReportAction | null | undefined,
448+
originalReportID: string | undefined,
449+
ignore?: IgnoreDirection,
450+
keys?: string[],
451+
) => void;
446452

447453
/** Function to dismiss the actionable whisper for tracking expenses */
448454
dismissTrackExpenseActionableWhisper?: (reportID: string | undefined, reportAction: OnyxEntry<OnyxTypes.ReportAction>) => void;
@@ -621,14 +627,14 @@ function PureReportActionItem({
621627
const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : undefined;
622628
if (isSendingMoney && transactionID && reportID) {
623629
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.chatReportID}`];
624-
cleanUpMoneyRequest(transactionID, action, reportID, report, chatReport, undefined, true);
630+
cleanUpMoneyRequest(transactionID, action, reportID, report, chatReport, undefined, originalReportID, true);
625631
return;
626632
}
627633
if (transactionID) {
628634
clearError(transactionID);
629635
}
630-
clearAllRelatedReportActionErrors(reportID, action);
631-
}, [action, isSendingMoney, clearAllRelatedReportActionErrors, reportID, allReports, report, clearError]);
636+
clearAllRelatedReportActionErrors(reportID, action, originalReportID);
637+
}, [action, isSendingMoney, clearAllRelatedReportActionErrors, reportID, allReports, report, clearError, originalReportID]);
632638

633639
const showDismissReceiptErrorModal = useCallback(async () => {
634640
const result = await showConfirmModal({

tests/actions/IOUTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,7 @@ describe('actions/IOU', () => {
25522552
() =>
25532553
new Promise<void>((resolve) => {
25542554
if (iouReportID) {
2555-
clearAllRelatedReportActionErrors(iouReportID, iouAction ?? null);
2555+
clearAllRelatedReportActionErrors(iouReportID, iouAction ?? null, iouReportID);
25562556
}
25572557
resolve();
25582558
}),

0 commit comments

Comments
 (0)