Basic implementation of notification

This commit is contained in:
Lucas Verney 2018-09-14 15:23:21 +02:00
parent 085f34dbe8
commit 04ac3538a4
1 changed files with 32 additions and 0 deletions

View File

@ -88,6 +88,7 @@ function setPosition(position) {
export default {
beforeDestroy() {
this.disableNoSleep();
this.removeNotification();
this.$store.dispatch('hideReportDetails');
},
components: {
@ -166,10 +167,16 @@ export default {
return {
isReportDialogVisible: false,
noSleep: null,
notification: null,
reportLatLng: null,
};
},
methods: {
createNotification() {
this.notification = new Notification('Toto', { body: 'Foobar', icon: '', tag: 'CyclassistMap' }); // TODO: icon
this.notification.addEventListener('click', this.showReportDialog);
this.notification.addEventListener('close', this.createNotification);
},
disableNoSleep() {
if (this.noSleep) {
this.noSleep.disable();
@ -211,6 +218,13 @@ export default {
onMapZoomUpdate(zoom) {
this.$store.dispatch('setCurrentMapZoom', { zoom });
},
removeNotification() {
if (this.notification) {
this.notification.removeEventListener('close', this.createNotification);
this.notification.close();
this.notification = null;
}
},
resetReportLatLng() {
this.reportLatLng = null;
},
@ -220,6 +234,22 @@ export default {
this.noSleep.enable();
}
},
setupNotification() {
if (!window.Notification) {
// Ensure notification API is available
return;
}
if (Notification.permission && Notification.permission === 'granted') {
this.createNotification();
} else {
Notification.requestPermission((permission) => {
if (permission === 'granted') {
this.createNotification();
}
});
}
},
showReportDialog(latlng) {
if (latlng) {
this.reportLatLng = [latlng[0], latlng[1]];
@ -249,6 +279,8 @@ export default {
}
}
this.$store.dispatch('fetchReports').catch(() => {});
this.setupNotification();
},
};
</script>