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

[TS migration] Migrate 'User.js' lib to TypeScript #28695

Merged

Conversation

fvlvte
Copy link
Contributor

@fvlvte fvlvte commented Oct 3, 2023

Details

Migrated 'User.js' lib to TypeScript.

Fixed Issues

$ #24924
PROPOSAL: N/A

Tests

  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

  • Verify that no errors appear in the JS console
  • Sign in to the account
  • Request secondary auth code
  • Verify that You received two auth codes on e-mail
  • Enter sign in code from e-mail
  • Set custom status
  • Verify custom status is set

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: mWeb Chrome
    • iOS: Native
    • iOS: mWeb 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 the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is 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
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
      • 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 code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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 the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • 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

Android: Native
android-nt.mov
Android: mWeb Chrome
chrome-android.mov
iOS: Native
ios.mov
iOS: mWeb Safari
ios-web.mov
MacOS: Chrome / Safari
mac-web.mov
MacOS: Desktop
mac-desktop.mov

src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
@@ -512,7 +498,7 @@ function subscribeToUserEvents() {
previousUpdateID: Number(pushJSON.previousUpdateID || 0),
};
if (!OnyxUpdates.doesClientNeedToBeUpdated(Number(pushJSON.previousUpdateID || 0))) {
OnyxUpdates.apply(updates);
OnyxUpdates.apply(updates as any);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this type match?

Suggested change
OnyxUpdates.apply(updates as any);
OnyxUpdates.apply(updates as OnyxUpdate[]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

needs to be as unknown as OnyxUpdate so kinda ugly cast

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean updates a kind of as unknown as OnyxUpdate[]? Just to clarify:updates - is an array of OnyxUpdate's?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no type is not applicable to onyx update, that why it needs to be as unknown as OnyxUpdate

Copy link
Contributor

Choose a reason for hiding this comment

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

First, lets type updates object to be OnyxUpdatesFromServer.

Then, I think we can remove these two lines from apply(). They are not doing anything additional there and is causing issues here.

Copy link
Contributor

@rezkiy37 rezkiy37 left a comment

Choose a reason for hiding this comment

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

Left comments.

src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
@@ -2,9 +2,11 @@ import {OnyxUpdate} from 'react-native-onyx';
import Request from './Request';
import Response from './Response';

type OnyxServerUpdate = OnyxUpdate & {shouldNotify?: boolean};
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about this update, maybe it makes sense to update OnyxUpdate type in the onyx lib if we really need shouldNotify field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fabioh8010 what do You think about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@VickyStash If I remember correctly it was me that suggested to create this separate type. The reason for that is because the OnyxUpdate from Onyx itself doesn't have shouldNotify, but Expensify backend often sends Onyx updates with this property together, so that's why I advised @fvlvte to create a separate type.

src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
status: status.text,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you change from status, to status: status.text,?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type mismatch, we use status as text in personal details list and CustomStatus is an object composed of text, emojiCode, expiresAt (according to JSDoc).

Copy link
Contributor

Choose a reason for hiding this comment

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

This could cause regression, our type maybe was wrong? Either way let's test it @fvlvte

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've aligned types in Onyx, and aligned usage in Sidebar component.
It was mistyped in PersonalDetails and misused in Sidebar component as object treated as string.
I think this feature was bugged originally, now seems working fine.

src/libs/actions/User.ts Outdated Show resolved Hide resolved
@@ -2,9 +2,11 @@ import {OnyxUpdate} from 'react-native-onyx';
import Request from './Request';
import Response from './Response';

type OnyxServerUpdate = OnyxUpdate & {shouldNotify?: boolean};
Copy link
Contributor

Choose a reason for hiding this comment

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

@VickyStash If I remember correctly it was me that suggested to create this separate type. The reason for that is because the OnyxUpdate from Onyx itself doesn't have shouldNotify, but Expensify backend often sends Onyx updates with this property together, so that's why I advised @fvlvte to create a separate type.

@fvlvte fvlvte changed the title [No QA][TS migration] Migrate 'User.js' lib to TypeScript [TS migration] Migrate 'User.js' lib to TypeScript Nov 15, 2023
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
status: status.text,
Copy link
Contributor

Choose a reason for hiding this comment

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

This could cause regression, our type maybe was wrong? Either way let's test it @fvlvte

src/libs/actions/User.ts Outdated Show resolved Hide resolved
@blazejkustra
Copy link
Contributor

Ops @fvlvte @fabioh8010 😅
image

@fvlvte
Copy link
Contributor Author

fvlvte commented Dec 8, 2023

Ye, reverting it right now

@fvlvte fvlvte force-pushed the 24924-migrate-user.js-lib-to-typescript branch from 85dafb7 to 4edce16 Compare December 8, 2023 13:45
@fvlvte
Copy link
Contributor Author

fvlvte commented Dec 12, 2023

@blazejkustra ready to re-review, answered and addressed to all comments i guess and fixed this custom status in sidebar :P

@jjcoffee
Copy link
Contributor

jjcoffee commented Jan 2, 2024

@fvlvte I think we're still waiting for updated QA steps per your comment? Also would be good if you could upload the test videos. Thanks!

@fvlvte
Copy link
Contributor Author

fvlvte commented Jan 2, 2024

Yes, im working on new steps and videos right now.

@fvlvte
Copy link
Contributor Author

fvlvte commented Jan 2, 2024

Steps added and videos updated.

Copy link
Contributor

@jjcoffee jjcoffee left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

melvin-bot bot commented Jan 2, 2024

We did not find an internal engineer to review this PR, trying to assign a random engineer to #24924 as well as to this PR... Please reach out for help on Slack if no one gets assigned!

@jjcoffee
Copy link
Contributor

jjcoffee commented Jan 2, 2024

PR review checklist updated!

src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
},
];

type CloseAccountParams = {message: string};
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB necessarily for this PR, but maybe worth a discussion ... this feels like it could be better. I'm not entirely sure what pattern we've followed so far, but I feel like the best pattern to follow would be to basically have a centralized APIParamMap somewhere that define the params accepted by each API endpoint, and which ensures that the parameters passed to a specific endpoint match the type defined for that endpoint.

cc @fabioh8010 @blazejkustra what do you think? Long-term this could lend itself to some compile-time validation across the front-end and the API that ensures that the params expected by each API endpoint (defined in the API layer) are provided by the front-end (verified via TypeScript)

Copy link
Contributor

Choose a reason for hiding this comment

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

Great thinking @roryabraham! 🙌

Actually this is our plan with @fabioh8010 to have a central point with an object with command names and params. We want to implement it in a separate PR once all libs are migrated to Typescript.

More context: That's why we decided to have these Params types defined in the bodies of action functions, so it is easier to extract in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, let's make an issue and start now and do the migration incrementally? Not going to block on this for this PR but let's discuss in slack.

src/libs/actions/User.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
*/
function clearDraftCustomStatus() {
Onyx.merge(ONYXKEYS.CUSTOM_STATUS_DRAFT, {text: '', emojiCode: '', clearAfter: '', customDateTemporary: ''});
Onyx.merge(ONYXKEYS.CUSTOM_STATUS_DRAFT, {text: '', emojiCode: '', clearAfter: ''});
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was customDateTemporary removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not used anywhere and it's missing on our Onyx type, so removed it to get rid of type errors.

@fvlvte
Copy link
Contributor Author

fvlvte commented Jan 3, 2024

@roryabraham - PR updated and answered.

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

@fvlvte I think this will cause lint errors on main because we just merged #32475

Please merge main into your branch and fix lint. Thanks!

@fvlvte
Copy link
Contributor Author

fvlvte commented Jan 4, 2024

Done @roryabraham

@roryabraham roryabraham merged commit 0c5a2cb into Expensify:main Jan 9, 2024
15 checks passed
@OSBotify
Copy link
Contributor

OSBotify commented Jan 9, 2024

✋ 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

OSBotify commented Jan 9, 2024

🚀 Deployed to staging by https://github.com/roryabraham in version: 1.4.23-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/thienlnam in version: 1.4.23-4 🚀

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

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.

9 participants