Skip to content

Commit

Permalink
fix auto closing new vis modal when navigating to lens or when naviga…
Browse files Browse the repository at this point in the history
…ting away with browser history (#56998) (#57092)

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
Dosant and elasticmachine authored Feb 7, 2020
1 parent 48551a5 commit cd1c04b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function initListingDirective(app) {
);
}

export function VisualizeListingController($injector, createNewVis) {
export function VisualizeListingController($injector, $scope, createNewVis) {
const {
addBasePath,
chrome,
Expand All @@ -59,7 +59,7 @@ export function VisualizeListingController($injector, createNewVis) {
this.savedObjects = savedObjects;

this.createNewVis = () => {
visualizations.showNewVisModal();
this.closeNewVisModal = visualizations.showNewVisModal();
};

this.editItem = ({ editUrl }) => {
Expand All @@ -73,7 +73,7 @@ export function VisualizeListingController($injector, createNewVis) {

if (createNewVis) {
// In case the user navigated to the page via the /visualize/new URL we start the dialog immediately
visualizations.showNewVisModal({
this.closeNewVisModal = visualizations.showNewVisModal({
onClose: () => {
// In case the user came via a URL to this page, change the URL to the regular landing page URL after closing the modal
kbnUrl.changePath(VisualizeConstants.LANDING_PAGE_PATH);
Expand Down Expand Up @@ -124,4 +124,10 @@ export function VisualizeListingController($injector, createNewVis) {
this.listingLimit = uiSettings.get('savedObjects:listingLimit');

addHelpMenuToAppChrome(chrome, docLinks);

$scope.$on('$destroy', () => {
if (this.closeNewVisModal) {
this.closeNewVisModal();
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ describe('NewVisModal', () => {
expect(window.location.assign).toBeCalledWith('testbasepath/aliasUrl?addToDashboard');
expect(onClose).toHaveBeenCalled();
});

it('closes and redirects properly if visualization with aliasUrl and without addToDashboard in editorParams', () => {
const onClose = jest.fn();
window.location.assign = jest.fn();
const wrapper = mountWithIntl(
<NewVisModal
isOpen={true}
onClose={onClose}
visTypesRegistry={visTypes}
editorParams={['foo=true', 'bar=42']}
addBasePath={addBasePath}
uiSettings={uiSettings}
savedObjects={{} as SavedObjectsStart}
/>
);
const visButton = wrapper.find('button[data-test-subj="visType-visWithAliasUrl"]');
visButton.simulate('click');
expect(window.location.assign).toBeCalledWith('testbasepath/aliasUrl');
expect(onClose).toHaveBeenCalled();
});
});

describe('filter for visualization types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class NewVisModal extends React.Component<TypeSelectionProps, TypeSelectionState
params = this.props.addBasePath(visType.aliasUrl);
if (this.props.editorParams && this.props.editorParams.includes('addToDashboard')) {
params = `${params}?addToDashboard`;
this.props.onClose();
}
this.props.onClose();
window.location.assign(params);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export interface ShowNewVisModalParams {

export function showNewVisModal({ editorParams = [], onClose }: ShowNewVisModalParams = {}) {
const container = document.createElement('div');
let isClosed = false;
const handleClose = () => {
if (isClosed) return;
ReactDOM.unmountComponentAtNode(container);
document.body.removeChild(container);
if (onClose) {
onClose();
}
isClosed = true;
};

document.body.appendChild(container);
Expand All @@ -55,4 +58,6 @@ export function showNewVisModal({ editorParams = [], onClose }: ShowNewVisModalP
</I18nProvider>
);
ReactDOM.render(element, container);

return () => handleClose();
}

0 comments on commit cd1c04b

Please sign in to comment.