1- const notificationForm = document . getElementById ( 'notificationForm' )
1+ const createNotificationForm = document . getElementById ( 'notificationForm' )
22const subscribeButton = document . getElementById ( 'subscribeButton' )
3+ const sendNotificationButton = document . getElementById ( 'send-notification-button' )
4+ const refreshNotificationsButton = document . getElementById ( 'refresh-notifications-button' )
5+ const notificationsList = document . getElementById ( 'notifications-list' )
6+
7+ // Functions to call on window load
8+ updateNotificationsList ( )
39
410// Common Functions
511function getBaseUrl ( ) {
@@ -10,7 +16,7 @@ function logError(error) {
1016 console . error ( 'Error: ' , error )
1117}
1218
13- function makeHttpRequest ( method , url , body , headers ) {
19+ function makeHttpRequest ( method , url , headers , body ) {
1420 return fetch ( url , {
1521 method : method ,
1622 body : body ,
@@ -40,18 +46,30 @@ function urlBase64ToUint8Array(base64String) {
4046}
4147
4248// Event Listeners
43- subscribeButton . addEventListener ( 'click' , function ( event ) {
49+ subscribeButton . addEventListener ( 'click' , function ( event ) {
4450 event . preventDefault ( )
4551 let result = requestNotificationPermission ( )
4652 console . log ( result )
4753} ) ;
4854
49- notificationForm . addEventListener ( 'submit' , function ( event ) {
55+ createNotificationForm . addEventListener ( 'submit' , function ( event ) {
56+ event . preventDefault ( )
57+ let result = saveNotification ( createNotificationForm )
58+ console . log ( result )
59+ } ) ;
60+
61+ sendNotificationButton . addEventListener ( 'click' , function ( event ) {
5062 event . preventDefault ( )
51- let result = saveNotification ( notificationForm )
63+ let selected_notification_id = notificationsList . options [ notificationsList . selectedIndex ] . id ;
64+ let result = triggerSendingPushNotification ( selected_notification_id )
5265 console . log ( result )
5366} ) ;
5467
68+ refreshNotificationsButton . addEventListener ( 'click' , function ( event ) {
69+ event . preventDefault ( )
70+ updateNotificationsList ( )
71+ } )
72+
5573
5674// Core Functions
5775function saveNotification ( notificationForm ) {
@@ -64,7 +82,7 @@ function saveNotification(notificationForm){
6482 let headers = {
6583 'Content-type' : 'application/json; charset=UTF-8' ,
6684 }
67- return makeHttpRequest ( 'POST' , url , body , headers )
85+ return makeHttpRequest ( 'POST' , url , headers , body )
6886 . then ( ( responseJson ) => {
6987 notificationForm . reset ( )
7088 return responseJson
@@ -96,6 +114,7 @@ function registerServiceWorker() {
96114
97115function requestNotificationPermission ( ) {
98116 if ( Notification . permission === 'granted' ) {
117+ alert ( "You are already subscribed!" )
99118 return Notification . permission
100119 }
101120 Notification . requestPermission ( )
@@ -138,7 +157,47 @@ function savePushSubscription(pushSubscription) {
138157 let headers = {
139158 'Content-type' : 'application/json; charset=UTF-8' ,
140159 }
141- return makeHttpRequest ( 'POST' , url , body , headers )
160+ return makeHttpRequest ( 'POST' , url , headers , body )
161+ . then ( ( responseJson ) => {
162+ return responseJson
163+ } ) . catch ( error => console . error ( 'Error: ' , error ) )
164+ }
165+
166+ function triggerSendingPushNotification ( notification_id ) {
167+ let url = getBaseUrl ( ) + '/api/notifications/' + notification_id + "/send"
168+ let headers = {
169+ 'Content-type' : 'application/json; charset=UTF-8' ,
170+ }
171+ return makeHttpRequest ( 'POST' , url , headers )
172+ . then ( ( responseJson ) => {
173+ return responseJson
174+ } ) . catch ( error => console . error ( 'Error: ' , error ) )
175+ }
176+
177+
178+ function updateNotificationsList ( ) {
179+ notificationsList . innerHTML = ''
180+ fetchAllNotifications ( )
181+ . then ( data => {
182+ data . forEach ( notification => render ( notification ) )
183+ } ) ;
184+ }
185+
186+ function render ( notification ) {
187+ const option = document . createElement ( 'option' )
188+ let option_text = notification . title + " | " + notification . description
189+ option . id = notification . id
190+ const content = document . createTextNode ( `${ option_text } ` )
191+ option . appendChild ( content )
192+ notificationsList . appendChild ( option )
193+ }
194+
195+ function fetchAllNotifications ( ) {
196+ let url = getBaseUrl ( ) + '/api/notifications'
197+ let headers = {
198+ 'Content-type' : 'application/json; charset=UTF-8' ,
199+ }
200+ return makeHttpRequest ( 'GET' , url , headers )
142201 . then ( ( responseJson ) => {
143202 return responseJson
144203 } ) . catch ( error => console . error ( 'Error: ' , error ) )
0 commit comments