From 2f5cd9b04765c7cf2c6d0560e43310c53308fed5 Mon Sep 17 00:00:00 2001 From: dominictb Date: Thu, 11 Jul 2024 15:48:16 +0700 Subject: [PATCH] fix: update logic of checking shouldKeepData Signed-off-by: dominictb Signed-off-by: dominictb --- src/libs/actions/IOU.ts | 16 +++++++--------- src/pages/iou/request/IOURequestStartPage.tsx | 5 ++--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bd4331b19403..ddd9f7ff5f1d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -324,13 +324,7 @@ function getReportPreviewAction(chatReportID: string, iouReportID: string): Onyx * @param shouldKeepExistingData * @param iouRequestType one of manual/scan/distance */ -function initMoneyRequest( - reportID: string, - policy: OnyxEntry, - isFromGlobalCreate: boolean, - shouldKeepExistingData: boolean, - iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL, -) { +function initMoneyRequest(reportID: string, policy: OnyxEntry, isFromGlobalCreate: boolean, iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL) { // Generate a brand new transactionID const newTransactionID = CONST.IOU.OPTIMISTIC_TRANSACTION_ID; const currency = policy?.outputCurrency ?? currentUserPersonalDetails?.localCurrencyCode ?? CONST.CURRENCY.USD; @@ -338,8 +332,12 @@ function initMoneyRequest( // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const created = currentDate || format(new Date(), 'yyyy-MM-dd'); - // In case we should keep existing transaction data, we only need to update the reportID, isFromGlobalCreate, created and currency - if (shouldKeepExistingData) { + const currentTransaction = allTransactionDrafts?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${newTransactionID}`]; + + // in case we have to re-init money request, but the IOU request type is the same with the old draft transaction, + // we should keep most of the existing data by using the ONYX MERGE operation + if (currentTransaction?.iouRequestType === iouRequestType) { + // so, we just need to update the reportID, isFromGlobalCreate, created, currency Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${newTransactionID}`, { reportID, isFromGlobalCreate, diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index 823b3999890e..82bb947d1525 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -96,8 +96,7 @@ function IOURequestStartPage({ if (transaction?.reportID === reportID) { return; } - const shouldKeepExistingData = !!transaction?.isFromGlobalCreate && transaction?.iouRequestType === transactionRequestType.current; - IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, shouldKeepExistingData, transactionRequestType.current); + IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, transactionRequestType.current); }, [transaction, policy, reportID, iouType, isFromGlobalCreate]); const isExpenseChat = ReportUtils.isPolicyExpenseChat(report); @@ -113,7 +112,7 @@ function IOURequestStartPage({ if (transaction?.iouRequestType === newIOUType) { return; } - IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, false, newIOUType); + IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, newIOUType); }, [policy, reportID, isFromGlobalCreate, transaction], );