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 all 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
21 changes: 19 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,31 @@ function getReportPreviewAction(chatReportID: string, iouReportID: string): Onyx
* @param policy
* @param isFromGlobalCreate
* @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) {
// 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');

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,
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 +367,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
5 changes: 4 additions & 1 deletion src/pages/iou/request/IOURequestStartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ function IOURequestStartPage({

const resetIOUTypeIfChanged = useCallback(
(newIOUType: IOURequestType) => {
if (transaction?.iouRequestType === newIOUType) {
return;
}
IOU.initMoneyRequest(reportID, policy, isFromGlobalCreate, 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