forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team_selector_modal.jsx
280 lines (248 loc) · 9.25 KB
/
team_selector_modal.jsx
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import PropTypes from 'prop-types';
import React from 'react';
import {Modal} from 'react-bootstrap';
import {FormattedMessage} from 'react-intl';
import Constants, {ModalIdentifiers} from 'utils/constants.jsx';
import {localizeMessage} from 'utils/utils.jsx';
import MultiSelect from 'components/multiselect/multiselect.jsx';
import ConfirmModal from 'components/confirm_modal.jsx';
import TeamIcon from 'components/widgets/team_icon/team_icon';
import FormattedMarkdownMessage from 'components/formatted_markdown_message.jsx';
import {imageURLForTeam} from 'utils/utils';
const TEAMS_PER_PAGE = 50;
export default class TeamSelectorModal extends React.Component {
static propTypes = {
currentSchemeId: PropTypes.string,
alreadySelected: PropTypes.array,
searchTerm: PropTypes.string.isRequired,
teams: PropTypes.array.isRequired,
onModalDismissed: PropTypes.func,
onTeamsSelected: PropTypes.func,
modalID: PropTypes.string,
actions: PropTypes.shape({
loadTeams: PropTypes.func.isRequired,
setModalSearchTerm: PropTypes.func.isRequired,
searchTeams: PropTypes.func.isRequired,
}).isRequired,
}
constructor(props) {
super(props);
this.searchTimeoutId = 0;
this.state = {
values: [],
show: true,
search: false,
loadingTeams: true,
confirmAddModal: false,
confirmAddTeam: null,
};
}
componentDidMount() {
this.props.actions.loadTeams(0, TEAMS_PER_PAGE + 1).then(() => {
this.setTeamsLoadingState(false);
});
}
UNSAFE_componentWillReceiveProps(nextProps) { // eslint-disable-line camelcase
if (this.props.searchTerm !== nextProps.searchTerm) {
clearTimeout(this.searchTimeoutId);
const searchTerm = nextProps.searchTerm;
if (searchTerm === '') {
return;
}
this.searchTimeoutId = setTimeout(
async () => {
this.setTeamsLoadingState(true);
await this.props.actions.searchTeams(searchTerm);
this.setTeamsLoadingState(false);
},
Constants.SEARCH_TIMEOUT_MILLISECONDS
);
}
}
handleHide = () => {
this.props.actions.setModalSearchTerm('');
this.setState({show: false});
}
handleExit = () => {
if (this.props.onModalDismissed) {
this.props.onModalDismissed();
}
}
handleSubmit = (e) => {
if (e) {
e.preventDefault();
}
if (this.state.values.length === 0) {
return;
}
this.props.onTeamsSelected(this.state.values);
this.handleHide();
}
addValue = (value, confirmed = false) => {
if (this.props.modalID === ModalIdentifiers.ADD_TEAMS_TO_SCHEME && value.scheme_id !== null && value.scheme_id !== '' && !confirmed) {
this.setState({confirmAddModal: true, confirmAddTeam: value});
return;
}
const values = Object.assign([], this.state.values);
const teamIds = values.map((v) => v.id);
if (value && value.id && teamIds.indexOf(value.id) === -1) {
values.push(value);
}
this.setState({values, confirmAddModal: false, confirmAddTeam: null});
}
setTeamsLoadingState = (loadingState) => {
this.setState({
loadingTeams: loadingState,
});
}
handlePageChange = (page, prevPage) => {
if (page > prevPage) {
this.setTeamsLoadingState(true);
this.props.actions.loadTeams(page, TEAMS_PER_PAGE + 1).then(() => {
this.setTeamsLoadingState(false);
});
}
}
handleDelete = (values) => {
this.setState({values});
}
search = (term, multiselectComponent) => {
if (multiselectComponent.state.page !== 0) {
multiselectComponent.setState({page: 0});
}
this.props.actions.setModalSearchTerm(term);
}
renderOption(option, isSelected, onAdd) {
var rowSelected = '';
if (isSelected) {
rowSelected = 'more-modal__row--selected';
}
return (
<div
key={option.id}
ref={isSelected ? 'selected' : option.id}
className={'more-modal__row clickable ' + rowSelected}
onClick={() => onAdd(option)}
>
<div
className='more-modal__details'
>
<TeamIcon
name={option.display_name}
url={imageURLForTeam(option)}
/>
</div>
<div className='more-modal__actions'>
<div className='more-modal__actions--round'>
<i className='fa fa-plus'/>
</div>
</div>
</div>
);
}
renderValue(props) {
return props.data.display_name;
}
renderConfirmModal(show, team) {
const title = (
<FormattedMessage
id='add_teams_to_scheme.confirmation.title'
defaultMessage='Team Override Scheme Change?'
/>
);
const message = (
<FormattedMessage
id='add_teams_to_scheme.confirmation.message'
defaultMessage='This team is already selected in another team scheme, are you sure you want to move it to this team scheme?'
/>
);
const confirmButtonText = (
<FormattedMessage
id='add_teams_to_scheme.confirmation.accept'
defaultMessage='Yes, Move Team'
/>
);
return (
<ConfirmModal
show={show}
title={title}
message={message}
confirmButtonText={confirmButtonText}
onCancel={() => this.setState({confirmAddModal: false, confirmAddTeam: null})}
onConfirm={() => this.addValue(team, true)}
/>
);
}
render() {
const confirmModal = this.renderConfirmModal(this.state.confirmAddModal, this.state.confirmAddTeam);
const numRemainingText = (
<FormattedMessage
id='multiselect.selectTeams'
defaultMessage='Use ↑↓ to browse, ↵ to select.'
/>
);
const buttonSubmitText = localizeMessage('multiselect.add', 'Add');
let teams = [];
if (this.props.teams) {
teams = this.props.teams.filter((team) => team.delete_at === 0);
teams = teams.filter((team) => team.scheme_id !== this.currentSchemeId);
teams = teams.filter((team) => this.props.alreadySelected.indexOf(team.id) === -1);
teams.sort((a, b) => {
const aName = a.display_name.toUpperCase();
const bName = b.display_name.toUpperCase();
if (aName === bName) {
return 0;
}
if (aName > bName) {
return 1;
}
return -1;
});
}
return (
<Modal
dialogClassName='a11y__modal more-modal more-direct-channels team-selector-modal'
show={this.state.show}
onHide={this.handleHide}
onExited={this.handleExit}
role='dialog'
aria-labelledby='teamSelectorModalLabel'
>
<Modal.Header closeButton={true}>
<Modal.Title
componentClass='h1'
id='teamSelectorModalLabel'
>
<FormattedMarkdownMessage
id='add_teams_to_scheme.title'
defaultMessage='Add Teams To **Team Selection** List'
/>
</Modal.Title>
</Modal.Header>
<Modal.Body>
{confirmModal}
<MultiSelect
key='addTeamsToSchemeKey'
options={teams}
optionRenderer={this.renderOption}
values={this.state.values}
valueRenderer={this.renderValue}
perPage={TEAMS_PER_PAGE}
handlePageChange={this.handlePageChange}
handleInput={this.search}
handleDelete={this.handleDelete}
handleAdd={this.addValue}
handleSubmit={this.handleSubmit}
numRemainingText={numRemainingText}
buttonSubmitText={buttonSubmitText}
saving={false}
loading={this.state.loadingTeams}
placeholderText={localizeMessage('multiselect.addTeamsPlaceholder', 'Search and add teams')}
/>
</Modal.Body>
</Modal>
);
}
}