diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3a5cbf37ce93..4a38e1f746d9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -8290,6 +8290,13 @@ function getTaskAssigneeChatOnyxData( }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${assigneeChatReportID}`, + value: { + isOptimisticReport: true, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`, @@ -8316,6 +8323,13 @@ function getTaskAssigneeChatOnyxData( isOptimisticReport: false, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${assigneeChatReportID}`, + value: { + isOptimisticReport: false, + }, + }, ); failureData.push( diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0373933f3b10..c9cc3dd391c7 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1274,8 +1274,35 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionThreadReport?.reportID}`, + value: { + isOptimisticReport: true, + }, + }, ); + if (isNewChatReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chat.report?.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } + + if (shouldCreateNewMoneyRequestReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iou.report?.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } + if (!isEmptyObject(transactionThreadCreatedReportAction)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -1703,6 +1730,13 @@ function buildOnyxDataForInvoice(invoiceParams: BuildOnyxDataForInvoiceParams): }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iou.report?.reportID}`, + value: { + isOptimisticReport: true, + }, + }, { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionParams.transaction.transactionID}`, @@ -1737,6 +1771,13 @@ function buildOnyxDataForInvoice(invoiceParams: BuildOnyxDataForInvoiceParams): key: `${ONYXKEYS.COLLECTION.REPORT}${transactionParams.threadReport.reportID}`, value: transactionParams.threadReport, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionParams.threadReport?.reportID}`, + value: { + isOptimisticReport: true, + }, + }, ]; if (transactionParams.threadCreatedReportAction?.reportActionID) { @@ -1763,6 +1804,16 @@ function buildOnyxDataForInvoice(invoiceParams: BuildOnyxDataForInvoiceParams): ...(chat.isNewReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}), }, }); + + if (chat.isNewReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chat.report?.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } } if (optimisticDataParams.policyRecentlyUsedCategories.length) { @@ -2207,6 +2258,15 @@ function buildOnyxDataForTrackExpense({ }, }, ); + if (shouldCreateNewMoneyRequestReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iouReport.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } } else { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -2231,6 +2291,13 @@ function buildOnyxDataForTrackExpense({ pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionThreadReport?.reportID}`, + value: { + isOptimisticReport: true, + }, + }, ); if (!isEmptyObject(transactionThreadCreatedReportAction)) { @@ -2279,6 +2346,15 @@ function buildOnyxDataForTrackExpense({ }, }, ); + if (shouldCreateNewMoneyRequestReport) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iouReport.reportID}`, + value: { + isOptimisticReport: false, + }, + }); + } } else { successData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -5503,6 +5579,17 @@ function createSplitsAndOnyxData({ value: splitTransaction, }, ]; + + if (!existingSplitChatReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${splitChatReport.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5519,6 +5606,16 @@ function createSplitsAndOnyxData({ }, ]; + if (!existingSplitChatReport) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${splitChatReport.reportID}`, + value: { + isOptimisticReport: false, + }, + }); + } + const redundantParticipants: Record = {}; if (!existingSplitChatReport) { successData.push({ @@ -6089,6 +6186,16 @@ function startSplitBill({ }, ]; + if (!existingSplitChatReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${splitChatReport.reportID}`, + value: { + isOptimisticReport: true, + }, + }); + } + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -6105,6 +6212,16 @@ function startSplitBill({ }, ]; + if (!existingSplitChatReport) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${splitChatReport.reportID}`, + value: { + isOptimisticReport: false, + }, + }); + } + const redundantParticipants: Record = {}; if (!existingSplitChatReport) { successData.push({ @@ -7647,6 +7764,23 @@ function getSendMoneyParams( } : undefined; + const optimisticMetaData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chatReport.reportID}`, + value: { + isOptimisticReport: true, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${optimisticTransactionThread.reportID}`, + value: { + isOptimisticReport: true, + }, + }, + ]; + const successData: OnyxUpdate[] = []; // Add optimistic personal details for recipient @@ -7831,6 +7965,7 @@ function getSendMoneyParams( optimisticIOUReportActionsData, optimisticTransactionData, optimisticTransactionThreadData, + ...optimisticMetaData, ]; if (optimisticTransactionThreadReportActionsData) { diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index ecd0fce734f9..6f59a315202f 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -2832,6 +2832,17 @@ function navigateToConciergeChatAndDeleteReport(reportID: string | undefined, sh }); } +function clearCreateChatError(report: OnyxEntry) { + const metaData = getReportMetadata(report?.reportID); + const isOptimisticReport = metaData?.isOptimisticReport; + if (report?.errorFields?.createChat && !isOptimisticReport) { + clearReportFieldKeyErrors(report.reportID, 'createChat'); + return; + } + + navigateToConciergeChatAndDeleteReport(report?.reportID, undefined, true); +} + /** * @param policyRoomReport The policy room report * @param policyRoomName The updated name for the policy room @@ -4390,6 +4401,13 @@ function prepareOnboardingOnyxData( }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReport.reportID}`, + value: { + isOptimisticReport: true, + }, + }, { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, @@ -4409,6 +4427,13 @@ function prepareOnboardingOnyxData( }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReport.reportID}`, + value: { + isOptimisticReport: false, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, @@ -5678,6 +5703,7 @@ export { navigateToAndOpenReportWithAccountIDs, navigateToConciergeChat, navigateToConciergeChatAndDeleteReport, + clearCreateChatError, notifyNewAction, openLastOpenedPublicRoom, openReport, diff --git a/src/pages/home/report/ReportActionItemCreated.tsx b/src/pages/home/report/ReportActionItemCreated.tsx index b5584c00087b..d55c04359589 100644 --- a/src/pages/home/report/ReportActionItemCreated.tsx +++ b/src/pages/home/report/ReportActionItemCreated.tsx @@ -10,7 +10,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; import {getIcons, isChatReport, isCurrentUserInvoiceReceiver, isInvoiceRoom, navigateToDetailsPage, shouldDisableDetailPage as shouldDisableDetailPageReportUtils} from '@libs/ReportUtils'; -import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report'; +import {clearCreateChatError} from '@userActions/Report'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import AnimatedEmptyStateBackground from './AnimatedEmptyStateBackground'; @@ -49,7 +49,7 @@ function ReportActionItemCreated({reportID, policyID}: ReportActionItemCreatedPr pendingAction={report?.pendingFields?.addWorkspaceRoom ?? report?.pendingFields?.createChat} errors={report?.errorFields?.addWorkspaceRoom ?? report?.errorFields?.createChat} errorRowStyles={[styles.ml10, styles.mr2]} - onClose={() => navigateToConciergeChatAndDeleteReport(report?.reportID ?? reportID, undefined, true)} + onClose={() => clearCreateChatError(report)} > diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 6af7a1f019b3..ba36a1b499a8 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -167,6 +167,36 @@ describe('actions/Report', () => { }); }); + it('clearCreateChatError should not delete the report if it is not optimistic report', () => { + const REPORT: OnyxTypes.Report = {...createRandomReport(1), errorFields: {createChat: {error: 'error'}}}; + const REPORT_METADATA: OnyxTypes.ReportMetadata = {isOptimisticReport: false}; + + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT.reportID}`, REPORT); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${REPORT.reportID}`, REPORT_METADATA); + + return waitForBatchedUpdates() + .then(() => { + Report.clearCreateChatError(REPORT); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT.reportID}`, + callback: (report) => { + Onyx.disconnect(connection); + resolve(); + + // The report should exist but the create chat error field should be cleared. + expect(report?.reportID).toBeDefined(); + expect(report?.errorFields?.createChat).toBeUndefined(); + }, + }); + }), + ); + }); + it('should update pins in Onyx when togglePinned is called', () => { const TEST_USER_ACCOUNT_ID = 1; const TEST_USER_LOGIN = 'test@test.com';