-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionItem.js
195 lines (178 loc) · 7.79 KB
/
ReportActionItem.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import _ from 'underscore';
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import CONST from '../../../CONST';
import ONYXKEYS from '../../../ONYXKEYS';
import ReportActionPropTypes from './ReportActionPropTypes';
import {
getReportActionItemStyle,
} from '../../../styles/getReportActionItemStyles';
import PressableWithSecondaryInteraction from '../../../components/PressableWithSecondaryInteraction';
import Hoverable from '../../../components/Hoverable';
import ReportActionItemSingle from './ReportActionItemSingle';
import ReportActionItemGrouped from './ReportActionItemGrouped';
import IOUAction from '../../../components/ReportActionItem/IOUAction';
import ReportActionItemMessage from './ReportActionItemMessage';
import UnreadActionIndicator from '../../../components/UnreadActionIndicator';
import ReportActionItemMessageEdit from './ReportActionItemMessageEdit';
import compose from '../../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import ControlSelection from '../../../libs/ControlSelection';
import canUseTouchScreen from '../../../libs/canUseTouchscreen';
import MiniReportActionContextMenu from './ContextMenu/MiniReportActionContextMenu';
import {isActiveReportAction, showContextMenu} from './ContextMenu/ReportActionContextMenu';
const propTypes = {
/** The ID of the report this action is on. */
reportID: PropTypes.number.isRequired,
/** All the data of the action item */
action: PropTypes.shape(ReportActionPropTypes).isRequired,
/** Should the comment have the appearance of being grouped with the previous comment? */
displayAsGroup: PropTypes.bool.isRequired,
/** Is this the most recent IOU Action? */
isMostRecentIOUReportAction: PropTypes.bool.isRequired,
/** Whether there is an outstanding amount in IOU */
hasOutstandingIOU: PropTypes.bool,
/** Should we display the new indicator on top of the comment? */
shouldDisplayNewIndicator: PropTypes.bool.isRequired,
/** Position index of the report action in the overall report FlatList view */
index: PropTypes.number.isRequired,
/** Draft message - if this is set the comment is in 'edit' mode */
draftMessage: PropTypes.string,
...withLocalizePropTypes,
...windowDimensionsPropTypes,
};
const defaultProps = {
draftMessage: '',
hasOutstandingIOU: false,
};
class ReportActionItem extends Component {
constructor(props) {
super(props);
this.popoverAnchor = undefined;
this.showPopover = this.showPopover.bind(this);
this.state = {
isContextMenuActive: isActiveReportAction(props.action.reportActionID),
};
}
shouldComponentUpdate(nextProps) {
return this.props.displayAsGroup !== nextProps.displayAsGroup
|| this.props.draftMessage !== nextProps.draftMessage
|| this.props.isMostRecentIOUReportAction !== nextProps.isMostRecentIOUReportAction
|| this.props.hasOutstandingIOU !== nextProps.hasOutstandingIOU
|| this.props.shouldDisplayNewIndicator !== nextProps.shouldDisplayNewIndicator
|| !_.isEqual(this.props.action, nextProps.action);
}
/**
* Show the ReportActionContextMenu modal popover.
*
* @param {Object} [event] - A press event.
* @param {string} [selection] - A copy text.
*/
showPopover(event, selection) {
// Block menu on the message being Edited
if (this.props.draftMessage) {
return;
}
showContextMenu(
event,
selection,
this.popoverAnchor,
this.props.reportID,
this.props.action,
this.props.draftMessage,
() => {
// Update to hide the Mini menu when PopoverMenu is shown
this.setState({isContextMenuActive: isActiveReportAction(this.props.action.reportActionID)});
},
);
}
render() {
let children;
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
children = (
<IOUAction
chatReportID={this.props.reportID}
action={this.props.action}
isMostRecentIOUReportAction={this.props.isMostRecentIOUReportAction}
/>
);
} else {
children = !this.props.draftMessage
? <ReportActionItemMessage action={this.props.action} />
: (
<ReportActionItemMessageEdit
action={this.props.action}
draftMessage={this.props.draftMessage}
reportID={this.props.reportID}
index={this.props.index}
/>
);
}
return (
<PressableWithSecondaryInteraction
ref={el => this.popoverAnchor = el}
onPressIn={() => this.props.isSmallScreenWidth && canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onSecondaryInteraction={this.showPopover}
preventDefaultContentMenu={!this.props.draftMessage}
>
<Hoverable resetsOnClickOutside={false}>
{hovered => (
<View>
{this.props.shouldDisplayNewIndicator && (
<UnreadActionIndicator />
)}
<View
style={getReportActionItemStyle(
hovered
|| this.state.isContextMenuActive
|| this.props.draftMessage,
)}
>
{!this.props.displayAsGroup
? (
<ReportActionItemSingle action={this.props.action}>
{children}
</ReportActionItemSingle>
)
: (
<ReportActionItemGrouped>
{children}
</ReportActionItemGrouped>
)}
</View>
<MiniReportActionContextMenu
reportID={this.props.reportID}
reportAction={this.props.action}
displayAsGroup={this.props.displayAsGroup}
isVisible={
hovered
&& !this.state.isContextMenuActive
&& !this.props.draftMessage
}
draftMessage={this.props.draftMessage}
/>
</View>
)}
</Hoverable>
</PressableWithSecondaryInteraction>
);
}
}
ReportActionItem.propTypes = propTypes;
ReportActionItem.defaultProps = defaultProps;
export default compose(
withWindowDimensions,
withLocalize,
withOnyx({
draftMessage: {
key: ({
reportID,
action,
}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}_${action.reportActionID}`,
},
}),
)(ReportActionItem);