-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Use transaction distanceUnit to prevent retroactive changes #50001
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
Changes from all commits
b9d9f6b
3cedd46
d6c88de
778dd23
540f11a
3961d4c
4520fe3
7579b73
9936790
56c96fb
ebff76e
9a06cfe
7982c26
5c43231
e07a837
18657fd
1703edb
466990b
da47219
8f10f77
5bd5c3a
a307c8d
99e9170
aded5c2
7a9d0ea
1e87ca3
ce80ef1
817266b
a2f0074
00eeb4a
5b12592
01e652a
611ad8b
f0ec762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -98,9 +98,6 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals | |||
| const [parentReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, { | ||||
| canEvict: false, | ||||
| }); | ||||
| const [distanceRates = {}] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { | ||||
| selector: () => DistanceRequestUtils.getMileageRates(policy, true), | ||||
| }); | ||||
| const [transactionViolations] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${getTransactionID(report, parentReportActions)}`); | ||||
|
|
||||
| const parentReportAction = parentReportActions?.[report?.parentReportActionID ?? '-1']; | ||||
|
|
@@ -202,14 +199,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals | |||
| let amountDescription = `${translate('iou.amount')}`; | ||||
|
|
||||
| const hasRoute = TransactionUtils.hasRoute(transactionBackup ?? transaction, isDistanceRequest); | ||||
| const rateID = TransactionUtils.getRateID(transaction) ?? '-1'; | ||||
|
|
||||
| const currency = transactionCurrency ?? CONST.CURRENCY.USD; | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this change re-introduced a bug we fixed in #50142.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to move that logic to Do you know why the currency is displayed correctly on the rate change page?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We display the transaction's currency for the selected rate:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Moving it into
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||||
|
|
||||
| const mileageRate = TransactionUtils.isCustomUnitRateIDForP2P(transaction) ? DistanceRequestUtils.getRateForP2P(currency) : distanceRates[rateID] ?? {}; | ||||
| const {unit} = mileageRate; | ||||
| const rate = transaction?.comment?.customUnit?.defaultP2PRate ?? mileageRate.rate; | ||||
|
neil-marcellini marked this conversation as resolved.
|
||||
|
|
||||
| const {unit, rate, currency} = DistanceRequestUtils.getRate({transaction, policy}); | ||||
| const distance = TransactionUtils.getDistanceInMeters(transactionBackup ?? transaction, unit); | ||||
| const rateToDisplay = DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline); | ||||
| const distanceToDisplay = DistanceRequestUtils.getDistanceForDisplay(hasRoute, distance, unit, rate, translate); | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import lodashHas from 'lodash/has'; | ||
| import lodashIsEqual from 'lodash/isEqual'; | ||
| import lodashSet from 'lodash/set'; | ||
| import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||
| import Onyx from 'react-native-onyx'; | ||
| import type {ValueOf} from 'type-fest'; | ||
|
|
@@ -139,6 +140,8 @@ function buildOptimisticTransaction( | |
| billable = false, | ||
| pendingFields: Partial<{[K in TransactionPendingFieldsKey]: ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>}> | undefined = undefined, | ||
| reimbursable = true, | ||
| existingTransaction: OnyxEntry<Transaction> | undefined = undefined, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function ( |
||
| policy: OnyxEntry<Policy> = undefined, | ||
| ): Transaction { | ||
| // transactionIDs are random, positive, 64-bit numeric strings. | ||
| // Because JS can only handle 53-bit numbers, transactionIDs are strings in the front-end (just like reportActionID) | ||
|
|
@@ -152,6 +155,12 @@ function buildOptimisticTransaction( | |
| commentJSON.originalTransactionID = originalTransactionID; | ||
| } | ||
|
|
||
| const isDistanceTransaction = !!pendingFields?.waypoints; | ||
| if (isDistanceTransaction) { | ||
| // Set the distance unit, which comes from the policy distance unit or the P2P rate data | ||
| lodashSet(commentJSON, 'customUnit.distanceUnit', DistanceRequestUtils.getUpdatedDistanceUnit({transaction: existingTransaction, policy})); | ||
| } | ||
|
|
||
| return { | ||
| ...(!isEmptyObject(pendingFields) ? {pendingFields} : {}), | ||
| transactionID, | ||
|
|
@@ -261,15 +270,26 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra | |
| } | ||
|
|
||
| if (Object.hasOwn(transactionChanges, 'customUnitRateID')) { | ||
| updatedTransaction.comment = { | ||
| ...(updatedTransaction?.comment ?? {}), | ||
| customUnit: { | ||
| ...updatedTransaction?.comment?.customUnit, | ||
| customUnitRateID: transactionChanges.customUnitRateID, | ||
| defaultP2PRate: null, | ||
| }, | ||
| }; | ||
| lodashSet(updatedTransaction, 'comment.customUnit.customUnitRateID', transactionChanges.customUnitRateID); | ||
| lodashSet(updatedTransaction, 'comment.customUnit.defaultP2PRate', null); | ||
| shouldStopSmartscan = true; | ||
|
|
||
| const existingDistanceUnit = transaction?.comment?.customUnit?.distanceUnit; | ||
| const allReports = ReportConnection.getAllReports(); | ||
| const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction.reportID}`] ?? null; | ||
| const policyID = report?.policyID ?? ''; | ||
| const policy = PolicyUtils.getPolicy(policyID); | ||
|
neil-marcellini marked this conversation as resolved.
|
||
|
|
||
| // Get the new distance unit from the rate's unit | ||
| const newDistanceUnit = DistanceRequestUtils.getUpdatedDistanceUnit({transaction: updatedTransaction, policy}); | ||
|
|
||
| // If the distanceUnit is set and the rate is changed to one that has a different unit, convert the distance to the new unit | ||
| if (existingDistanceUnit && newDistanceUnit !== existingDistanceUnit) { | ||
| const conversionFactor = existingDistanceUnit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? CONST.CUSTOM_UNITS.MILES_TO_KILOMETERS : CONST.CUSTOM_UNITS.KILOMETERS_TO_MILES; | ||
| const distance = NumberUtils.roundToTwoDecimalPlaces((transaction?.comment?.customUnit?.quantity ?? 0) * conversionFactor); | ||
| lodashSet(updatedTransaction, 'comment.customUnit.quantity', distance); | ||
| lodashSet(updatedTransaction, 'pendingFields.waypoints', CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from #51285 {BZ checklist}, setting pending action on waypoint caused the map preview to be blank when selecting distance rate with different unit. There more to RC see detailed Analysis in this Proposal #51285 (comment) |
||
| } | ||
| } | ||
|
|
||
| if (Object.hasOwn(transactionChanges, 'taxAmount') && typeof transactionChanges.taxAmount === 'number') { | ||
|
|
@@ -823,7 +843,7 @@ function calculateAmountForUpdatedWaypointOrRate( | |
| const mileageRates = DistanceRequestUtils.getMileageRates(policy, true); | ||
| const policyCurrency = policy?.outputCurrency ?? PolicyUtils.getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD; | ||
| const mileageRate = isCustomUnitRateIDForP2P(transaction) | ||
| ? DistanceRequestUtils.getRateForP2P(policyCurrency) | ||
| ? DistanceRequestUtils.getRateForP2P(policyCurrency, transaction ?? undefined) | ||
| : mileageRates?.[customUnitRateID] ?? DistanceRequestUtils.getDefaultMileageRate(policy); | ||
| const {unit, rate, currency} = mileageRate; | ||
|
|
||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.