-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
withReportOrNavigateHome.js
59 lines (51 loc) · 2.05 KB
/
withReportOrNavigateHome.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
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import getComponentDisplayName from '../../../libs/getComponentDisplayName';
import Navigation from '../../../libs/Navigation/Navigation';
import ONYXKEYS from '../../../ONYXKEYS';
import reportPropTypes from '../../reportPropTypes';
export default function (WrappedComponent) {
const propTypes = {
/** The HOC takes an optional ref as a prop and passes it as a ref to the wrapped component.
* That way, if a ref is passed to a component wrapped in the HOC, the ref is a reference to the wrapped component, not the HOC. */
forwardedRef: PropTypes.func,
/** The report currently being looked at */
report: reportPropTypes,
};
const defaultProps = {
forwardedRef: () => {},
report: {},
};
class WithReportOrNavigateHome extends Component {
componentDidMount() {
if (!_.isEmpty(this.props.report)) {
return;
}
Navigation.dismissModal();
}
render() {
const rest = _.omit(this.props, ['forwardedRef']);
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
ref={this.props.forwardedRef}
/>
);
}
}
WithReportOrNavigateHome.propTypes = propTypes;
WithReportOrNavigateHome.defaultProps = defaultProps;
WithReportOrNavigateHome.displayName = `withReportOrNavigateHome(${getComponentDisplayName(WrappedComponent)})`;
const withReportOrNavigateHome = React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<WithReportOrNavigateHome {...props} forwardedRef={ref} />
));
return withOnyx({
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID}`,
},
})(withReportOrNavigateHome);
}