-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TaskAssigneeSelectorModal.js
225 lines (195 loc) · 8.21 KB
/
TaskAssigneeSelectorModal.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* eslint-disable es/no-optional-chaining */
import React, {useState, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import OptionsSelector from '../../components/OptionsSelector';
import * as OptionsListUtils from '../../libs/OptionsListUtils';
import ONYXKEYS from '../../ONYXKEYS';
import styles from '../../styles/styles';
import Navigation from '../../libs/Navigation/Navigation';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import ScreenWrapper from '../../components/ScreenWrapper';
import CONST from '../../CONST';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import personalDetailsPropType from '../personalDetailsPropType';
import reportPropTypes from '../reportPropTypes';
import ROUTES from '../../ROUTES';
import * as TaskUtils from '../../libs/actions/Task';
const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** All of the personal details for everyone */
personalDetails: personalDetailsPropType,
/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),
/** URL Route params */
route: PropTypes.shape({
/** Params from the URL path */
params: PropTypes.shape({
/** reportID passed via route: /r/:reportID/title */
reportID: PropTypes.string,
}),
}),
// /** The report currently being looked at */
// report: reportPropTypes.isRequired,
/** Current user session */
session: PropTypes.shape({
email: PropTypes.string.isRequired,
}),
/** Grab the Share destination of the Task */
task: PropTypes.shape({
/** Share destination of the Task */
shareDestination: PropTypes.string,
/** The task report if it's currently being edited */
report: reportPropTypes,
}),
...withLocalizePropTypes,
};
const defaultProps = {
betas: [],
personalDetails: {},
reports: {},
session: {},
route: {},
task: {},
};
function TaskAssigneeSelectorModal(props) {
const [searchValue, setSearchValue] = useState('');
const [headerMessage, setHeaderMessage] = useState('');
const [filteredRecentReports, setFilteredRecentReports] = useState([]);
const [filteredPersonalDetails, setFilteredPersonalDetails] = useState([]);
const [filteredUserToInvite, setFilteredUserToInvite] = useState(null);
const [filteredCurrentUserOption, setFilteredCurrentUserOption] = useState(null);
useEffect(() => {
const results = OptionsListUtils.getNewChatOptions(props.reports, props.personalDetails, props.betas, '', [], CONST.EXPENSIFY_EMAILS, false);
setFilteredRecentReports(results.recentReports);
setFilteredPersonalDetails(results.personalDetails);
setFilteredUserToInvite(results.userToInvite);
setFilteredCurrentUserOption(results.currentUserOption);
}, [props]);
useEffect(() => {
const {recentReports, personalDetails, userToInvite, currentUserOption} = OptionsListUtils.getNewChatOptions(
props.reports,
props.personalDetails,
props.betas,
searchValue.trim(),
[],
CONST.EXPENSIFY_EMAILS,
false,
);
setHeaderMessage(OptionsListUtils.getHeaderMessage(recentReports?.length + personalDetails?.length !== 0 || currentUserOption, Boolean(userToInvite), searchValue));
setFilteredUserToInvite(userToInvite);
setFilteredRecentReports(recentReports);
setFilteredPersonalDetails(personalDetails);
setFilteredCurrentUserOption(currentUserOption);
}, [props, searchValue]);
const onChangeText = (newSearchTerm = '') => {
setSearchValue(newSearchTerm);
};
const sections = useMemo(() => {
const sectionsList = [];
let indexOffset = 0;
if (filteredCurrentUserOption) {
sectionsList.push({
title: props.translate('newTaskPage.assignMe'),
data: [filteredCurrentUserOption],
shouldShow: true,
indexOffset,
});
indexOffset += 1;
}
sectionsList.push({
title: props.translate('common.recents'),
data: filteredRecentReports,
shouldShow: filteredRecentReports?.length > 0,
indexOffset,
});
indexOffset += filteredRecentReports?.length;
sectionsList.push({
title: props.translate('common.contacts'),
data: filteredPersonalDetails,
shouldShow: filteredPersonalDetails?.length > 0,
indexOffset,
});
indexOffset += filteredPersonalDetails?.length;
if (filteredUserToInvite) {
sectionsList.push({
data: [filteredUserToInvite],
shouldShow: true,
indexOffset,
});
}
return sectionsList;
}, [filteredCurrentUserOption, filteredPersonalDetails, filteredRecentReports, filteredUserToInvite, props]);
const selectReport = (option) => {
if (!option) {
return;
}
// Check to see if we're creating a new task
// If there's no route params, we're creating a new task
if (!props.route.params && option.accountID) {
// Clear out the state value, set the assignee and navigate back to the NewTaskPage
setSearchValue('');
TaskUtils.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option));
return Navigation.goBack();
}
// Check to see if we're editing a task and if so, update the assignee
if (props.route.params.reportID && props.task.report.reportID === props.route.params.reportID) {
// There was an issue where sometimes a new assignee didn't have a DM thread
// This would cause the app to crash, so we need to make sure we have a DM thread
TaskUtils.setAssigneeValue(option.login, option.accountID, props.task.shareDestination, OptionsListUtils.isCurrentUser(option));
// Pass through the selected assignee
TaskUtils.editTaskAndNavigate(props.task.report, props.session.email, props.session.accountID, {assignee: option.login, assigneeAccountID: option.accountID});
}
};
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
{({didScreenTransitionEnd, safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithBackButton
title={props.translate('newTaskPage.assignee')}
onBackButtonPress={() => Navigation.goBack(ROUTES.NEW_TASK)}
/>
<View style={[styles.flex1, styles.w100, styles.pRelative]}>
<OptionsSelector
sections={sections}
value={searchValue}
onSelectRow={selectReport}
onChangeText={onChangeText}
headerMessage={headerMessage}
showTitleTooltip
shouldShowOptions={didScreenTransitionEnd}
placeholderText={props.translate('optionsSelector.nameEmailOrPhoneNumber')}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
</View>
</>
)}
</ScreenWrapper>
);
}
TaskAssigneeSelectorModal.displayName = 'TaskAssigneeSelectorModal';
TaskAssigneeSelectorModal.propTypes = propTypes;
TaskAssigneeSelectorModal.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
betas: {
key: ONYXKEYS.BETAS,
},
task: {
key: ONYXKEYS.TASK,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(TaskAssigneeSelectorModal);