diff --git a/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx b/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx index b3737bddd9a..11caee332e1 100644 --- a/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx +++ b/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx @@ -1,5 +1,6 @@ import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; +import { Prompt } from 'react-router-dom'; import AutoSizer from 'react-virtualized-auto-sizer'; import { css, cx } from 'emotion'; import { Subscription } from 'rxjs'; @@ -29,6 +30,7 @@ import { DashboardPanel } from '../../dashgrid/DashboardPanel'; import { exitPanelEditor, + discardPanelChanges, initPanelEditor, panelEditorCleanUp, updatePanelEditorUIState, @@ -36,7 +38,6 @@ import { } from './state/actions'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; -import { setDiscardChanges } from './state/reducers'; import { getPanelEditorTabs } from './state/selectors'; import { getPanelStateById } from '../../state/selectors'; @@ -83,7 +84,7 @@ const mapDispatchToProps = { exitPanelEditor, updateSourcePanel, panelEditorCleanUp, - setDiscardChanges, + discardPanelChanges, updatePanelEditorUIState, updateTimeZoneForSession, notifyApp, @@ -118,15 +119,18 @@ export class PanelEditorUnconnected extends PureComponent { this.forceUpdate(); }; - onDiscard = () => { - this.props.setDiscardChanges(true); - + onBack = () => { locationService.partial({ editPanel: null, tab: null, }); }; + onDiscard = () => { + this.props.discardPanelChanges(); + this.onBack(); + }; + onOpenDashboardSettings = () => { locationService.partial({ editview: 'settings', @@ -148,12 +152,11 @@ export class PanelEditorUnconnected extends PureComponent { return; } - if (this.props.panel.libraryPanel.meta.connectedDashboards === 0) { - return; - } - const connectedDashboards = await getLibraryPanelConnectedDashboards(this.props.panel.libraryPanel.uid); - if (connectedDashboards.length === 1 && connectedDashboards.indexOf(this.props.dashboard.id) !== -1) { + if ( + connectedDashboards.length === 0 || + (connectedDashboards.length === 1 && connectedDashboards.includes(this.props.dashboard.id)) + ) { try { await saveAndRefreshLibraryPanel(this.props.panel, this.props.dashboard.meta.folderId!); this.props.updateSourcePanel(this.props.panel); @@ -327,12 +330,7 @@ export class PanelEditorUnconnected extends PureComponent { Save ), - + Apply , ]; @@ -408,6 +406,18 @@ export class PanelEditorUnconnected extends PureComponent { return (
+ { + const searchParams = new URLSearchParams(location.search); + if (!this.props.panel.libraryPanel || !this.props.panel.hasChanged || searchParams.has('editPanel')) { + return true; + } + + exitPanelEditor(); + return false; + }} + /> {this.renderEditorActions()} diff --git a/public/app/features/dashboard/components/PanelEditor/state/actions.ts b/public/app/features/dashboard/components/PanelEditor/state/actions.ts index 0e62f6ad2ba..712cb7e1bf0 100644 --- a/public/app/features/dashboard/components/PanelEditor/state/actions.ts +++ b/public/app/features/dashboard/components/PanelEditor/state/actions.ts @@ -42,19 +42,25 @@ export function updateSourcePanel(sourcePanel: PanelModel): ThunkResult { }; } +export function discardPanelChanges(): ThunkResult { + return async (dispatch, getStore) => { + const { getPanel } = getStore().panelEditor; + getPanel().hasChanged = false; + dispatch(setDiscardChanges(true)); + }; +} export function exitPanelEditor(): ThunkResult { return async (dispatch, getStore) => { const dashboard = getStore().dashboard.getModel(); const { getPanel, shouldDiscardChanges } = getStore().panelEditor; const onConfirm = () => locationService.partial({ editPanel: null, tab: null }); + const panel = getPanel(); const onDiscard = () => { - dispatch(setDiscardChanges(true)); + dispatch(discardPanelChanges()); onConfirm(); }; - const panel = getPanel(); - if (shouldDiscardChanges || !panel.libraryPanel) { onConfirm(); return; diff --git a/public/app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal.tsx b/public/app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal.tsx index 3f5d9452692..53c25f610b6 100644 --- a/public/app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal.tsx +++ b/public/app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal.tsx @@ -109,12 +109,12 @@ export const SaveLibraryPanelModal: React.FC = ({ > Update all - +