Skip to content
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
14 changes: 4 additions & 10 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({

Check warning on line 793 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand Down Expand Up @@ -923,7 +923,7 @@
};

let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 926 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -937,7 +937,7 @@
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 940 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -946,7 +946,7 @@
});

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({

Check warning on line 949 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -960,7 +960,7 @@
});

let allPolicyTags: OnyxCollection<OnyxTypes.PolicyTagLists> = {};
Onyx.connect({

Check warning on line 963 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -973,7 +973,7 @@
});

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({

Check warning on line 976 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -982,7 +982,7 @@
});

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({

Check warning on line 985 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -992,7 +992,7 @@

let userAccountID = -1;
let currentUserEmail = '';
Onyx.connect({

Check warning on line 995 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserEmail = value?.email ?? '';
Expand All @@ -1001,7 +1001,7 @@
});

let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>;
Onyx.connect({

Check warning on line 1004 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
deprecatedCurrentUserPersonalDetails = value?.[userAccountID] ?? undefined;
Expand All @@ -1009,7 +1009,7 @@
});

let allReportActions: OnyxCollection<OnyxTypes.ReportActions>;
Onyx.connect({

Check warning on line 1012 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -1660,19 +1660,13 @@
/**
* Revert custom unit of the draft transaction to the original transaction's value
*/
function resetDraftTransactionsCustomUnit(transactionID: string | undefined) {
if (!transactionID) {
return;
}

const originalTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
if (!originalTransaction) {
function resetDraftTransactionsCustomUnit(transaction: OnyxEntry<OnyxTypes.Transaction>) {
Comment thread
parasharrajat marked this conversation as resolved.
Resolved
if (!transaction?.transactionID) {
return;
}

Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction?.transactionID}`, {
comment: {
customUnit: originalTransaction.comment?.customUnit ?? {},
customUnit: transaction.comment?.customUnit ?? {},
},
});
}
Expand Down
36 changes: 19 additions & 17 deletions src/pages/iou/request/step/IOURequestStepParticipants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useOnyx from '@hooks/useOnyx';
import useOptimisticDraftTransactions from '@hooks/useOptimisticDraftTransactions';
import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses';
import useThemeStyles from '@hooks/useThemeStyles';
import useTransactionsByID from '@hooks/useTransactionsByID';
import {setTransactionReport} from '@libs/actions/Transaction';
import {READ_COMMANDS} from '@libs/API/types';
import {isMobileSafari as isMobileSafariBrowser} from '@libs/Browser';
Expand Down Expand Up @@ -78,10 +79,11 @@ function IOURequestStepParticipants({
const [allPolicies] = useMappedPolicies(policyMapper);

const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES);
const [transactions] = useOptimisticDraftTransactions(initialTransaction);
const [draftTransactions] = useOptimisticDraftTransactions(initialTransaction);
// Depend on transactions.length to avoid updating transactionIDs when only the transaction details change
// eslint-disable-next-line react-hooks/exhaustive-deps
const transactionIDs = useMemo(() => transactions?.map((transaction) => transaction.transactionID), [transactions.length]);
const transactionIDs = useMemo(() => draftTransactions?.map((transaction) => transaction.transactionID), [draftTransactions.length]);
const [transactions] = useTransactionsByID(transactionIDs);

// We need to set selectedReportID if user has navigated back from confirmation page and navigates to confirmation page with already selected participant
const selectedReportID = useRef<string>(participants?.length === 1 ? (participants.at(0)?.reportID ?? reportID) : reportID);
Expand Down Expand Up @@ -162,14 +164,14 @@ function IOURequestStepParticipants({
// because we want to first check if the p2p rate exists on the workspace.
// If it doesn't exist - we'll show an error message to force the user to choose a valid rate from the workspace.
useEffect(() => {
if (!isMovingTransactionFromTrackExpense) {
if (!isMovingTransactionFromTrackExpense || !transactions || transactions?.length === 0) {
return;
}

for (const transactionID of transactionIDs) {
resetDraftTransactionsCustomUnit(transactionID);
for (const transaction of transactions) {
resetDraftTransactionsCustomUnit(transaction);
}
}, [isFocused, isMovingTransactionFromTrackExpense, transactionIDs]);
}, [isFocused, isMovingTransactionFromTrackExpense, transactions]);

const waitForKeyboardDismiss = useCallback(
(callback: () => void) => {
Expand All @@ -192,7 +194,7 @@ function IOURequestStepParticipants({
}

const rateID = CONST.CUSTOM_UNITS.FAKE_P2P_ID;
for (const transaction of transactions) {
for (const transaction of draftTransactions) {
setCustomUnitRateID(transaction.transactionID, rateID, transaction, activePolicy);
const shouldSetParticipantAutoAssignment = iouType === CONST.IOU.TYPE.CREATE;
setMoneyRequestParticipantsFromReport(
Expand All @@ -219,7 +221,7 @@ function IOURequestStepParticipants({
});
}, [
selfDMReportID,
transactions,
draftTransactions,
action,
initialTransactionID,
waitForKeyboardDismiss,
Expand Down Expand Up @@ -250,8 +252,8 @@ function IOURequestStepParticipants({

// Use transactions array if available, otherwise use initialTransactionID directly
// This handles the case where initialTransaction hasn't loaded yet but we still need to set participants
if (transactions.length > 0) {
for (const transaction of transactions) {
if (draftTransactions.length > 0) {
for (const transaction of draftTransactions) {
setMoneyRequestParticipants(transaction.transactionID, val);
}
} else {
Expand All @@ -264,8 +266,8 @@ function IOURequestStepParticipants({
// Otherwise, keep the original p2p rate and let the user manually change it to the one they want from the workspace.
const rateID = DistanceRequestUtils.getCustomUnitRateID({reportID: firstParticipantReportID, isPolicyExpenseChat, policy, lastSelectedDistanceRates});

if (transactions.length > 0) {
for (const transaction of transactions) {
if (draftTransactions.length > 0) {
for (const transaction of draftTransactions) {
setCustomUnitRateID(transaction.transactionID, rateID, transaction, policy);
}
} else {
Expand Down Expand Up @@ -294,7 +296,7 @@ function IOURequestStepParticipants({
selectedReportID.current = firstParticipantReportID || generateReportID();
}
},
[isSplitRequest, allPolicies, iouType, transactions, isMovingTransactionFromTrackExpense, reportID, trackExpense, initialTransactionID, lastSelectedDistanceRates],
[isSplitRequest, allPolicies, iouType, draftTransactions, isMovingTransactionFromTrackExpense, reportID, trackExpense, initialTransactionID, lastSelectedDistanceRates],
);

const goToNextStep = useCallback(
Expand All @@ -312,7 +314,7 @@ function IOURequestStepParticipants({
const shouldUpdateTransactionReportID = participants?.at(0)?.reportID !== newReportID;
const transactionReportID = newReportID === selfDMReportID ? CONST.REPORT.UNREPORTED_REPORT_ID : newReportID;
// TODO: probably should also change participants here for selectedParticipants.current, but out of scope of this PR
for (const transaction of transactions) {
for (const transaction of draftTransactions) {
const tag = isMovingTransactionFromTrackExpense && transaction?.tag ? transaction?.tag : '';
setMoneyRequestTag(transaction.transactionID, tag);
const firstParticipant = _participants?.at(0);
Expand All @@ -327,7 +329,7 @@ function IOURequestStepParticipants({
}
if ((isCategorizing || isShareAction) && numberOfParticipants.current === 0) {
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace(introSelected);
for (const transaction of transactions) {
for (const transaction of draftTransactions) {
setMoneyRequestParticipants(transaction.transactionID, [
{
selected: true,
Expand Down Expand Up @@ -385,7 +387,7 @@ function IOURequestStepParticipants({
initialTransactionID,
reportID,
waitForKeyboardDismiss,
transactions,
draftTransactions,
isMovingTransactionFromTrackExpense,
allPolicies,
policyForMovingExpenses,
Expand Down Expand Up @@ -413,7 +415,7 @@ function IOURequestStepParticipants({
const isCategorizing = action === CONST.IOU.ACTION.CATEGORIZE;
const isShareAction = action === CONST.IOU.ACTION.SHARE;
if (isFocused && (isCategorizing || isShareAction)) {
for (const transaction of transactions) {
for (const transaction of draftTransactions) {
setMoneyRequestParticipants(transaction.transactionID, []);
}
numberOfParticipants.current = 0;
Expand Down
43 changes: 43 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
rejectMoneyRequest,
replaceReceipt,
requestMoney,
resetDraftTransactionsCustomUnit,
retractReport,
setMoneyRequestCategory,
shouldOptimisticallyUpdateSearch,
Expand Down Expand Up @@ -17206,4 +17207,46 @@ describe('actions/IOU', () => {
);
});
});

describe('resetDraftTransactionsCustomUnit', () => {
it('should do nothing if transaction is not passed', async () => {
// Call the reset function without a transaction
resetDraftTransactionsCustomUnit(undefined);
await waitForBatchedUpdates();
const allDraftTransactions = await getOnyxValue(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT);
// Assuming there are no draft transactions, this should be undefined or an empty object
expect(allDraftTransactions).toBeUndefined();
});
it('should reset custom unit for a transaction', async () => {
const transactionID = 'transaction_reset_001';
const fakeTransaction: Transaction = {
transactionID,
amount: 1500,
currency: CONST.CURRENCY.USD,
created: format(new Date(), CONST.DATE.FNS_FORMAT_STRING),
merchant: 'Test Reset',
reportID: 'report_reset_001',
comment: {
type: CONST.TRANSACTION.TYPE.CUSTOM_UNIT,
customUnit: {
name: CONST.CUSTOM_UNITS.NAME_DISTANCE,
quantity: 100,
},
waypoints: {
waypoint0: {lat: 40.7128, lng: -74.006, address: 'NYC', name: 'NYC', keyForList: 'nyc_key'},
},
},
};

await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, fakeTransaction);
await waitForBatchedUpdates();
// Call the reset function
resetDraftTransactionsCustomUnit(fakeTransaction);
await waitForBatchedUpdates();
// Verify the transaction's custom unit and waypoints have been reset
const updatedTransaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`);
expect(updatedTransaction?.comment?.customUnit?.name).toBe(CONST.CUSTOM_UNITS.NAME_DISTANCE);
expect(updatedTransaction?.comment?.customUnit?.quantity).toBe(100);
});
});
});
Loading