Skip to content

Commit

Permalink
Merge pull request #44363 from dominictb/fix/40936-init-money-req
Browse files Browse the repository at this point in the history
fix: do not refresh draft txn after redirect
  • Loading branch information
puneetlath authored Jul 11, 2024
2 parents e7c027a + 07e9c6e commit e0635bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
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,
});
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

0 comments on commit e0635bc

Please sign in to comment.