diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c24df5ad7d5d..c22b0d566be6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1032,14 +1032,21 @@ function createDraftTransaction(transaction: OnyxTypes.Transaction) { Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`, newTransaction); } -function clearMoneyRequest(transactionID: string, skipConfirmation = false) { - removeDraftTransactions(); +function clearMoneyRequest(transactionID: string, skipConfirmation = false, draftTransactions?: OnyxCollection) { + removeDraftTransactions(undefined, draftTransactions); Onyx.set(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`, skipConfirmation); } -function startMoneyRequest(iouType: ValueOf, reportID: string, requestType?: IOURequestType, skipConfirmation = false, backToReport?: string) { +function startMoneyRequest( + iouType: ValueOf, + reportID: string, + requestType?: IOURequestType, + skipConfirmation = false, + backToReport?: string, + draftTransactions?: OnyxCollection, +) { Performance.markStart(CONST.TIMING.OPEN_CREATE_EXPENSE); - clearMoneyRequest(CONST.IOU.OPTIMISTIC_TRANSACTION_ID, skipConfirmation); + clearMoneyRequest(CONST.IOU.OPTIMISTIC_TRANSACTION_ID, skipConfirmation, draftTransactions); switch (requestType) { case CONST.IOU.REQUEST_TYPE.MANUAL: Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE_TAB_MANUAL.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, reportID, backToReport)); diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index d07ee25a71f7..010ac39489f9 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -1163,8 +1163,8 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string, po }); } -function getDraftTransactions(): Transaction[] { - return Object.values(allTransactionDrafts ?? {}).filter((transaction): transaction is Transaction => !!transaction); +function getDraftTransactions(draftTransactions?: OnyxCollection): Transaction[] { + return Object.values(draftTransactions ?? allTransactionDrafts ?? {}).filter((transaction): transaction is Transaction => !!transaction); } export { diff --git a/src/libs/actions/TransactionEdit.ts b/src/libs/actions/TransactionEdit.ts index 5490daa7b107..ce71ad919d5e 100644 --- a/src/libs/actions/TransactionEdit.ts +++ b/src/libs/actions/TransactionEdit.ts @@ -1,6 +1,6 @@ import {format} from 'date-fns'; import Onyx from 'react-native-onyx'; -import type {Connection, OnyxEntry} from 'react-native-onyx'; +import type {Connection, OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {formatCurrentUserToAttendee} from '@libs/IOUUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -97,8 +97,8 @@ function removeDraftSplitTransaction(transactionID: string | undefined) { Onyx.set(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`, null); } -function removeDraftTransactions(shouldExcludeInitialTransaction = false) { - const draftTransactions = getDraftTransactions(); +function removeDraftTransactions(shouldExcludeInitialTransaction = false, allTransactionDrafts?: OnyxCollection) { + const draftTransactions = getDraftTransactions(allTransactionDrafts); const draftTransactionsSet = draftTransactions.reduce( (acc, item) => { if (shouldExcludeInitialTransaction && item.transactionID === CONST.IOU.OPTIMISTIC_TRANSACTION_ID) { diff --git a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx index 74cc121803b6..46117fb8b18e 100644 --- a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx +++ b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx @@ -104,6 +104,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT const [quickActionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${quickAction?.chatReportID}`, {canBeMissing: true}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true}); + const [allTransactionDrafts] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true}); const policyChatForActivePolicy = useMemo(() => { if (isEmptyObject(activePolicy) || !activePolicy?.isPolicyExpenseChatEnabled) { @@ -223,9 +224,9 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT } // Start the scan flow directly - startMoneyRequest(CONST.IOU.TYPE.CREATE, generateReportID(), CONST.IOU.REQUEST_TYPE.SCAN, false); + startMoneyRequest(CONST.IOU.TYPE.CREATE, generateReportID(), CONST.IOU.REQUEST_TYPE.SCAN, false, undefined, allTransactionDrafts); }); - }, [shouldRedirectToExpensifyClassic]); + }, [shouldRedirectToExpensifyClassic, allTransactionDrafts]); /** * Check if LHN status changed from active to inactive. @@ -311,11 +312,15 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT // When starting to create an expense from the global FAB, there is not an existing report yet. A random optimistic reportID is generated and used // for all of the routes in the creation flow. generateReportID(), + undefined, + undefined, + undefined, + allTransactionDrafts, ); }), }, ]; - }, [translate, shouldRedirectToExpensifyClassic, shouldUseNarrowLayout]); + }, [translate, shouldRedirectToExpensifyClassic, shouldUseNarrowLayout, allTransactionDrafts]); const quickActionMenuItems = useMemo(() => { // Define common properties in baseQuickAction @@ -366,7 +371,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT } const quickActionReportID = policyChatForActivePolicy?.reportID || generateReportID(); - startMoneyRequest(CONST.IOU.TYPE.SUBMIT, quickActionReportID, CONST.IOU.REQUEST_TYPE.SCAN, true); + startMoneyRequest(CONST.IOU.TYPE.SUBMIT, quickActionReportID, CONST.IOU.REQUEST_TYPE.SCAN, true, undefined, allTransactionDrafts); }); }; @@ -402,6 +407,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT showDelegateNoAccessModal, isReportArchived, isManualDistanceTrackingEnabled, + allTransactionDrafts, ]); const isTravelEnabled = useMemo(() => { @@ -519,6 +525,10 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT // When starting to create an invoice from the global FAB, there is not an existing report yet. A random optimistic reportID is generated and used // for all of the routes in the creation flow. generateReportID(), + undefined, + undefined, + undefined, + allTransactionDrafts, ); }), },