Skip to content
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

feat: create formatPhoneNumber util function #16804

Merged
merged 18 commits into from
Apr 18, 2023

Conversation

koko57
Copy link
Contributor

@koko57 koko57 commented Mar 31, 2023

Details

Fixed Issues

$ #16099
PROPOSAL: #16099(COMMENT)

Tests

  1. Login to app
    --- CHAT ---
  2. Create a new chat - enter a phone number.
  3. A new chat should appear (in the list, for web desktop in the drawer ) with properly formatted phone number - national format for the logged in user's region, international format for other regions.
  4. Open chat details page - the number should also be formatted properly.
    --- CONTACT METHODS ---
  5. Open Settings -> Profile -> Contact method. If you already have a phone number as a secondary contact method it should be formatted locally.
  6. If not, try to add your number and it should appear on the list. The number in the text "Please enter the magic code sent to [number] should be also formatted.
    --- LOGIN ---
  7. Log out, then try to sign up with a new mobile phone number. The number in the text "I've sent a magic sign-in link to..." should be displayed with the country code.
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

same as above

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web

Screenshot 2023-03-31 at 14 45 03
Screenshot 2023-03-31 at 14 53 37
Screenshot 2023-03-31 at 10 27 26
Screenshot 2023-03-31 at 15 05 40

Mobile Web - Chrome

Screenshot_1680268703

Mobile Web - Safari

Simulator Screen Shot - iPhone 13 - 2023-03-31 at 15 17 38
Simulator Screen Shot - iPhone 13 - 2023-03-31 at 15 20 32

Desktop

Screenshot 2023-03-31 at 14 46 35
Screenshot 2023-03-31 at 15 05 23
Screenshot 2023-03-31 at 15 29 06

iOS

Simulator Screen Shot - iPhone 13 - 2023-03-31 at 14 48 17
Simulator Screen Shot - iPhone 13 - 2023-03-31 at 15 16 05
Simulator Screen Shot - iPhone 13 - 2023-03-31 at 14 48 33

Android

Screenshot_1680268097
Screenshot_1680268595

@koko57 koko57 requested a review from a team as a code owner March 31, 2023 13:37
@melvin-bot melvin-bot bot requested review from jasperhuangg and removed request for a team March 31, 2023 13:38
@MelvinBot
Copy link

@jasperhuangg 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]

@koko57
Copy link
Contributor Author

koko57 commented Mar 31, 2023

as for this one:
Screenshot 2023-03-31 at 09 50 02
Screenshot 2023-03-31 at 09 58 06

we have this number in a message text, but I can remove the '@expensify.sms' domain

Copy link
Contributor

@jasperhuangg jasperhuangg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be sure to include all the pages that this touches in the testing steps?

src/libs/formatPhoneNumber.js Outdated Show resolved Hide resolved
src/pages/signin/ResendValidationForm.js Show resolved Hide resolved
@jasperhuangg
Copy link
Contributor

we have this number in a message text, but I can remove the '@expensify.sms' domain

@koko57 It looks like there's an issue with how SMS logins are getting formatted (the SMS domain shouldn't be included in those messages). Can you look into this and possibly use your new utility function there as well? More here:

App/src/libs/ReportUtils.js

Lines 735 to 756 in bbcf7ce

/**
* Get the displayName for a single report participant.
*
* @param {String} login
* @param {Boolean} [shouldUseShortForm]
* @returns {String}
*/
function getDisplayNameForParticipant(login, shouldUseShortForm = false) {
if (!login) {
return '';
}
const personalDetails = getPersonalDetailsForLogin(login);
const loginWithoutSMSDomain = Str.removeSMSDomain(personalDetails.login);
let longName = personalDetails.displayName || loginWithoutSMSDomain;
if (longName === loginWithoutSMSDomain && Str.isSMSLogin(longName)) {
longName = LocalePhoneNumber.toLocalPhone(preferredLocale, longName);
}
const shortName = personalDetails.firstName || longName;
return shouldUseShortForm ? shortName : longName;
}

@koko57
Copy link
Contributor Author

koko57 commented Mar 31, 2023

@jasperhuangg yes, of course, I'll take care of it

@koko57 koko57 marked this pull request as draft April 4, 2023 13:44
@koko57
Copy link
Contributor Author

koko57 commented Apr 4, 2023

we have this number in a message text, but I can remove the '@expensify.sms' domain

@koko57 It looks like there's an issue with how SMS logins are getting formatted (the SMS domain shouldn't be included in those messages). Can you look into this and possibly use your new utility function there as well? More here:

App/src/libs/ReportUtils.js

Lines 735 to 756 in bbcf7ce

/**
* Get the displayName for a single report participant.
*
* @param {String} login
* @param {Boolean} [shouldUseShortForm]
* @returns {String}
*/
function getDisplayNameForParticipant(login, shouldUseShortForm = false) {
if (!login) {
return '';
}
const personalDetails = getPersonalDetailsForLogin(login);
const loginWithoutSMSDomain = Str.removeSMSDomain(personalDetails.login);
let longName = personalDetails.displayName || loginWithoutSMSDomain;
if (longName === loginWithoutSMSDomain && Str.isSMSLogin(longName)) {
longName = LocalePhoneNumber.toLocalPhone(preferredLocale, longName);
}
const shortName = personalDetails.firstName || longName;
return shouldUseShortForm ? shortName : longName;
}

@jasperhuangg

Yes, the util is already used in the getDisplayNameForParticipant function. When I i.e. request money - for a split of a second the number is displayed correctly (formatted) but then as the request succeed the text is updated and the unformatted number is displayed (in the pushJSON the message includes the 'expensify.sms' domain). We are only formatting the number for the message for the optimisticData here. The data we get when we open the chat also looks like this:
Screenshot 2023-04-04 at 13 12 27

Correct me if I'm wrong but it seems that the message is stored that way in the backend. So if it should be fixed on the frontend side I need to create another function to format these messages correctly using the formatPhoneNumber util.

src/libs/LocalePhoneNumber.js Outdated Show resolved Hide resolved
src/components/withLocalize.js Outdated Show resolved Hide resolved
@koko57 koko57 marked this pull request as ready for review April 5, 2023 12:39
@koko57 koko57 marked this pull request as draft April 5, 2023 12:41
@koko57 koko57 marked this pull request as ready for review April 5, 2023 13:01
@jasperhuangg
Copy link
Contributor

we have this number in a message text, but I can remove the '@expensify.sms' domain

@koko57 It looks like there's an issue with how SMS logins are getting formatted (the SMS domain shouldn't be included in those messages). Can you look into this and possibly use your new utility function there as well? More here:

App/src/libs/ReportUtils.js

Lines 735 to 756 in bbcf7ce

/**
* Get the displayName for a single report participant.
*
* @param {String} login
* @param {Boolean} [shouldUseShortForm]
* @returns {String}
*/
function getDisplayNameForParticipant(login, shouldUseShortForm = false) {
if (!login) {
return '';
}
const personalDetails = getPersonalDetailsForLogin(login);
const loginWithoutSMSDomain = Str.removeSMSDomain(personalDetails.login);
let longName = personalDetails.displayName || loginWithoutSMSDomain;
if (longName === loginWithoutSMSDomain && Str.isSMSLogin(longName)) {
longName = LocalePhoneNumber.toLocalPhone(preferredLocale, longName);
}
const shortName = personalDetails.firstName || longName;
return shouldUseShortForm ? shortName : longName;
}

@jasperhuangg

Yes, the util is already used in the getDisplayNameForParticipant function. When I i.e. request money - for a split of a second the number is displayed correctly (formatted) but then as the request succeed the text is updated and the unformatted number is displayed (in the pushJSON the message includes the 'expensify.sms' domain). We are only formatting the number for the message for the optimisticData here. The data we get when we open the chat also looks like this: Screenshot 2023-04-04 at 13 12 27

Correct me if I'm wrong but it seems that the message is stored that way in the backend. So if it should be fixed on the frontend side I need to create another function to format these messages correctly using the formatPhoneNumber util.

@koko57 We should format the text right before it's displayed in addition to formatting it in the optimisticData

@koko57
Copy link
Contributor Author

koko57 commented Apr 6, 2023

we have this number in a message text, but I can remove the '@expensify.sms' domain

@koko57 It looks like there's an issue with how SMS logins are getting formatted (the SMS domain shouldn't be included in those messages). Can you look into this and possibly use your new utility function there as well? More here:

App/src/libs/ReportUtils.js

Lines 735 to 756 in bbcf7ce

/**
* Get the displayName for a single report participant.
*
* @param {String} login
* @param {Boolean} [shouldUseShortForm]
* @returns {String}
*/
function getDisplayNameForParticipant(login, shouldUseShortForm = false) {
if (!login) {
return '';
}
const personalDetails = getPersonalDetailsForLogin(login);
const loginWithoutSMSDomain = Str.removeSMSDomain(personalDetails.login);
let longName = personalDetails.displayName || loginWithoutSMSDomain;
if (longName === loginWithoutSMSDomain && Str.isSMSLogin(longName)) {
longName = LocalePhoneNumber.toLocalPhone(preferredLocale, longName);
}
const shortName = personalDetails.firstName || longName;
return shouldUseShortForm ? shortName : longName;
}

@jasperhuangg

Yes, the util is already used in the getDisplayNameForParticipant function. When I i.e. request money - for a split of a second the number is displayed correctly (formatted) but then as the request succeed the text is updated and the unformatted number is displayed (in the pushJSON the message includes the 'expensify.sms' domain). We are only formatting the number for the message for the optimisticData here. The data we get when we open the chat also looks like this: Screenshot 2023-04-04 at 13 12 27
Correct me if I'm wrong but it seems that the message is stored that way in the backend. So if it should be fixed on the frontend side I need to create another function to format these messages correctly using the formatPhoneNumber util.

@koko57 We should format the text right before it's displayed in addition to formatting it in the optimisticData

I've added formatPhoneNumberInText function that takes care of that
Screenshot 2023-04-05 at 11 42 13

@puneetlath puneetlath requested review from puneetlath and rushatgabhane and removed request for ArekChr April 6, 2023 16:51
@puneetlath
Copy link
Contributor

@rushatgabhane can you take the first round of review here?

@rushatgabhane
Copy link
Member

rushatgabhane commented Apr 6, 2023

@puneetlath sure, thanks for assigning me!

@Prince-Mendiratta
Copy link
Contributor

Prince-Mendiratta commented Apr 15, 2023

Hi folks, I was working on a related issue, #17461 and found out a pretty substantial bug that would've been introduced with this PR.

Right now, we are using awesome-phonenumber version 5.3.0. This was the latest version when @koko57 started working on this PR (March 31).

On April 4, a new version of awesome-phonenumber was released, which patches a bug that parsed incorrect numbers as a possible number grantila/awesome-phonenumber#98. For an instance of this bug, the number +13313212 is an invalid number. However, when this number is parsed using v5.3.0, it is returned as a possible number, which affects our conditions here. This is not an issue with 5.3.1. Here are the outputs for comparison:

v5.3.0

{
  number: {
    input: '+13313212',
    international: '+1 3313212',
    national: '331-3212',
    e164: '+13313212',
    rfc3966: 'tel:+1-3313212',
    significant: '3313212'
  },
  regionCode: 'US',
  valid: false,
  possible: true,
  canBeInternationallyDialled: true,
  type: 'unknown',
  possibility: 'is-possible',
  typeIsMobile: false,
  typeIsFixedLine: false,
  countryCode: 1
}

v5.3.1

{
  number: {
    input: '+13313212',
    international: '+1 3313212',
    national: '331-3212',
    e164: '+13313212',
    rfc3966: 'tel:+1-3313212',
    significant: '3313212'
  },
  regionCode: 'US',
  valid: false,
  possible: false,
  canBeInternationallyDialled: true,
  possibility: 'invalid',
  type: 'unknown',
  typeIsMobile: false,
  typeIsFixedLine: false,
  countryCode: 1
}

I'd suggest to upgrade the lib to v5.3.1 before this is merged.

@koko57
Copy link
Contributor Author

koko57 commented Apr 17, 2023

upgraded to the latest one - 5.4.0

@koko57 koko57 marked this pull request as draft April 17, 2023 07:28
@koko57 koko57 marked this pull request as ready for review April 17, 2023 07:55
@puneetlath
Copy link
Contributor

Should this be formatted? Or is this something we're handling in follow-up?

image

image

@koko57
Copy link
Contributor Author

koko57 commented Apr 17, 2023

Should this be formatted? Or is this something we're handling in follow-up?

image image

@puneetlath I think display names formatting should be handled in the follow-up issue

puneetlath
puneetlath previously approved these changes Apr 17, 2023
Copy link
Contributor

@puneetlath puneetlath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. @rushatgabhane are you good with everything as well?

Copy link
Contributor

@puneetlath puneetlath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good to me. @rushatgabhane I'd like to make sure we test again before merging though, since a lot of things are touched here. Let's try to do this today if we can to avoid more conflicts. Thanks!

@rushatgabhane
Copy link
Member

rushatgabhane commented Apr 18, 2023

WIP

Web

Sidebar
image

Login stuff
image

image image image image image image image image image image

iOS

image image image image

Android

WhatsApp Image 2023-04-18 at 22 56 24

mWeb Safari

image image

mWeb Chrome

image image

Desktop

image image image image

Copy link
Member

@rushatgabhane rushatgabhane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puneetlath LGTM!

@puneetlath puneetlath merged commit 509664a into Expensify:main Apr 18, 2023
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/puneetlath in version: 1.3.2-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.2-5 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@@ -85,7 +85,7 @@ class CloseAccountPage extends Component {
}

render() {
const userEmailOrPhone = Str.removeSMSDomain(this.props.session.email);
const userEmailOrPhone = this.props.formatPhoneNumber(this.props.session.email);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR caused regression - #17976.
This change should have bee applied to validation as well:

    validate(values) {
        const userEmailOrPhone = Str.removeSMSDomain(this.props.session.email);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah crap, nice catch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for commenting!

@fedirjh
Copy link
Contributor

fedirjh commented Aug 17, 2023

Hey there, coming from #23724, this PR should have handled the formatting inside the report participants page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants