]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: CdNotificationConfig
authorStephan Müller <smueller@suse.com>
Tue, 4 Dec 2018 14:18:41 +0000 (15:18 +0100)
committerStephan Müller <smueller@suse.com>
Wed, 12 Dec 2018 13:53:09 +0000 (14:53 +0100)
This model will now be taken as single argument in the 'show' method
of the notification service. Through using this model it's easy to
define notifications before sending them out. Which can be helpful
if you have to process an array of possible notification and the
possibility that those notifications would look a like.

This scenario doesn't exist in the current code base, but it's the
case for new feature to show alerts from Prometheus.

Fixes: https://tracker.ceph.com/issues/37469
Signed-off-by: Stephan Müller <smueller@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-notification.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts

index ba6a73a000ed7012b4cd041187833bbebc3ff596..575dc2d4056a4fb107a718bfd08412f51d30f4a2 100644 (file)
@@ -1,16 +1,23 @@
+import { ToastOptions } from 'ng2-toastr';
 import { NotificationType } from '../enum/notification-type.enum';
 
+export class CdNotificationConfig {
+  constructor(
+    public type: NotificationType,
+    public title: string,
+    public message?: string, // Use this for error notifications only
+    public options?: any | ToastOptions
+  ) {}
+}
+
 export class CdNotification {
-  message: string;
   timestamp: string;
-  title: string;
-  type: NotificationType;
-
-  constructor(type: NotificationType = NotificationType.info, title?: string, message?: string) {
-    this.type = type;
-    this.title = title;
-    this.message = message;
 
+  constructor(
+    public type: NotificationType = NotificationType.info,
+    public title?: string,
+    public message?: string
+  ) {
     /* string representation of the Date object so it can be directly compared
     with the timestamps parsed from localStorage */
     this.timestamp = new Date().toJSON();
index f751d76179437fad3e1bd10cca422ae6071439f3..a229c982ebed11dc23c42d7f19f7cdfebf379001 100644 (file)
@@ -5,6 +5,7 @@ import { ToastsManager } from 'ng2-toastr';
 
 import { configureTestBed, i18nProviders } from '../../../testing/unit-test-helper';
 import { NotificationType } from '../enum/notification-type.enum';
+import { CdNotificationConfig } from '../models/cd-notification';
 import { FinishedTask } from '../models/finished-task';
 import { NotificationService } from './notification.service';
 import { TaskMessageService } from './task-message.service';
@@ -57,7 +58,7 @@ describe('NotificationService', () => {
   }));
 
   it('should create a success notification and save it', fakeAsync(() => {
-    notificationService.show(NotificationType.success, 'Simple test');
+    notificationService.show(new CdNotificationConfig(NotificationType.success, 'Simple test'));
     tick(100);
     expect(notificationService['dataSource'].getValue().length).toBe(1);
     expect(notificationService['dataSource'].getValue()[0].type).toBe(NotificationType.success);
@@ -71,7 +72,7 @@ describe('NotificationService', () => {
   }));
 
   it('should create an info notification and save it', fakeAsync(() => {
-    notificationService.show(NotificationType.info, 'Simple test');
+    notificationService.show(new CdNotificationConfig(NotificationType.info, 'Simple test'));
     tick(100);
     expect(notificationService['dataSource'].getValue().length).toBe(1);
     const notification = notificationService['dataSource'].getValue()[0];
index 6c2650147868a72bf027741414debd485d2486a1..8874e27282644ce13673fd6ec7e0fa32f8ca41de 100644 (file)
@@ -5,7 +5,7 @@ import { ToastsManager } from 'ng2-toastr';
 import { BehaviorSubject } from 'rxjs';
 
 import { NotificationType } from '../enum/notification-type.enum';
-import { CdNotification } from '../models/cd-notification';
+import { CdNotification, CdNotificationConfig } from '../models/cd-notification';
 import { FinishedTask } from '../models/finished-task';
 import { ServicesModule } from './services.module';
 import { TaskMessageService } from './task-message.service';
@@ -70,10 +70,22 @@ export class NotificationService {
    * @param {string} [message] The message to be displayed. Note, use this field
    *   for error notifications only.
    * @param {*} [options] toastr compatible options, used when creating a toastr
-   * @memberof NotificationService
    * @returns The timeout ID that is set to be able to cancel the notification.
    */
-  show(type: NotificationType, title: string, message?: string, options?: any) {
+  show(type: NotificationType, title: string, message?: string, options?: any): number;
+  show(config: CdNotificationConfig): number;
+  show(
+    arg: NotificationType | CdNotificationConfig,
+    title?: string,
+    message?: string,
+    options?: any
+  ): number {
+    let type;
+    if (_.isObject(arg)) {
+      ({ message, type, title, options } = <CdNotificationConfig>arg);
+    } else {
+      type = arg;
+    }
     return setTimeout(() => {
       this.save(type, title, message);
       if (!message) {
@@ -94,15 +106,20 @@ export class NotificationService {
   }
 
   notifyTask(finishedTask: FinishedTask, success: boolean = true) {
+    let notification: CdNotificationConfig;
     if (finishedTask.success && success) {
-      this.show(NotificationType.success, this.taskMessageService.getSuccessTitle(finishedTask));
+      notification = new CdNotificationConfig(
+        NotificationType.success,
+        this.taskMessageService.getSuccessTitle(finishedTask)
+      );
     } else {
-      this.show(
+      notification = new CdNotificationConfig(
         NotificationType.error,
         this.taskMessageService.getErrorTitle(finishedTask),
         this.taskMessageService.getErrorMessage(finishedTask)
       );
     }
+    this.show(notification);
   }
 
   /**