Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notification toggle #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
</div>
</div>
</div>

<div class="container d-flex flex-row flex-wrap pt-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="notification">
<label class="form-check-label" for="notification">Show notifications</label>
</div>
</div>

<div class="container d-flex flex-row flex-wrap pt-4">
<div class="form-group p-1" id="fetch-div">
<div class="input-group">
Expand Down
8 changes: 8 additions & 0 deletions js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const $relogin = $('#relogin')
const $logout = $('#logout')
const $ben = $('#ben')

const $notification = $('#notification')
const $fetchDiv = $('#fetch-div')
const $fetchLabel = $('#fetch-label')
const $fetchingDiv = $('#fetching-div')
Expand Down Expand Up @@ -59,6 +60,7 @@ $(document).ready(function () {
$startFetch.click(startFetchSlots)
$stopFetch.click(stopFetchSlots)
$book.click(book)
$notification.click(toggleNotification)
$('input[type=radio]').change(setPreferences)
})
$fetchingDiv.hide()
Expand Down Expand Up @@ -87,6 +89,8 @@ function play(msg, low=null) {
const audio = new Audio();
audio.src = 'assets/alert.mp3'
audio.play()
if(localStorage.getItem('notification'))
new Notification(msg)
}

const delay = (s) => new Promise((resolve) => setTimeout(resolve, s * 1000))
Expand Down Expand Up @@ -132,6 +136,10 @@ if (localStorage.getItem('fees'))
$(`input#${localStorage.getItem('fees')}`).prop('checked', true)
if (localStorage.getItem('mobile'))
$mobile.attr('value', localStorage.getItem('mobile'))
if(Notification.permission !== 'granted')
localStorage.removeItem('notification')
if(localStorage.getItem('notification') && Notification.permission !== 'denied')
$notification.prop('checked', true)

$(document).ready(async function () {
token = localStorage.getItem('token')
Expand Down
24 changes: 24 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,27 @@ async function download(appointment_id) {
document.body.appendChild(link);
link.click();
}

async function toggleNotification() {
if(localStorage.getItem('notification')) {
localStorage.removeItem('notification')
$notification.prop('checked', false)
} else {
if ("Notification" in window) {
if (Notification.permission !== "denied") {
const permission = await Notification.requestPermission()

if(permission === 'granted') {
$notification.prop('checked', true)
localStorage.setItem('notification', 'true')
new Notification('Notifcations Enabled')
}
}
}
}

if(Notification.permission === 'denied') {
$notification.prop('checked', false)
alert('Permission Denied')
}
}