-
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
fix: [P2P Distance] - Distance unit in the system message changes from miles to mi after changing rate. #47605
Conversation
…m miles to mi after changing rate. Signed-off-by: krishna2323 <[email protected]>
@paultsimura Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
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.
A couple of small notes.
src/libs/DistanceRequestUtils.ts
Outdated
@@ -171,7 +179,7 @@ function getDistanceForDisplay(hasRoute: boolean, distanceInMeters: number, unit | |||
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer'); | |||
const unitString = distanceInUnits === '1' ? singularDistanceUnit : distanceUnit; | |||
|
|||
return `${distanceInUnits} ${unitString}`; | |||
return `${distanceInUnits} ${useShortFormUnit ? unit : unitString}`; |
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.
Let's avoid all the unnecessary unitString
calculations by returning early if useShortFormUnit
:
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;
return `${distanceInUnits} ${unitString}`;
}
This should work, right?
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.
This should work, right?
yes, updating...
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.
updated.
unit: Unit | undefined, | ||
rate: number | undefined, | ||
translate: LocaleContextProps['translate'], | ||
useShortFormUnit?: boolean, |
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.
@@ -137,6 +137,7 @@ function getRateForDisplay( | |||
translate: LocaleContextProps['translate'], | |||
toLocaleDigit: LocaleContextProps['toLocaleDigit'], | |||
isOffline?: boolean, | |||
useShortFormUnit?: boolean, |
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.
Reviewer Checklist
Screenshots/VideosAndroid: Native2024-08-20.-.20.36.-.android.mp4Android: mWeb Chrome2024-08-20.-.20.36.-.chrome.mp4iOS: Native2024-08-20.-.20.36.-.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-08-20.at.20.32.43.mp4iOS: mWeb Safari2024-08-20.-.20.36.-.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-08-20.at.20.35.45.mp4MacOS: Chrome / Safari2024-08-20.-.20.36.-.Screen.Recording.2024-08-20.at.19.57.35.mp4MacOS: Desktop2024-08-20.-.20.36.-.Screen.Recording.2024-08-20.at.20.02.35.mp4 |
src/libs/DistanceRequestUtils.ts
Outdated
@@ -192,13 +200,14 @@ function getDistanceMerchant( | |||
currency: string, | |||
translate: LocaleContextProps['translate'], | |||
toLocaleDigit: LocaleContextProps['toLocaleDigit'], | |||
useShortFormUnit?: boolean, |
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.
We are now always building the merchant with the short unit form.
So we can remove this param and just call both getDistanceForDisplay
and getRateForDisplay
with useShortFormUnit: true
@Krishna2323 the PR tests good in general, thanks. There are a couple of things to change though. |
Signed-off-by: krishna2323 <[email protected]>
Signed-off-by: krishna2323 <[email protected]>
Signed-off-by: krishna2323 <[email protected]>
src/libs/PolicyUtils.ts
Outdated
if (!Number.isNaN(Number(numValue)) && withDecimals) { | ||
return Number(numValue).toFixed(2).toString().replace('.', toLocaleDigit('.')); | ||
} |
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.
There is an issue with this approach.
When the rate is with decimals, it uses 3 digits after a comma, and here you cut it to fixed 2, which leads to a discrepancy with the BE merchant:
Line 2221 in c0a9cbf
RATE_DECIMALS: 3, |
We want to build the following logic:
- If the value has 3 decimals, leave them as is
- If it has less than 2 decimals, add trailing zeros to have 2 after comma
A potential solution would be to round to 3 decimals by default and cut the last one off if it's a 0.
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.
Also, since we perform this check and return early:
Lines 142 to 144 in 4daf8df
if (Number.isNaN(numValue)) { | |
return ''; | |
} |
Do you think the !Number.isNaN(Number(numValue))
check is necessary here?
Can't we use just if (withDecimals)
assuming that !Number.isNaN(Number(numValue))
would have caused the early return?
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.
Let's handle this properly:
- Rewrite this function to handle the digits correctly.
- Add
PolicyUtils
unit tests to cover thegetRateDisplayValue
function with different params.
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.
I will start updating accordingly in few hours.
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.
@paultsimura, can you please check the updated code?
Signed-off-by: krishna2323 <[email protected]>
Thanks @Krishna2323, this tests well. Please add the unit test – import * as PolicyUtils from '../../src/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');
});
});
});
}); |
Signed-off-by: krishna2323 <[email protected]>
@paultsimura, unit test added. |
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.
Solid work done here, thanks ✔️
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
🚀 Deployed to staging by https://github.com/puneetlath in version: 9.0.26-1 🚀
|
🚀 Deployed to production by https://github.com/puneetlath in version: 9.0.26-6 🚀
|
Details
Fixed Issues
$ #46873
PROPOSAL: #46873 (comment)
Tests
Offline tests
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodSTYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)Design
label and/or tagged@Expensify/design
so the design team can review the changes.ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Android: Native
android_native.mp4
Android: mWeb Chrome
android_chrome.mp4
iOS: Native
ios_native.mp4
iOS: mWeb Safari
ios_safari.mp4
MacOS: Chrome / Safari
web_chrome.mp4
MacOS: Desktop
desktop_app.mp4