-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathmanage_space_page.tsx
410 lines (353 loc) · 10.8 KB
/
manage_space_page.tsx
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import {
EuiButton,
EuiButtonEmpty,
EuiFlexGroup,
EuiFlexItem,
EuiLoadingSpinner,
EuiPageContentBody,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import _ from 'lodash';
import React, { Component, Fragment } from 'react';
import { Capabilities, HttpStart, NotificationsStart } from 'src/core/public';
import { Feature } from '../../../../features/public';
import { isReservedSpace } from '../../../common';
import { Space } from '../../../common/model/space';
import { SpacesManager } from '../../spaces_manager';
import { SecureSpaceMessage, UnauthorizedPrompt } from '../components';
import { toSpaceIdentifier } from '../lib';
import { SpaceValidator } from '../lib/validate_space';
import { ConfirmAlterActiveSpaceModal } from './confirm_alter_active_space_modal';
import { CustomizeSpace } from './customize_space';
import { DeleteSpacesButton } from './delete_spaces_button';
import { EnabledFeatures } from './enabled_features';
import { ReservedSpaceBadge } from './reserved_space_badge';
interface Props {
http: HttpStart;
notifications: NotificationsStart;
spacesManager: SpacesManager;
spaceId?: string;
onLoadSpace?: (space: Space) => void;
capabilities: Capabilities;
securityEnabled: boolean;
}
interface State {
space: Partial<Space>;
features: Feature[];
originalSpace?: Partial<Space>;
showAlteringActiveSpaceDialog: boolean;
isLoading: boolean;
saveInProgress: boolean;
formError?: {
isInvalid: boolean;
error?: string;
};
}
export class ManageSpacePage extends Component<Props, State> {
private readonly validator: SpaceValidator;
constructor(props: Props) {
super(props);
this.validator = new SpaceValidator({ shouldValidate: false });
this.state = {
isLoading: true,
showAlteringActiveSpaceDialog: false,
saveInProgress: false,
space: {},
features: [],
};
}
public async componentDidMount() {
if (!this.props.capabilities.spaces.manage) {
return;
}
const { spaceId, http } = this.props;
const getFeatures = http.get('/api/features');
if (spaceId) {
await this.loadSpace(spaceId, getFeatures);
} else {
const features = await getFeatures;
this.setState({ isLoading: false, features });
}
}
public async componentDidUpdate(previousProps: Props) {
if (this.props.spaceId !== previousProps.spaceId && this.props.spaceId) {
await this.loadSpace(this.props.spaceId, Promise.resolve(this.state.features));
}
}
public render() {
const content = this.state.isLoading ? this.getLoadingIndicator() : this.getForm();
return (
<Fragment>
<EuiPageContentBody>{content}</EuiPageContentBody>
{this.maybeGetSecureSpacesMessage()}
</Fragment>
);
}
public getLoadingIndicator = () => (
<div>
<EuiLoadingSpinner size={'xl'} />{' '}
<EuiTitle>
<h1>Loading...</h1>
</EuiTitle>
</div>
);
public getForm = () => {
if (!this.props.capabilities.spaces.manage) {
return <UnauthorizedPrompt />;
}
const { showAlteringActiveSpaceDialog } = this.state;
return (
<div data-test-subj="spaces-edit-page">
{this.getFormHeading()}
<EuiSpacer size={'s'} />
<EuiText size="s">
<FormattedMessage
id="xpack.spaces.management.manageSpacePage.manageDescription"
defaultMessage="Organize your saved objects into meaningful categories."
/>
</EuiText>
<EuiSpacer />
<CustomizeSpace
space={this.state.space}
onChange={this.onSpaceChange}
editingExistingSpace={this.editingExistingSpace()}
validator={this.validator}
/>
<EuiSpacer />
<EnabledFeatures
space={this.state.space}
features={this.state.features}
onChange={this.onSpaceChange}
securityEnabled={this.props.securityEnabled}
/>
<EuiSpacer />
{this.getFormButtons()}
{showAlteringActiveSpaceDialog && (
<ConfirmAlterActiveSpaceModal
onConfirm={() => this.performSave(true)}
onCancel={() => {
this.setState({ showAlteringActiveSpaceDialog: false });
}}
/>
)}
</div>
);
};
public getFormHeading = () => (
<EuiTitle size="m">
<h1>
{this.getTitle()} <ReservedSpaceBadge space={this.state.space as Space} />
</h1>
</EuiTitle>
);
public getTitle = () => {
if (this.editingExistingSpace()) {
return `Edit space`;
}
return (
<FormattedMessage
id="xpack.spaces.management.manageSpacePage.createSpaceTitle"
defaultMessage="Create a space"
/>
);
};
public maybeGetSecureSpacesMessage = () => {
if (this.editingExistingSpace() && this.props.securityEnabled) {
return <SecureSpaceMessage />;
}
return null;
};
public getFormButtons = () => {
const createSpaceText = i18n.translate(
'xpack.spaces.management.manageSpacePage.createSpaceButton',
{
defaultMessage: 'Create space',
}
);
const updateSpaceText = i18n.translate(
'xpack.spaces.management.manageSpacePage.updateSpaceButton',
{
defaultMessage: 'Update space',
}
);
const cancelButtonText = i18n.translate(
'xpack.spaces.management.manageSpacePage.cancelSpaceButton',
{
defaultMessage: 'Cancel',
}
);
const saveText = this.editingExistingSpace() ? updateSpaceText : createSpaceText;
return (
<EuiFlexGroup responsive={false}>
<EuiFlexItem grow={false}>
<EuiButton
fill
onClick={this.saveSpace}
data-test-subj="save-space-button"
isLoading={this.state.saveInProgress}
>
{saveText}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButtonEmpty onClick={this.backToSpacesList} data-test-subj="cancel-space-button">
{cancelButtonText}
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem grow={true} />
{this.getActionButton()}
</EuiFlexGroup>
);
};
public getActionButton = () => {
if (this.state.space && this.editingExistingSpace() && !isReservedSpace(this.state.space)) {
return (
<EuiFlexItem grow={false}>
<DeleteSpacesButton
data-test-subj="delete-space-button"
space={this.state.space as Space}
spacesManager={this.props.spacesManager}
onDelete={this.backToSpacesList}
notifications={this.props.notifications}
/>
</EuiFlexItem>
);
}
return null;
};
public onSpaceChange = (updatedSpace: Partial<Space>) => {
this.setState({
space: updatedSpace,
});
};
public saveSpace = () => {
this.validator.enableValidation();
const result = this.validator.validateForSave(this.state.space as Space);
if (result.isInvalid) {
this.setState({
formError: result,
});
return;
}
if (this.editingExistingSpace()) {
const { spacesManager } = this.props;
const originalSpace: Space = this.state.originalSpace as Space;
const space: Space = this.state.space as Space;
spacesManager.getActiveSpace().then(activeSpace => {
const editingActiveSpace = activeSpace.id === originalSpace.id;
const haveDisabledFeaturesChanged =
space.disabledFeatures.length !== originalSpace.disabledFeatures.length ||
_.difference(space.disabledFeatures, originalSpace.disabledFeatures).length > 0;
if (editingActiveSpace && haveDisabledFeaturesChanged) {
this.setState({
showAlteringActiveSpaceDialog: true,
});
return;
}
this.performSave();
});
} else {
this.performSave();
}
};
private loadSpace = async (spaceId: string, featuresPromise: Promise<Feature[]>) => {
const { spacesManager, onLoadSpace } = this.props;
try {
const [space, features] = await Promise.all([
spacesManager.getSpace(spaceId),
featuresPromise,
]);
if (space) {
if (onLoadSpace) {
onLoadSpace(space);
}
this.setState({
space,
features: await features,
originalSpace: space,
isLoading: false,
});
}
} catch (error) {
const message = error?.body?.message ?? '';
this.props.notifications.toasts.addDanger(
i18n.translate('xpack.spaces.management.manageSpacePage.errorLoadingSpaceTitle', {
defaultMessage: 'Error loading space: {message}',
values: { message },
})
);
this.backToSpacesList();
}
};
private performSave = (requireRefresh = false) => {
if (!this.state.space) {
return;
}
const name = this.state.space.name || '';
const {
id = toSpaceIdentifier(name),
description,
initials,
color,
disabledFeatures = [],
imageUrl,
} = this.state.space;
const params = {
name,
id,
description,
initials,
color,
disabledFeatures,
imageUrl,
};
let action;
if (this.editingExistingSpace()) {
action = this.props.spacesManager.updateSpace(params);
} else {
action = this.props.spacesManager.createSpace(params);
}
this.setState({ saveInProgress: true });
action
.then(() => {
this.props.notifications.toasts.addSuccess(
i18n.translate(
'xpack.spaces.management.manageSpacePage.spaceSuccessfullySavedNotificationMessage',
{
defaultMessage: `Space {name} was saved.`,
values: { name: `'${name}'` },
}
)
);
window.location.hash = `#/management/kibana/spaces`;
if (requireRefresh) {
setTimeout(() => {
window.location.reload();
});
}
})
.catch(error => {
const message = error?.body?.message ?? '';
this.setState({ saveInProgress: false });
this.props.notifications.toasts.addDanger(
i18n.translate('xpack.spaces.management.manageSpacePage.errorSavingSpaceTitle', {
defaultMessage: 'Error saving space: {message}',
values: { message },
})
);
});
};
private backToSpacesList = () => {
window.location.hash = `#/management/kibana/spaces`;
};
private editingExistingSpace = () => !!this.props.spaceId;
}