-
Notifications
You must be signed in to change notification settings - Fork 3k
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: [P2P Distance] - Distance unit in the system message changes from miles to mi after changing rate. #47605
Merged
puneetlath
merged 8 commits into
Expensify:main
from
Krishna2323:krishna2323/issue/46873
Aug 27, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4e2234a
fix: [P2P Distance] - Distance unit in the system message changes fro…
Krishna2323 5be6b94
Merge branch 'Expensify:main' into krishna2323/issue/46873
Krishna2323 b8e14c9
fix: use shortform for unit in money request preview.
Krishna2323 2151608
add suggested changes.
Krishna2323 3962b7d
minor spacing fix.
Krishna2323 4daf8df
minor fix.
Krishna2323 f2da0ae
update getRateDisplayValue to return correct decimal points.
Krishna2323 fdd5fc7
add test for PolicyUtils.
Krishna2323 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,7 @@ function getRoundedDistanceInUnits(distanceInMeters: number, unit: Unit): string | |
* @param currency The currency associated with the rate | ||
* @param translate Translate function | ||
* @param toLocaleDigit Function to convert to localized digit | ||
* @param useShortFormUnit If true, the unit will be returned in short form (e.g., "mi", "km"). | ||
* @returns A string that displays the rate used for expense calculation | ||
*/ | ||
function getRateForDisplay( | ||
|
@@ -137,6 +138,7 @@ function getRateForDisplay( | |
translate: LocaleContextProps['translate'], | ||
toLocaleDigit: LocaleContextProps['toLocaleDigit'], | ||
isOffline?: boolean, | ||
useShortFormUnit?: boolean, | ||
): string { | ||
if (isOffline && !rate) { | ||
return translate('iou.defaultRate'); | ||
|
@@ -146,11 +148,11 @@ function getRateForDisplay( | |
} | ||
|
||
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer'); | ||
const formattedRate = PolicyUtils.getUnitRateValue(toLocaleDigit, {rate}); | ||
const formattedRate = PolicyUtils.getUnitRateValue(toLocaleDigit, {rate}, useShortFormUnit); | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
const currencySymbol = CurrencyUtils.getCurrencySymbol(currency) || `${currency} `; | ||
|
||
return `${currencySymbol}${formattedRate} / ${singularDistanceUnit}`; | ||
return `${currencySymbol}${formattedRate} / ${useShortFormUnit ? unit : singularDistanceUnit}`; | ||
} | ||
|
||
/** | ||
|
@@ -159,14 +161,26 @@ function getRateForDisplay( | |
* @param unit Unit that should be used to display the distance | ||
* @param rate Expensable amount allowed per unit | ||
* @param translate Translate function | ||
* @param useShortFormUnit If true, the unit will be returned in short form (e.g., "mi", "km"). | ||
* @returns A string that describes the distance traveled | ||
*/ | ||
function getDistanceForDisplay(hasRoute: boolean, distanceInMeters: number, unit: Unit | undefined, rate: number | undefined, translate: LocaleContextProps['translate']): string { | ||
function getDistanceForDisplay( | ||
hasRoute: boolean, | ||
distanceInMeters: number, | ||
unit: Unit | undefined, | ||
rate: number | undefined, | ||
translate: LocaleContextProps['translate'], | ||
useShortFormUnit?: boolean, | ||
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. Please add it to JsDoc. 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. done. |
||
): string { | ||
if (!hasRoute || !rate || !unit || !distanceInMeters) { | ||
return translate('iou.fieldPending'); | ||
} | ||
|
||
const distanceInUnits = getRoundedDistanceInUnits(distanceInMeters, unit); | ||
if (useShortFormUnit) { | ||
return `${distanceInUnits} ${unit}`; | ||
} | ||
|
||
const distanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers'); | ||
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer'); | ||
const unitString = distanceInUnits === '1' ? singularDistanceUnit : distanceUnit; | ||
|
@@ -197,8 +211,8 @@ function getDistanceMerchant( | |
return translate('iou.fieldPending'); | ||
} | ||
|
||
const distanceInUnits = getDistanceForDisplay(hasRoute, distanceInMeters, unit, rate, translate); | ||
const ratePerUnit = getRateForDisplay(unit, rate, currency, translate, toLocaleDigit); | ||
const distanceInUnits = getDistanceForDisplay(hasRoute, distanceInMeters, unit, rate, translate, true); | ||
const ratePerUnit = getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, undefined, true); | ||
|
||
return `${distanceInUnits} @ ${ratePerUnit}`; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
|
||
function toLocaleDigitMock(dot: string): string { | ||
return dot; | ||
} | ||
|
||
describe('PolicyUtils', () => { | ||
describe('getRateDisplayValue', () => { | ||
it('should return an empty string for NaN', () => { | ||
const rate = PolicyUtils.getRateDisplayValue('invalid' as unknown as number, toLocaleDigitMock); | ||
expect(rate).toEqual(''); | ||
}); | ||
|
||
describe('withDecimals = false', () => { | ||
it('should return integer value as is', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(100, toLocaleDigitMock); | ||
expect(rate).toEqual('100'); | ||
}); | ||
|
||
it('should return non-integer value as is', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(10.5, toLocaleDigitMock); | ||
expect(rate).toEqual('10.5'); | ||
}); | ||
}); | ||
|
||
describe('withDecimals = true', () => { | ||
it('should return integer value with 2 trailing zeros', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(10, toLocaleDigitMock, true); | ||
expect(rate).toEqual('10.00'); | ||
}); | ||
|
||
it('should return non-integer value with up to 2 trailing zeros', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(10.5, toLocaleDigitMock, true); | ||
expect(rate).toEqual('10.50'); | ||
}); | ||
|
||
it('should return non-integer value with 3 decimals as is', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(10.531, toLocaleDigitMock, true); | ||
expect(rate).toEqual('10.531'); | ||
}); | ||
|
||
it('should return non-integer value with 3+ decimals cut to 3', () => { | ||
const rate = PolicyUtils.getRateDisplayValue(10.531345, toLocaleDigitMock, true); | ||
expect(rate).toEqual('10.531'); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add it to JsDoc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.