Skip to content

Commit 68802f1

Browse files
fix auto closing new vis modal when navigating to lens or when navigating away with browser history (#56998)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent fa8becb commit 68802f1

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/legacy/core_plugins/kibana/public/visualize/np_ready/listing/visualize_listing.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function initListingDirective(app) {
3232
);
3333
}
3434

35-
export function VisualizeListingController($injector, createNewVis) {
35+
export function VisualizeListingController($injector, $scope, createNewVis) {
3636
const {
3737
addBasePath,
3838
chrome,
@@ -59,7 +59,7 @@ export function VisualizeListingController($injector, createNewVis) {
5959
this.savedObjects = savedObjects;
6060

6161
this.createNewVis = () => {
62-
visualizations.showNewVisModal();
62+
this.closeNewVisModal = visualizations.showNewVisModal();
6363
};
6464

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

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

126126
addHelpMenuToAppChrome(chrome, docLinks);
127+
128+
$scope.$on('$destroy', () => {
129+
if (this.closeNewVisModal) {
130+
this.closeNewVisModal();
131+
}
132+
});
127133
}

src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/new_vis_modal.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ describe('NewVisModal', () => {
155155
expect(window.location.assign).toBeCalledWith('testbasepath/aliasUrl?addToDashboard');
156156
expect(onClose).toHaveBeenCalled();
157157
});
158+
159+
it('closes and redirects properly if visualization with aliasUrl and without addToDashboard in editorParams', () => {
160+
const onClose = jest.fn();
161+
window.location.assign = jest.fn();
162+
const wrapper = mountWithIntl(
163+
<NewVisModal
164+
isOpen={true}
165+
onClose={onClose}
166+
visTypesRegistry={visTypes}
167+
editorParams={['foo=true', 'bar=42']}
168+
addBasePath={addBasePath}
169+
uiSettings={uiSettings}
170+
savedObjects={{} as SavedObjectsStart}
171+
/>
172+
);
173+
const visButton = wrapper.find('button[data-test-subj="visType-visWithAliasUrl"]');
174+
visButton.simulate('click');
175+
expect(window.location.assign).toBeCalledWith('testbasepath/aliasUrl');
176+
expect(onClose).toHaveBeenCalled();
177+
});
158178
});
159179

160180
describe('filter for visualization types', () => {

src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/new_vis_modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ class NewVisModal extends React.Component<TypeSelectionProps, TypeSelectionState
145145
params = this.props.addBasePath(visType.aliasUrl);
146146
if (this.props.editorParams && this.props.editorParams.includes('addToDashboard')) {
147147
params = `${params}?addToDashboard`;
148-
this.props.onClose();
149148
}
149+
this.props.onClose();
150150
window.location.assign(params);
151151
return;
152152
}

src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/show_new_vis.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ export interface ShowNewVisModalParams {
3131

3232
export function showNewVisModal({ editorParams = [], onClose }: ShowNewVisModalParams = {}) {
3333
const container = document.createElement('div');
34+
let isClosed = false;
3435
const handleClose = () => {
36+
if (isClosed) return;
3537
ReactDOM.unmountComponentAtNode(container);
3638
document.body.removeChild(container);
3739
if (onClose) {
3840
onClose();
3941
}
42+
isClosed = true;
4043
};
4144

4245
document.body.appendChild(container);
@@ -55,4 +58,6 @@ export function showNewVisModal({ editorParams = [], onClose }: ShowNewVisModalP
5558
</I18nProvider>
5659
);
5760
ReactDOM.render(element, container);
61+
62+
return () => handleClose();
5863
}

0 commit comments

Comments
 (0)