diff --git a/public/app/features/dashboard/state/reducers.ts b/public/app/features/dashboard/state/reducers.ts index f7fb457a30a..8c2aed70873 100644 --- a/public/app/features/dashboard/state/reducers.ts +++ b/public/app/features/dashboard/state/reducers.ts @@ -80,12 +80,14 @@ const dashbardSlice = createSlice({ updatePanelState(state, action.payload.panelId, { plugin: action.payload.plugin }); }, cleanUpEditPanel: (state, action: PayloadAction) => { + // TODO: refactor, since the state should be mutated by copying only delete state.panels[EDIT_PANEL_ID]; }, setPanelAngularComponent: (state: DashboardState, action: PayloadAction) => { updatePanelState(state, action.payload.panelId, { angularComponent: action.payload.angularComponent }); }, addPanel: (state, action: PayloadAction) => { + // TODO: refactor, since the state should be mutated by copying only state.panels[action.payload.id] = { pluginId: action.payload.type }; }, }, diff --git a/public/app/features/manage-dashboards/SnapshotListCtrl.ts b/public/app/features/manage-dashboards/SnapshotListCtrl.ts deleted file mode 100644 index f13a7b3e86a..00000000000 --- a/public/app/features/manage-dashboards/SnapshotListCtrl.ts +++ /dev/null @@ -1,60 +0,0 @@ -import _ from 'lodash'; -import { ILocationService, IScope } from 'angular'; -import { getBackendSrv } from '@grafana/runtime'; - -import { NavModelSrv } from 'app/core/core'; -import { GrafanaRootScope } from 'app/routes/GrafanaCtrl'; -import { CoreEvents } from 'app/types'; -import { promiseToDigest } from '../../core/utils/promiseToDigest'; - -export class SnapshotListCtrl { - navModel: any; - snapshots: any; - - /** @ngInject */ - constructor( - private $rootScope: GrafanaRootScope, - navModelSrv: NavModelSrv, - private $location: ILocationService, - private $scope: IScope - ) { - this.navModel = navModelSrv.getNav('dashboards', 'snapshots', 0); - promiseToDigest(this.$scope)( - getBackendSrv() - .get('/api/dashboard/snapshots') - .then((result: any) => { - const baseUrl = this.$location.absUrl().replace($location.url(), ''); - this.snapshots = result.map((snapshot: any) => ({ - ...snapshot, - url: snapshot.externalUrl || `${baseUrl}/dashboard/snapshot/${snapshot.key}`, - })); - }) - ); - } - - removeSnapshotConfirmed(snapshot: any) { - _.remove(this.snapshots, { key: snapshot.key }); - promiseToDigest(this.$scope)( - getBackendSrv() - .delete('/api/snapshots/' + snapshot.key) - .then( - () => {}, - () => { - this.snapshots.push(snapshot); - } - ) - ); - } - - removeSnapshot(snapshot: any) { - this.$rootScope.appEvent(CoreEvents.showConfirmModal, { - title: 'Delete', - text: 'Are you sure you want to delete snapshot ' + snapshot.name + '?', - yesText: 'Delete', - icon: 'trash-alt', - onConfirm: () => { - this.removeSnapshotConfirmed(snapshot); - }, - }); - } -} diff --git a/public/app/features/manage-dashboards/SnapshotListPage.tsx b/public/app/features/manage-dashboards/SnapshotListPage.tsx new file mode 100644 index 00000000000..571354e0dbe --- /dev/null +++ b/public/app/features/manage-dashboards/SnapshotListPage.tsx @@ -0,0 +1,30 @@ +import React, { FC } from 'react'; +import { MapStateToProps, connect } from 'react-redux'; +import { NavModel } from '@grafana/data'; +import Page from 'app/core/components/Page/Page'; +import { getUrl } from 'app/core/selectors/location'; +import { StoreState } from 'app/types'; +import { SnapshotListTable } from './components/SnapshotListTable'; +import { getDashboardNavModel } from './state/selectors'; + +interface Props { + navModel: NavModel; + url: string; +} + +export const SnapshotListPage: FC = ({ navModel, url }) => { + return ( + + + + + + ); +}; + +const mapStateToProps: MapStateToProps = (state: StoreState) => ({ + navModel: getDashboardNavModel(state), + url: getUrl(state.location), +}); + +export default connect(mapStateToProps)(SnapshotListPage); diff --git a/public/app/features/manage-dashboards/components/SnapshotListTable.tsx b/public/app/features/manage-dashboards/components/SnapshotListTable.tsx new file mode 100644 index 00000000000..a0189dca322 --- /dev/null +++ b/public/app/features/manage-dashboards/components/SnapshotListTable.tsx @@ -0,0 +1,100 @@ +import React, { FC, useState, useCallback, useEffect } from 'react'; +import { ConfirmModal, Button, LinkButton } from '@grafana/ui'; +import { getBackendSrv } from '@grafana/runtime'; +import { noop } from 'rxjs'; +import { Snapshot } from '../types'; + +interface Props { + url: string; +} + +export const SnapshotListTable: FC = ({ url }) => { + const [snapshots, setSnapshots] = useState([]); + const [removeSnapshot, setRemoveSnapshot] = useState(); + + const getSnapshots = useCallback(async () => { + await getBackendSrv() + .get('/api/dashboard/snapshots') + .then((result: Snapshot[]) => { + const absUrl = window.location.href; + const baseUrl = absUrl.replace(url, ''); + const snapshots = result.map(snapshot => ({ + ...snapshot, + url: snapshot.externalUrl || `${baseUrl}/dashboard/snapshot/${snapshot.key}`, + })); + setSnapshots(snapshots); + }); + }, []); + + const doRemoveSnapshot = useCallback( + async (snapshot: Snapshot) => { + setSnapshots(snapshots.filter(ss => ss.key !== snapshot.key)); + await getBackendSrv() + .delete(`/api/snapshots/${snapshot.key}`) + .then(noop, () => { + setSnapshots(snapshots.concat(snapshot)); + }); + }, + [snapshots] + ); + + useEffect(() => { + getSnapshots(); + }, []); + + return ( +
+ + + + + + + + + + + + {snapshots.map((snapshot, key) => { + return ( + + + + + + + + ); + })} + +
+ Name + + Snapshot url +
+ {snapshot.name} + + {snapshot.url} + {snapshot.external && External} + + View + + +
+ + setRemoveSnapshot(undefined)} + onConfirm={() => { + doRemoveSnapshot(removeSnapshot); + setRemoveSnapshot(undefined); + }} + /> +
+ ); +}; diff --git a/public/app/features/manage-dashboards/index.ts b/public/app/features/manage-dashboards/index.ts index 79b9b7148e3..a1790ce0340 100644 --- a/public/app/features/manage-dashboards/index.ts +++ b/public/app/features/manage-dashboards/index.ts @@ -3,10 +3,3 @@ export { ValidationSrv } from './services/ValidationSrv'; // Components export * from './components/UploadDashboard'; - -// Controllers -import { SnapshotListCtrl } from './SnapshotListCtrl'; - -import coreModule from 'app/core/core_module'; - -coreModule.controller('SnapshotListCtrl', SnapshotListCtrl); diff --git a/public/app/features/manage-dashboards/partials/snapshot_list.html b/public/app/features/manage-dashboards/partials/snapshot_list.html deleted file mode 100644 index acdb08d71f7..00000000000 --- a/public/app/features/manage-dashboards/partials/snapshot_list.html +++ /dev/null @@ -1,37 +0,0 @@ - - -
- - - - - - - - - - - - - - - -
NameSnapshot url
- {{snapshot.name}} - - {{snapshot.url}} - - External - - - - View - - - - - -
-
- -