Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spaces] - Handle space renaming and deleting #22586

Merged
merged 7 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions x-pack/plugins/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export const spaces = (kibana) => new kibana.Plugin({
hacks: [],
mappings,
home: ['plugins/spaces/register_feature'],
injectDefaultVars: function () {
injectDefaultVars: function (server) {
return {
spaces: [],
activeSpace: null
activeSpace: null,
rootBasePath: server.config().get('server.basePath'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about renaming this to "spaceSelectorURL" or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea -- it better reflects the intent/usage of this variable

};
},
replaceInjectedVars: async function (vars, request, server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { SpacesManager } from './spaces_manager';
export { SpacesManager } from './spaces_manager';
8 changes: 7 additions & 1 deletion x-pack/plugins/spaces/public/lib/spaces_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { Space } from '../../common/model/space';
export class SpacesManager extends EventEmitter {
private httpAgent: any;
private baseUrl: any;
private rootBasePath: string;

constructor(httpAgent: any, chrome: any) {
constructor(httpAgent: any, chrome: any, rootBasePath: string) {
super();
this.httpAgent = httpAgent;
this.baseUrl = chrome.addBasePath(`/api/spaces/v1`);
this.rootBasePath = rootBasePath;
}

public async getSpaces(): Promise<Space[]> {
Expand Down Expand Up @@ -54,6 +56,10 @@ export class SpacesManager extends EventEmitter {
.catch(() => this._displayError());
}

public redirectToSpaceSelector() {
window.location.href = this.rootBasePath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.rootBasePath can be "" and in that situation, this isn't working, we wanna be using "/" instead.

}

public async requestRefresh() {
this.emit('request_refresh');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

// @ts-ignore
import { EuiConfirmModal, EuiOverlayMask } from '@elastic/eui';
import React from 'react';
import PropTypes from 'prop-types';
import { Space } from '../../../../common/model/space';

import {
EuiOverlayMask,
EuiConfirmModal,
} from '@elastic/eui';
interface Props {
spaces: Space[];
onCancel: () => void;
onConfirm: () => void;
}

export const ConfirmDeleteModal = (props) => {
const {
spaces
} = props;
export const ConfirmDeleteModal = (props: Props) => {
const { spaces } = props;

const buttonText = spaces.length > 1
? `Delete ${spaces.length} spaces`
: `Delete space`;
const buttonText = spaces.length > 1 ? `Delete ${spaces.length} spaces` : `Delete space`;

const bodyQuestion = spaces.length > 1
? `Are you sure you want to delete these ${spaces.length} spaces?`
: `Are you sure you want to delete this space?`;
const bodyQuestion =
spaces.length > 1
? `Are you sure you want to delete these ${spaces.length} spaces?`
: `Are you sure you want to delete this space?`;

return (
<EuiOverlayMask>
Expand All @@ -42,9 +42,3 @@ export const ConfirmDeleteModal = (props) => {
</EuiOverlayMask>
);
};

ConfirmDeleteModal.propTypes = {
spaces: PropTypes.array.isRequired,
onCancel: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.
*/

// @ts-ignore
import { EuiConfirmModal, EuiOverlayMask } from '@elastic/eui';
import React from 'react';
import { Space } from '../../../../common/model/space';

interface Props {
space?: Space;
onCancel: () => void;
onConfirm: () => void;
}

export const ConfirmRedirectModal = (props: Props) => {
const { space } = props;

const buttonText = `Delete current space`;

const bodyQuestion =
`You are about to delete your current space ` +
`(${space.name}). If you continue, you will be redirected to select a different space.`;

return (
<EuiOverlayMask>
<EuiConfirmModal
buttonColor={'danger'}
cancelButtonText={'Cancel'}
confirmButtonText={buttonText}
onCancel={props.onCancel}
onConfirm={props.onConfirm}
title={`Delete your current space?`}
defaultFocusedButton={'cancel'}
>
<p>{bodyQuestion}</p>
</EuiConfirmModal>
</EuiOverlayMask>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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 } from '@elastic/eui';
import React, { Component, Fragment } from 'react';
import { toastNotifications } from 'ui/notify';
import { Space } from '../../../../common/model/space';
import { SpacesManager } from '../../../lib/spaces_manager';
import { ConfirmDeleteModal } from './confirm_delete_modal';
import { ConfirmRedirectModal } from './confirm_redirect_modal';

interface Props {
spaces: Space[];
spacesManager: SpacesManager;
spacesNavState: any;
onDelete: () => void;
}

interface State {
showConfirmDeleteModal: boolean;
showConfirmRedirectModal: boolean;
}

export class DeleteSpacesButton extends Component<Props, State> {
public state = {
showConfirmDeleteModal: false,
showConfirmRedirectModal: false,
};

public render() {
const numSpaces = this.props.spaces.length;

const buttonText = numSpaces > 1 ? `Delete ${numSpaces} spaces` : `Delete space`;

return (
<Fragment>
<EuiButton color={'danger'} onClick={this.onDeleteClick}>
{buttonText}
</EuiButton>
{this.getConfirmDeleteModal()}
{this.getConfirmRedirectModal()}
</Fragment>
);
}

public onDeleteClick = () => {
this.setState({
showConfirmDeleteModal: true,
});
};

public getConfirmDeleteModal = () => {
if (!this.state.showConfirmDeleteModal) {
return null;
}

const { spaces, spacesNavState } = this.props;

const isDeletingCurrentSpace = !!this.locateCurrentSpace();

const performDelete = () => {
this.deleteSpaces(() => {
this.setState({
showConfirmDeleteModal: false,
});

const message =
spaces.length > 1
? `Deleted ${spaces.length} spaces.`
: `Deleted "${spaces[0].name}" space.`;

toastNotifications.addSuccess(message);

if (this.props.onDelete) {
this.props.onDelete();
}

spacesNavState.refreshSpacesList();
});
};

const nextStep = isDeletingCurrentSpace ? this.showConfirmRedirectModal : performDelete;

return (
<ConfirmDeleteModal
spaces={this.props.spaces}
onCancel={() => {
this.setState({
showConfirmDeleteModal: false,
});
}}
onConfirm={nextStep}
/>
);
};

public showConfirmRedirectModal = () => {
this.setState({
showConfirmDeleteModal: false,
showConfirmRedirectModal: true,
});
};

public getConfirmRedirectModal = () => {
if (!this.state.showConfirmRedirectModal) {
return null;
}

const performDelete = () => {
this.deleteSpaces(() => {
this.props.spacesManager.redirectToSpaceSelector();
});
};

return (
<ConfirmRedirectModal
space={this.locateCurrentSpace()}
onCancel={() => {
this.setState({
showConfirmRedirectModal: false,
});
}}
onConfirm={performDelete}
/>
);
};

public deleteSpaces = (onComplete: () => void) => {
const { spacesManager, spaces } = this.props;

const deleteOperations = spaces.map(space => spacesManager.deleteSpace(space));

Promise.all(deleteOperations)
.then(onComplete)
.catch(error => {
const { message = '' } = error.data || {};

toastNotifications.addDanger(`Error deleting space: ${message}`);
});
};

private locateCurrentSpace = () => {
return this.props.spaces.find(
space => space.id === this.props.spacesNavState.getActiveSpace().id
);
};
}
Loading