Skip to content

Commit

Permalink
create new function to create and remove draft transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanDylann committed Apr 21, 2024
1 parent 8616678 commit 0c6a7e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/libs/actions/TransactionEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ function restoreOriginalTransactionFromBackup(transactionID: string, isDraft: bo
});
}

export {createBackupTransaction, removeBackupTransaction, restoreOriginalTransactionFromBackup};
function createDraftTransaction(transaction: OnyxEntry<Transaction>) {
if (!transaction) {
return;
}

const newTransaction = {
...transaction,
};

// Use set so that it will always fully overwrite any backup transaction that could have existed before
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`, newTransaction);
}

function removeDraftTransaction(transactionID: string) {
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, null);
}

export {createBackupTransaction, removeBackupTransaction, restoreOriginalTransactionFromBackup, createDraftTransaction, removeDraftTransaction};
16 changes: 8 additions & 8 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type IOURequestStepAmountOnyxProps = {
/** Whether the confirmation step should be skipped */
skipConfirmation: OnyxEntry<boolean>;

/** The backup transaction object being modified in Onyx */
backupTransaction: OnyxEntry<OnyxTypes.Transaction>;
/** The draft transaction object being modified in Onyx */
draftTransaction: OnyxEntry<OnyxTypes.Transaction>;

/** Personal details of all users */
personalDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>;
Expand All @@ -66,8 +66,8 @@ function IOURequestStepAmount({
personalDetails,
currentUserPersonalDetails,
splitDraftTransaction,
backupTransaction,
skipConfirmation,
draftTransaction,
}: IOURequestStepAmountProps) {
const {translate} = useLocalize();
const textInput = useRef<BaseTextInputRef | null>(null);
Expand All @@ -79,7 +79,7 @@ function IOURequestStepAmount({
const isSplitBill = iouType === CONST.IOU.TYPE.SPLIT;
const isEditingSplitBill = isEditing && isSplitBill;
const {amount: transactionAmount} = ReportUtils.getTransactionDetails(isEditingSplitBill && !isEmptyObject(splitDraftTransaction) ? splitDraftTransaction : transaction) ?? {amount: 0};
const {currency: originalCurrency} = ReportUtils.getTransactionDetails(isEditing ? backupTransaction : transaction) ?? {currency: CONST.CURRENCY.USD};
const {currency: originalCurrency} = ReportUtils.getTransactionDetails(isEditing ? draftTransaction : transaction) ?? {currency: CONST.CURRENCY.USD};
const currency = CurrencyUtils.isValidCurrencyCode(selectedCurrency) ? selectedCurrency : originalCurrency;

// For quick button actions, we'll skip the confirmation page unless the report is archived or this is a workspace request, as
Expand Down Expand Up @@ -111,13 +111,13 @@ function IOURequestStepAmount({
// A temporary solution to not prevent users from editing the currency
// We create a backup transaction and use it to save the currency and remove this transaction backup if we don't save the amount
// It should be removed after this issue https://github.com/Expensify/App/issues/34607 is fixed
TransactionEdit.createBackupTransaction(isEditingSplitBill && !isEmptyObject(splitDraftTransaction) ? splitDraftTransaction : transaction);
TransactionEdit.createDraftTransaction(isEditingSplitBill && !isEmptyObject(splitDraftTransaction) ? splitDraftTransaction : transaction);

return () => {
if (isSaveButtonPressed.current) {
return;
}
TransactionEdit.removeBackupTransaction(transaction?.transactionID ?? '');
TransactionEdit.removeDraftTransaction(transaction?.transactionID ?? '');
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down Expand Up @@ -266,10 +266,10 @@ const IOURequestStepAmountWithOnyx = withOnyx<IOURequestStepAmountProps, IOURequ
return `${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`;
},
},
backupTransaction: {
draftTransaction: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? 0;
return `${ONYXKEYS.COLLECTION.TRANSACTION_BACKUP}${transactionID}`;
return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`;
},
},
skipConfirmation: {
Expand Down

0 comments on commit 0c6a7e5

Please sign in to comment.