forked from PlanQK/workflow-modeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationHandler.js
74 lines (67 loc) · 2.16 KB
/
NotificationHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React from "react";
import Notifications from "./Notifications";
export const NOTIFICATION_TYPES = ["info", "success", "error", "warning"];
/**
* Handler to manage notifications displayed to the user. Use getInstance() to get the current instance of the handler.
*
* Implements the Singleton pattern.
*/
export default class NotificationHandler {
static instance = undefined;
static getInstance() {
if (this.instance) {
return this.instance;
} else {
this.instance = new NotificationHandler([]);
return this.instance;
}
}
constructor(notifications) {
this.notifications = notifications;
this.currentNotificationId = -1;
this.notificationRef = React.createRef();
}
/**
* Creates a new Notifications React Component with a fixed ref to access the methods of the component.
*
* @param notifications The initial set of components to display wright after creation.
* @param notificationsContainer DOM element the notifications are rendered into.
* @returns the created Notifications React Component
*/
createNotificationsComponent(notifications, notificationsContainer) {
if (notifications) {
this.notifications = notifications;
}
return (
<Notifications
ref={this.notificationRef}
notifications={this.notifications}
container={notificationsContainer}
/>
);
}
/**
* Creates and displays a new Notification with the given properties. Calls effectively the respective method of the
* Notification Component.
*
* @param type The NOTIFICATION_TYPES of the notification.
* @param title The title of the notification.
* @param content The text displayed by the notification.
* @param duration The duration in milliseconds.
* @returns {{update: update, close: close}}
*/
displayNotification({ type = "info", title, content, duration = 4000 }) {
return this.notificationRef.current.displayNotification({
type: type,
title: title,
content: content,
duration: duration,
});
}
/**
* Close all open notifications.
*/
closeNotifications() {
this.notificationRef.current.closeNotifications();
}
}