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

fix: do not refresh draft txn after redirect #44363

Merged
merged 15 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
26 changes: 23 additions & 3 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,35 @@ function getReportPreviewAction(chatReportID: string, iouReportID: string): Onyx
* @param reportID to attach the transaction to
* @param policy
* @param isFromGlobalCreate
* @param shouldKeepExistingData
dominictb marked this conversation as resolved.
Show resolved Hide resolved
* @param iouRequestType one of manual/scan/distance
* @param skipConfirmation if true, skip confirmation step
*/
function initMoneyRequest(reportID: string, policy: OnyxEntry<OnyxTypes.Policy>, isFromGlobalCreate: boolean, iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL) {
function initMoneyRequest(
reportID: string,
policy: OnyxEntry<OnyxTypes.Policy>,
isFromGlobalCreate: boolean,
shouldKeepExistingData: boolean,
dominictb marked this conversation as resolved.
Show resolved Hide resolved
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;
// Disabling this line since currentDate can be an empty string
// 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) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${newTransactionID}`, {
reportID,
isFromGlobalCreate,
created,
currency,
transactionID: newTransactionID,
Comment on lines +341 to +345
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we resetting all these props? Shouldn't be just the report id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is that the transaction should look like it is re-created, so the data like reportID, created, currency, even transactionID (a bit redundant), isFromGlobalCreated should be updated, but the other old data will be kept intact since iouRequestType is the same

});
return;
}

const comment: Comment = {};

// Add initial empty waypoints when starting a distance expense
Expand All @@ -350,7 +370,7 @@ function initMoneyRequest(reportID: string, policy: OnyxEntry<OnyxTypes.Policy>,
amount: 0,
comment,
created,
currency: policy?.outputCurrency ?? currentUserPersonalDetails?.localCurrencyCode ?? CONST.CURRENCY.USD,
currency,
iouRequestType,
reportID,
transactionID: newTransactionID,
Expand Down
10 changes: 7 additions & 3 deletions src/pages/iou/request/IOURequestStartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function IOURequestStartPage({
if (transaction?.reportID === reportID) {
return;
}
IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, transactionRequestType.current);
const shouldKeepExistingData = !!transaction?.isFromGlobalCreate && transaction?.iouRequestType === transactionRequestType.current;
IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, shouldKeepExistingData, transactionRequestType.current);
}, [transaction, policy, reportID, iouType, isFromGlobalCreate]);

const isExpenseChat = ReportUtils.isPolicyExpenseChat(report);
Expand All @@ -109,9 +110,12 @@ function IOURequestStartPage({

const resetIOUTypeIfChanged = useCallback(
(newIOUType: IOURequestType) => {
IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, newIOUType);
if (transaction?.iouRequestType === newIOUType) {
return;
}
IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, false, newIOUType);
},
[policy, reportID, isFromGlobalCreate],
[policy, reportID, isFromGlobalCreate, transaction],
);

if (!transaction?.transactionID) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ function IOURequestStepConfirmation({
// If there is not a report attached to the IOU with a reportID, then the participants were manually selected and the user needs taken
// back to the participants step
if (!transaction?.participantsAutoAssigned) {
Navigation.goBack(ROUTES.MONEY_REQUEST_STEP_PARTICIPANTS.getRoute(iouType, transactionID, reportID, undefined, action));
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Navigation.goBack(ROUTES.MONEY_REQUEST_STEP_PARTICIPANTS.getRoute(iouType, transactionID, transaction?.reportID || reportID, undefined, action));
return;
}
IOUUtils.navigateToStartMoneyRequestStep(requestType, iouType, transactionID, reportID, action);
Expand Down
Loading