Skip to content

Commit

Permalink
Merge pull request #44923 from bernhardoj/fix/43481-delete-report-whe…
Browse files Browse the repository at this point in the history
…n-error

Delete all linked report when clearing optimistic chat and transaction error
  • Loading branch information
arosiclair authored Jul 15, 2024
2 parents f1b9f5e + 1e6c518 commit 5710b69
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import * as Link from '@userActions/Link';
import * as Transaction from '@userActions/Transaction';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import * as Report from '@src/libs/actions/Report';
import * as ReportActions from '@src/libs/actions/ReportActions';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -115,6 +116,9 @@ function MoneyRequestView({
const {isOffline} = useNetwork();
const {translate, toLocaleDigit} = useLocalize();
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.parentReportID}`, {
selector: (chatReportValue) => chatReportValue && {reportID: chatReportValue.reportID, errorFields: chatReportValue.errorFields},
});

const parentReportAction = parentReportActions?.[report.parentReportActionID ?? '-1'];
const isTrackExpense = ReportUtils.isTrackExpenseReport(report);
Expand Down Expand Up @@ -403,11 +407,14 @@ function MoneyRequestView({
if (!transaction?.transactionID) {
return;
}
if (
transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD &&
Object.values(transaction?.errors ?? {})?.find((error) => ErrorUtils.isReceiptError(error))
) {
deleteTransaction(parentReport, parentReportAction);
if (transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
if (chatReport?.reportID && ReportUtils.getAddWorkspaceRoomOrChatReportErrors(chatReport)) {
Report.navigateToConciergeChatAndDeleteReport(chatReport.reportID, true, true);
return;
}
if (Object.values(transaction?.errors ?? {})?.find((error) => ErrorUtils.isReceiptError(error))) {
deleteTransaction(parentReport, parentReportAction);
}
}
Transaction.clearError(transaction.transactionID);
ReportActions.clearAllRelatedReportActionErrors(report.reportID, parentReportAction);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ function getAllReportTransactions(reportID?: string, transactions?: OnyxCollecti
// `reportID` from the `/CreateDistanceRequest` endpoint return's number instead of string for created `transaction`.
// For reference, https://github.com/Expensify/App/pull/26536#issuecomment-1703573277.
// We will update this in a follow-up Issue. According to this comment: https://github.com/Expensify/App/pull/26536#issuecomment-1703591019.
const nonNullableTransactions: Transaction[] = Object.values(transactions ?? allTransactions ?? {}).filter((transaction): transaction is Transaction => transaction !== null);
const nonNullableTransactions: Transaction[] = Object.values(transactions ?? allTransactions ?? {}).filter((transaction): transaction is Transaction => !!transaction);
return nonNullableTransactions.filter((transaction) => `${transaction.reportID}` === `${reportID}`);
}

Expand Down
22 changes: 17 additions & 5 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,7 @@ function addPolicyReport(policyReport: ReportUtils.OptimisticChatReport) {
}

/** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */
function deleteReport(reportID: string) {
function deleteReport(reportID: string, shouldDeleteChildReports = false) {
const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const onyxData: Record<string, null> = {
[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]: null,
Expand All @@ -2165,20 +2165,32 @@ function deleteReport(reportID: string) {

Onyx.multiSet(onyxData);

if (shouldDeleteChildReports) {
Object.values(reportActionsForReport ?? {}).forEach((reportAction) => {
if (!reportAction.childReportID) {
return;
}
deleteReport(reportAction.childReportID, shouldDeleteChildReports);
});
}

// Delete linked IOU report
if (report?.iouReportID) {
deleteReport(report.iouReportID);
deleteReport(report.iouReportID, shouldDeleteChildReports);
}
}

/**
* @param reportID The reportID of the policy report (workspace room)
*/
function navigateToConciergeChatAndDeleteReport(reportID: string) {
function navigateToConciergeChatAndDeleteReport(reportID: string, shouldPopToTop = false, shouldDeleteChildReports = false) {
// Dismiss the current report screen and replace it with Concierge Chat
Navigation.goBack();
if (shouldPopToTop) {
Navigation.setShouldPopAllStateOnUP(true);
}
Navigation.goBack(undefined, undefined, shouldPopToTop);
navigateToConciergeChat();
deleteReport(reportID);
deleteReport(reportID, shouldDeleteChildReports);
}

/**
Expand Down

0 comments on commit 5710b69

Please sign in to comment.