Created
July 1, 2020 23:24
-
-
Save iam-raihan/0300f3a68be4403b250f8deca3ab8d92 to your computer and use it in GitHub Desktop.
triggers notification when shops are available in a campaign in evaly.com.bd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var campaign = 'evaly-earthquake-july-2020-99c4e2'; // set campaign slug | |
var pageUrl = `https://evaly.com.bd/campaign/${campaign}`; | |
var apiUrl = `https://api.evaly.com.bd/core/campaigns/public/${campaign}/shops`; | |
var delay = 5000; // ms | |
var showNotification = (msg, shopUrl = null) => { | |
var notification = new Notification('Evaly', { | |
icon: 'https://evaly.com.bd/static/images/icons/android-chrome-144x144.png', | |
body: msg, | |
}); | |
notification.onclick = function() { | |
window.open(shopUrl ?? pageUrl); | |
}; | |
}; | |
var onShopsFound = (shops) => { | |
var slug = shops[0].slug; | |
var shopUrl = `https://evaly.com.bd/campaign/shop/${slug}/${campaign}`; | |
showNotification(`Shops Found = ${shops.length}`, shopUrl); | |
}; | |
var processData = (data) => { | |
if (data.success) { | |
var shops = data.data; | |
if (shops.length === 0) { | |
console.log('No Shop Found'); | |
} else { | |
onShopsFound(shops); | |
} | |
} else { | |
showNotification(`Failed to fetch shops`); | |
} | |
}; | |
var callApi = () => { | |
fetch(apiUrl) | |
.then(res => { | |
if (res.status === 200) { | |
res.json().then(data => processData(data)); | |
} else { | |
showNotification(`Error! Status Code: ${res.status}`); | |
} | |
}) | |
.catch(err => showNotification(err)); | |
}; | |
var worker = setInterval(callApi, delay); | |
var stop = () => clearInterval(worker); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instruction:
That's it.
Then it will start working with 5 second interval and notify you when any shop is available.
Click on the notification to open available shop in new tab.