From abd4e7079244a00444b36fa7b8278bf03fda319f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Thu, 3 Jun 2021 16:55:04 +0200 Subject: [PATCH] Annotations: Fix so adds, updates and deletes are shown correctly (#35184) * Annotations: Fix for update/save/delete annotation not appearing/disappearing * Annotations: Fix so adds, updates and deletes are shown correctly * Chore: updates after PR comments --- .../app/features/annotations/event_editor.ts | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/public/app/features/annotations/event_editor.ts b/public/app/features/annotations/event_editor.ts index 1b92798d5ea..43b47a02dff 100644 --- a/public/app/features/annotations/event_editor.ts +++ b/public/app/features/annotations/event_editor.ts @@ -3,6 +3,7 @@ import { coreModule } from 'app/core/core'; import { AnnotationEvent, dateTime } from '@grafana/data'; import { MetricsPanelCtrl } from '../panel/metrics_panel_ctrl'; import { deleteAnnotation, saveAnnotation, updateAnnotation } from './api'; +import { getDashboardQueryRunner } from '../query/state/DashboardQueryRunner/DashboardQueryRunner'; export class EventEditorCtrl { // @ts-ignore initialized through Angular not constructor @@ -18,7 +19,7 @@ export class EventEditorCtrl { constructor() {} $onInit() { - this.event.panelId = this.panelCtrl.panel.id; + this.event.panelId = this.panelCtrl.panel.editSourceId ?? this.panelCtrl.panel.id; // set correct id if in panel edit this.event.dashboardId = this.panelCtrl.dashboard.id; // Annotations query returns time as Unix timestamp in milliseconds @@ -30,7 +31,7 @@ export class EventEditorCtrl { this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time!); } - save() { + async save(): Promise { if (!this.form.$valid) { return; } @@ -48,39 +49,30 @@ export class EventEditorCtrl { } } + let crudFunction = saveAnnotation; if (saveModel.id) { - updateAnnotation(saveModel) - .then(() => { - this.panelCtrl.refresh(); - this.close(); - }) - .catch(() => { - this.panelCtrl.refresh(); - this.close(); - }); - } else { - saveAnnotation(saveModel) - .then(() => { - this.panelCtrl.refresh(); - this.close(); - }) - .catch(() => { - this.panelCtrl.refresh(); - this.close(); - }); + crudFunction = updateAnnotation; + } + + try { + await crudFunction(saveModel); + } catch (err) { + console.log(err); + } finally { + this.close(); + getDashboardQueryRunner().run({ dashboard: this.panelCtrl.dashboard, range: this.panelCtrl.range }); } } - delete() { - return deleteAnnotation(this.event) - .then(() => { - this.panelCtrl.refresh(); - this.close(); - }) - .catch(() => { - this.panelCtrl.refresh(); - this.close(); - }); + async delete(): Promise { + try { + await deleteAnnotation(this.event); + } catch (err) { + console.log(err); + } finally { + this.close(); + getDashboardQueryRunner().run({ dashboard: this.panelCtrl.dashboard, range: this.panelCtrl.range }); + } } }