-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathwithCurrentUserPersonalDetails.js
75 lines (64 loc) · 2.58 KB
/
withCurrentUserPersonalDetails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, {useMemo} from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import ONYXKEYS from '../ONYXKEYS';
import personalDetailsPropType from '../pages/personalDetailsPropType';
const withCurrentUserPersonalDetailsPropTypes = {
currentUserPersonalDetails: personalDetailsPropType,
};
const withCurrentUserPersonalDetailsDefaultProps = {
currentUserPersonalDetails: {},
};
export default function (WrappedComponent) {
const propTypes = {
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]),
/** Personal details of all the users, including current user */
personalDetails: PropTypes.objectOf(personalDetailsPropType),
/** Session of the current user */
session: PropTypes.shape({
email: PropTypes.string,
accountID: PropTypes.number,
}),
};
const defaultProps = {
forwardedRef: undefined,
personalDetails: {},
session: {
email: '',
accountID: 0,
},
};
const WithCurrentUserPersonalDetails = (props) => {
const currentUserEmail = props.session.email;
const accountID = props.session.accountID;
const currentUserPersonalDetails = useMemo(() => ({...props.personalDetails[currentUserEmail], accountID}), [props.personalDetails, currentUserEmail, accountID]);
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={props.forwardedRef}
currentUserPersonalDetails={currentUserPersonalDetails}
/>
);
};
WithCurrentUserPersonalDetails.displayName = `WithCurrentUserPersonalDetails(${getComponentDisplayName(WrappedComponent)})`;
WithCurrentUserPersonalDetails.propTypes = propTypes;
WithCurrentUserPersonalDetails.defaultProps = defaultProps;
const withCurrentUserPersonalDetails = React.forwardRef((props, ref) => (
<WithCurrentUserPersonalDetails
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));
return withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
session: {
key: ONYXKEYS.SESSION,
},
})(withCurrentUserPersonalDetails);
}
export {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps};