-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[No QA] [TS migration] Migrate 'withCurrentUserPersonalDetails.js' HOC to TypeScript #29547
Merged
dangrous
merged 4 commits into
Expensify:main
from
VickyStash:ts-migration/withCurrentUserPersonalDetails-hoc
Oct 20, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d7ec0c
[TS migration] Migrate 'withCurrentUserPersonalDetails.js' HOC
VickyStash 5342f99
Update withCurrentUserPersonalDetails HOC typing
VickyStash 5d016ef
Use OnyxEntry for the onyx type
VickyStash 8ab35de
Merge branch 'main' into ts-migration/withCurrentUserPersonalDetails-hoc
VickyStash 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 was deleted.
Oops, something went wrong.
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,67 @@ | ||
import React, {ComponentType, RefAttributes, ForwardedRef, useMemo} from 'react'; | ||
import {OnyxEntry, withOnyx} from 'react-native-onyx'; | ||
import getComponentDisplayName from '../libs/getComponentDisplayName'; | ||
import ONYXKEYS from '../ONYXKEYS'; | ||
import personalDetailsPropType from '../pages/personalDetailsPropType'; | ||
import type {PersonalDetails, Session} from '../types/onyx'; | ||
|
||
type CurrentUserPersonalDetails = PersonalDetails | Record<string, never>; | ||
|
||
type OnyxProps = { | ||
/** Personal details of all the users, including current user */ | ||
personalDetails: OnyxEntry<Record<string, PersonalDetails>>; | ||
|
||
/** Session of the current user */ | ||
session: OnyxEntry<Session>; | ||
}; | ||
|
||
type HOCProps = { | ||
currentUserPersonalDetails: CurrentUserPersonalDetails; | ||
}; | ||
|
||
type ComponentProps = OnyxProps & HOCProps; | ||
|
||
// TODO: remove when all components that use it will be migrated to TS | ||
const withCurrentUserPersonalDetailsPropTypes = { | ||
currentUserPersonalDetails: personalDetailsPropType, | ||
}; | ||
|
||
const withCurrentUserPersonalDetailsDefaultProps: HOCProps = { | ||
currentUserPersonalDetails: {}, | ||
}; | ||
|
||
export default function <TProps extends ComponentProps, TRef>( | ||
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>, | ||
): ComponentType<Omit<Omit<TProps, keyof HOCProps> & RefAttributes<TRef>, keyof OnyxProps>> { | ||
function WithCurrentUserPersonalDetails(props: Omit<TProps, keyof HOCProps>, ref: ForwardedRef<TRef>) { | ||
const accountID = props.session?.accountID ?? 0; | ||
const accountPersonalDetails = props.personalDetails?.[accountID]; | ||
const currentUserPersonalDetails: CurrentUserPersonalDetails = useMemo( | ||
() => (accountPersonalDetails ? {...accountPersonalDetails, accountID} : {}), | ||
[accountPersonalDetails, accountID], | ||
); | ||
return ( | ||
<WrappedComponent | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...(props as TProps)} | ||
ref={ref} | ||
currentUserPersonalDetails={currentUserPersonalDetails} | ||
/> | ||
); | ||
} | ||
|
||
WithCurrentUserPersonalDetails.displayName = `WithCurrentUserPersonalDetails(${getComponentDisplayName(WrappedComponent)})`; | ||
|
||
const withCurrentUserPersonalDetails = React.forwardRef(WithCurrentUserPersonalDetails); | ||
|
||
return withOnyx<Omit<TProps, keyof HOCProps> & RefAttributes<TRef>, OnyxProps>({ | ||
VickyStash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
personalDetails: { | ||
key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
}, | ||
session: { | ||
key: ONYXKEYS.SESSION, | ||
}, | ||
})(withCurrentUserPersonalDetails); | ||
} | ||
|
||
export {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps}; |
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.
Just one question - can these be null (i.e. do they need the
?
s? Seems like they'll always be there, right? Not marked as optional in OnyxProps...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.
@dangrous Since
session
andpersonalDetails
are OnyxEntry, and OnyxEntry can be null, we need the?
s.