Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all linked report when clearing optimistic chat and transaction error #44923

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
15 changes: 10 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 @@ -404,11 +405,15 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let us do an early return here to keep possible regressions at bay. Also, I think doing any further steps does not make sense when the chat report and all related reports are deleted. Something like this can be done within the if condition here. What do you think?

if (ReportUtils.getAddWorkspaceRoomOrChatReportErrors(chatReportID)) {
   Report.navigateToConciergeChatAndDeleteReport(chatReportID, true);
   return;
}
if (Object.values(transaction?.errors ?? {})?.find((error) => ErrorUtils.isReceiptError(error))) {
   deleteTransaction(parentReport, parentReportAction);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, updated!

const chatReportID = parentReport?.parentReportID ?? '';
if (ReportUtils.getAddWorkspaceRoomOrChatReportErrors(chatReportID)) {
Report.navigateToConciergeChatAndDeleteReport(chatReportID, 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
3 changes: 2 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6113,7 +6113,8 @@ function isValidReportIDFromPath(reportIDFromPath: string): boolean {
/**
* Return the errors we have when creating a chat or a workspace room
*/
function getAddWorkspaceRoomOrChatReportErrors(report: OnyxEntry<Report>): Errors | null | undefined {
function getAddWorkspaceRoomOrChatReportErrors(reportOrID: OnyxEntry<Report> | string): Errors | null | undefined {
const report = typeof reportOrID === 'string' ? ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] : reportOrID;
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't fetch the report in the lib here if we don't need to. Can we just get the report in the MoneyRequestView with useOnyx and pass it in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// We are either adding a workspace room, or we're creating a chat, it isn't possible for both of these to have errors for the same report at the same time, so
// simply looking up the first truthy value will get the relevant property if it's set.
return report?.errorFields?.addWorkspaceRoom ?? report?.errorFields?.createChat;
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
14 changes: 12 additions & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,13 @@ function deleteReport(reportID: string) {

Onyx.multiSet(onyxData);

Object.values(reportActionsForReport ?? {}).forEach((reportAction) => {
if (!reportAction.childReportID) {
return;
}
deleteReport(reportAction.childReportID);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably not always delete child reports. Can we put this behind a shouldDeleteChildReports flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

});

// Delete linked IOU report
if (report?.iouReportID) {
deleteReport(report.iouReportID);
Expand All @@ -2177,9 +2184,12 @@ function deleteReport(reportID: string) {
/**
* @param reportID The reportID of the policy report (workspace room)
*/
function navigateToConciergeChatAndDeleteReport(reportID: string) {
function navigateToConciergeChatAndDeleteReport(reportID: string, shouldPopToTop?: boolean) {
// 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);
}
Expand Down
Loading