Commit 4e3ff196 by Marcus Andersson Committed by GitHub

Dashboard: fixed issues with re-rendering of UI when importing dashboard (#21723)

* Fixed issues with angular digest and new backendSrv.
* Fixed failing tests.
parent f46ee123
import { DashboardImportCtrl } from './DashboardImportCtrl';
import config from 'app/core/config';
import { backendSrv } from 'app/core/services/backend_srv';
import { IScope } from 'angular';
describe('DashboardImportCtrl', () => {
const ctx: any = {};
jest.spyOn(backendSrv, 'getDashboardByUid').mockImplementation(() => Promise.resolve([]));
jest.spyOn(backendSrv, 'search').mockImplementation(() => Promise.resolve([]));
const getMock = jest.spyOn(backendSrv, 'get');
const $scope = ({ $evalAsync: jest.fn() } as any) as IScope;
let navModelSrv: any;
let validationSrv: any;
......@@ -20,7 +22,7 @@ describe('DashboardImportCtrl', () => {
validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
};
ctx.ctrl = new DashboardImportCtrl(validationSrv, navModelSrv, {} as any, {} as any);
ctx.ctrl = new DashboardImportCtrl($scope, validationSrv, navModelSrv, {} as any, {} as any);
jest.clearAllMocks();
});
......
......@@ -3,8 +3,9 @@ import config from 'app/core/config';
import locationUtil from 'app/core/utils/location_util';
import { ValidationSrv } from './services/ValidationSrv';
import { NavModelSrv } from 'app/core/core';
import { ILocationService } from 'angular';
import { ILocationService, IScope } from 'angular';
import { backendSrv } from 'app/core/services/backend_srv';
import { promiseToDigest } from 'app/core/utils/promiseToDigest';
export class DashboardImportCtrl {
navModel: any;
......@@ -32,6 +33,7 @@ export class DashboardImportCtrl {
/** @ngInject */
constructor(
private $scope: IScope,
private validationSrv: ValidationSrv,
navModelSrv: NavModelSrv,
private $location: ILocationService,
......@@ -116,20 +118,22 @@ export class DashboardImportCtrl {
this.titleTouched = true;
this.nameExists = false;
this.validationSrv
.validateNewDashboardName(this.folderId, this.dash.title)
.then(() => {
this.nameExists = false;
this.hasNameValidationError = false;
})
.catch(err => {
if (err.type === 'EXISTING') {
this.nameExists = true;
}
this.hasNameValidationError = true;
this.nameValidationError = err.message;
});
promiseToDigest(this.$scope)(
this.validationSrv
.validateNewDashboardName(this.folderId, this.dash.title)
.then(() => {
this.nameExists = false;
this.hasNameValidationError = false;
})
.catch(err => {
if (err.type === 'EXISTING') {
this.nameExists = true;
}
this.hasNameValidationError = true;
this.nameValidationError = err.message;
})
);
}
uidChanged(initial: boolean) {
......@@ -144,17 +148,19 @@ export class DashboardImportCtrl {
return;
}
backendSrv
// @ts-ignore
.getDashboardByUid(this.dash.uid)
.then((res: any) => {
this.uidExists = true;
this.hasUidValidationError = true;
this.uidValidationError = `Dashboard named '${res.dashboard.title}' in folder '${res.meta.folderTitle}' has the same uid`;
})
.catch((err: any) => {
err.isHandled = true;
});
promiseToDigest(this.$scope)(
backendSrv
// @ts-ignore
.getDashboardByUid(this.dash.uid)
.then((res: any) => {
this.uidExists = true;
this.hasUidValidationError = true;
this.uidValidationError = `Dashboard named '${res.dashboard.title}' in folder '${res.meta.folderTitle}' has the same uid`;
})
.catch((err: any) => {
err.isHandled = true;
})
);
}
onFolderChange(folder: any) {
......@@ -184,17 +190,19 @@ export class DashboardImportCtrl {
};
});
return backendSrv
.post('api/dashboards/import', {
dashboard: this.dash,
overwrite: true,
inputs: inputs,
folderId: this.folderId,
})
.then(res => {
const dashUrl = locationUtil.stripBaseFromUrl(res.importedUrl);
this.$location.url(dashUrl);
});
return promiseToDigest(this.$scope)(
backendSrv
.post('api/dashboards/import', {
dashboard: this.dash,
overwrite: true,
inputs: inputs,
folderId: this.folderId,
})
.then(res => {
const dashUrl = locationUtil.stripBaseFromUrl(res.importedUrl);
this.$location.url(dashUrl);
})
);
}
loadJsonText() {
......@@ -223,18 +231,20 @@ export class DashboardImportCtrl {
this.gnetError = 'Could not find dashboard';
}
return backendSrv
.get('api/gnet/dashboards/' + dashboardId)
.then(res => {
this.gnetInfo = res;
// store reference to grafana.com
res.json.gnetId = res.id;
this.onUpload(res.json);
})
.catch(err => {
err.isHandled = true;
this.gnetError = err.data.message || err;
});
return promiseToDigest(this.$scope)(
backendSrv
.get('api/gnet/dashboards/' + dashboardId)
.then(res => {
this.gnetInfo = res;
// store reference to grafana.com
res.json.gnetId = res.id;
this.onUpload(res.json);
})
.catch(err => {
err.isHandled = true;
this.gnetError = err.data.message || err;
})
);
}
back() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment