Add locking mechanism around sendNotification

Workaround for multiple messages being sent
This commit is contained in:
Ceda EI 2021-01-03 12:51:50 +05:30
parent 49430114fe
commit 66d688291c
1 changed files with 12 additions and 3 deletions

View File

@ -7,12 +7,21 @@ import Telegram from "../utils/telegram";
import { TG_API, TG_USERID } from "../config";
function sendNotification(close) {
if (window.sendingAccidentAlert)
return;
window.sending = true;
const bot = new Telegram(TG_API);
bot.sendMessage(TG_USERID, "User detected in an accident. Location has been attached below.");
navigator.geolocation.getCurrentPosition(
position => bot.sendLocation( TG_USERID,
position.coords.latitude, position.coords.longitude),
() => bot.sendMessage(TG_USERID, "Error retrieving location")
position => {
bot.sendLocation( TG_USERID,
position.coords.latitude, position.coords.longitude);
window.sendingAccidentAlert = false;
},
() => {
bot.sendMessage(TG_USERID, "Error retrieving location");
window.sendingAccidentAlert = false;
}
);
setTimeout(close, 2000);
}