-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNotificationBanner.vue
More file actions
177 lines (155 loc) · 5.08 KB
/
NotificationBanner.vue
File metadata and controls
177 lines (155 loc) · 5.08 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<template>
<div class="notification-banner">
<b-alert
v-for="(notification, index) in notifications"
:key="index"
:varient="notificationVariant"
:class="{ 'alert-custom': customVariant }"
show
>
<h4>
<font-awesome-icon :icon="notificationIcon" :size="notificationIconSize" />
{{ notification.title }}
</h4>
<span v-html="notification.body" />
</b-alert>
</div>
</template>
<script>
import { library } from '@fortawesome/fontawesome-svg-core';
import { faInfo, faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { bAlert } from 'bootstrap-vue';
import oidc from '@uportal/open-id-connect';
import { get } from 'axios';
library.add(faInfo, faExclamationTriangle);
const variants = ['info', 'primary', 'success', 'danger', 'warning', 'secondary', 'light', 'dark'];
const icons = ['info', 'exclamation-triangle'];
const sizes = ['xs', 'sm', 'lg', ...[...Array(10).keys()].map((k) => `${k + 1}x`)];
const validator = (list) => (value) => list.indexOf(value) !== -1;
export default {
name: 'NotificationBanner',
props: {
debug: {
type: String,
default: 'false',
},
userInfoApiUrl: {
type: String,
default: '/uPortal/api/v5-1/userinfo',
},
notificationApiUrl: {
type: String,
default: '/NotificationPortlet/api/v2/notifications',
},
notificationVariant: {
type: String,
default: 'info',
validator: validator(variants),
},
notificationIcon: {
type: String,
default: 'info',
validator: validator(icons),
},
notificationIconSize: {
type: String,
default: '1x',
validator: validator(sizes),
},
filter: {
type: String,
default: '',
},
refresh: {
type: String,
default: '0',
},
},
components: {
'b-alert': bAlert,
'font-awesome-icon': FontAwesomeIcon,
},
data() {
return {
// list of notifications to display
notifications: [],
};
},
computed: {
isDebug: function () {
return this.debug === 'true' ? true : false;
},
customVariant: function () {
const { notificationVariant } = this;
return variants.indexOf(notificationVariant) === -1;
},
},
methods: {
async fetchNotifications() {
// read props
const { notificationApiUrl, filter, userInfoApiUrl } = this;
try {
// Obtain an OIDC Id Token, except in debug mode
const { encoded: token } = this.isDebug
? { encoded: null }
: await oidc({ userInfoApiUrl });
// gather notifications
const querystring = filter ? '?' + filter : '';
const { data: notifications } = await get(notificationApiUrl + querystring, {
withCredentials: true,
headers: {
Authorization: `Bearer ${token}`,
'content-type': 'application/jwt',
},
});
// store notifications to state
this.notifications = notifications;
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
if (this.refresh !== '0') {
const { refresh } = this;
const time = parseInt(refresh);
setTimeout(() => this.fetchNotifications(), time);
}
},
},
// entrypoint
created() {
return this.fetchNotifications();
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.notification-banner {
// core bootstrap framework
@import '../../node_modules/bootstrap/scss/functions';
@import '../../node_modules/bootstrap/scss/variables';
@import '../../node_modules/bootstrap/scss/mixins';
// bootstrap styles needed by page
@import '../../node_modules/bootstrap/scss/alert';
// custom styles
margin: 1rem;
margin: var(--notif-banner-container-margin, 1rem);
text-align: left;
.alert.alert-custom {
background: var(--notif-banner-bg-color, grey);
color: var(--notif-banner-body-text-color, white);
border-color: var(--notif-banner-border-color, var(--notif-banner-bg-color, grey));
border-radius: var(--notif-banner-border-radius, 0.25rem);
margin: var(--notif-banner-item-margin, 1rem);
> h4 {
color: var(--notif-banner-heading-text-color, white);
margin-top: 0;
}
}
}
.notification-banner svg {
height: 2rem;
position: relative;
top: 0.1rem;
}
</style>