-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseNotificationSystem.ts
More file actions
151 lines (135 loc) · 3.53 KB
/
Copy pathuseNotificationSystem.ts
File metadata and controls
151 lines (135 loc) · 3.53 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { ref } from 'vue';
import useTokenGenerator from '@/composables/useTokenGenerator/useTokenGenerator';
import useLogger from '@/composables/useLogger/useLogger';
/**
* notifications list
*/
const notifications = ref<Notification[]>([]);
/**
* Notification System Composable
*/
export default function useNotificationSystem() {
const { generateValidToken } = useTokenGenerator();
const { addLog, addWarnLog, addErrorLog } = useLogger();
/**
* Create a notification
* @param options
*/
const createNotification: CreateNotification = (options) => {
const _options = Object.assign(
{ ...defaultNotificationOptions },
options
);
const type = _options.type;
// Add log message to the log file
if (type === 'error') {
addErrorLog(_options.title);
} else if (type === 'warning') {
addWarnLog(_options.title);
} else {
addLog(_options.title);
}
notifications.value.push(
...[
{
id: generateValidToken(),
..._options,
},
]
);
};
/**
* Create an error notification
* @param options
*/
const createErrorNotification: CreateNotification = (options) => {
createNotification({
type: 'error',
title: 'Yikes. Something went wrong.',
duration: 8,
...options,
});
};
/**
* Create a success notification
* @param options
*/
const createSuccessNotification: CreateNotification = (options) => {
createNotification({ type: 'success', title: 'Success!', ...options });
};
/**
* Create a warning notification
* @param options
*/
const createWarningNotification: CreateNotification = (options) => {
createNotification({
type: 'warning',
title: 'Something to lookout for.',
duration: 8,
...options,
});
};
/**
* Remove notification from the list
* @param id
*/
const removeNotifications = (id: string) => {
const index = notifications.value.findIndex((item) => item.id === id);
if (index !== -1) notifications.value.splice(index, 1);
};
/**
* Stop body overflow when notification is shown
*/
const stopBodyOverflow = () => {
document && document.body.classList.add(...['hide-overflow']);
};
/**
* Allow body overflow after notification is removed
*/
const allowBodyOverflow = () => {
document && document.body.classList.remove(...['hide-overflow']);
};
return {
notifications,
createNotification,
createSuccessNotification,
createErrorNotification,
createWarningNotification,
removeNotifications,
stopBodyOverflow,
allowBodyOverflow,
};
}
/**
* Notification interface
*/
export interface Notification {
id: string;
type: string;
title: string;
message: string;
autoClose: boolean;
duration: number;
}
/**
* Create notification function
*/
export type CreateNotification = {
(options: {
type?: string;
title?: string;
message?: string;
autoClose?: boolean;
duration?: number;
}): void;
};
/**
* Default notification options
*/
const defaultNotificationOptions = {
type: 'info',
title: 'Info Notification',
message: '',
autoClose: true,
duration: 5,
};