-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
189 lines (177 loc) · 6.14 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
window.dataLayer = window.dataLayer || [];
function gtag()
{
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-XLFH7YHQ85');
$(function ()
{
getStates();
});
var attemptCount = 1;
const baseURL = 'https://cdn-api.co-vin.in/api/v2';
function setTimer() {
document.getElementById("attemptCount").innerHTML = attemptCount;
var maxTicks = 30;
var tickCount = 0;
var tick = function () {
if (tickCount >= maxTicks) {
clearInterval(myInterval);
getAvailability();
attemptCount++;
document.getElementById("attemptCount").innerHTML = attemptCount;
return;
}
document.getElementById("timer").innerHTML = (maxTicks - tickCount);
tickCount++;
};
var myInterval = setInterval(tick, 1000);
}
function resetSearch(){
window.location.reload();
}
function getAvailability() {
document.getElementById("searchButton").disabled = true;
document.getElementById("resetButton").style = "display:block"
var stateId = $("#states option:selected").val();
var districtId = $("#district option:selected").val();
if (stateId > 0 && districtId > 0) {
var todaysDate = new Date();
const options = { dateStyle: 'short' };
const date = todaysDate.toLocaleString('hi-IN', options);
document.getElementById("data").innerHTML = "";
$.ajax({
type: 'GET',
url: baseURL + '/appointment/sessions/public/calendarByDistrict',
data: {
"district_id": districtId,
"date": date
},
success: function (resp) {
}
}).done(
function (response) {
let centerDatas = "";
if (response?.centers?.length > 0) {
let availabilityFound = false;
response.centers.forEach(center => {
center.sessions.forEach(session => {
if (session.available_capacity > 0) {
availabilityFound = true;
}
});
});
if (availabilityFound) {
play();
document.getElementById("alert").style = "display:none"
} else {
setTimer();
playWait();
document.getElementById("alert").style = "display:block"
}
response.centers.forEach(element => {
centerDatas += `
<div class="card p-3 m-2">
<h5 class="card-title text-secondary">${element.name} <span class="badge badge-success">${element.fee_type} Vaccine</span></h5>
<p><strong>Address </strong> : ${element.address} </p>
<p> 🕑 ${element.from} - ${element.to}</p>
<span>${getSessions(element.sessions)}</span>
</div>
`
})
} else {
playNoSlots();
alert("No Centers are available!");
}
document.getElementById("data").innerHTML = centerDatas;
}).fail(function (data) {
alert("failed");
});
} else {
alert("Select state and district!");
}
}
function getSessions(sessions) {
var sessionsData = "";
if (sessions?.length > 0) {
sessions.forEach(session => {
if (session.available_capacity > 0) {
sessionsData +=
`<h4> Sessions </h4>
<h4 class="text-primary">Available Capacity : ${session.available_capacity}
<span ><a class="badge bookBtn" href='https://selfregistration.cowin.gov.in/' target="_blank">Book Now</a></span>
</h4>
<span>${getSlots(session)}</span>`
}
})
}
return sessionsData;
}
function getSlots(session) {
var slotsData = "";
if (session?.slots?.length > 0 && session.available_capacity > 0) {
slotsData +=
`
<h6>Date : ${session.date}</h6>
<h6>Min Age Limit : ${session.min_age_limit}</h6>
<h6>Time Slots </h6>
`
session.slots.forEach(slot => {
slotsData += `<h6>${slot}</h6>`
})
}
return slotsData;
}
function getDistricts() {
document.getElementById("data").innerHTML = "";
var stateId = $("#states option:selected").val();
$.ajax({
type: 'GET',
url: baseURL + '/admin/location/districts/' + stateId,
success: function (resp) {
}
}).done(
function (response) {
let districtsOptionsHTML = "";
if (response?.districts?.length > 0) {
response.districts.forEach(element => {
districtsOptionsHTML += `<option value="${element.district_id}"> ${element.district_name} </option>`
});
document.getElementById("district").innerHTML = districtsOptionsHTML;
}
}).fail(function (data) {
alert("failed");
});
}
function getStates() {
$.ajax({
type: 'GET',
url: baseURL + '/admin/location/states',
success: function (resp) {
}
}).done(
function (response) {
let statesOptionsHTML = "<option selected> Select State </option> ";
if (response?.states?.length > 0) {
response.states.forEach(element => {
statesOptionsHTML += `<option value="${element.state_id}"> ${element.state_name} </option>`
});
document.getElementById("states").innerHTML = statesOptionsHTML;
}
}).fail(function (data) {
alert("failed");
});
}
function play() {
var audio = new Audio('files/alert_available.mp3');
audio.play();
}
function playWait() {
var audioWait = new Audio('files/alert_not_available.mp3');
audioWait.play();
}
function playNoSlots() {
var audioNoSlot = new Audio('files/alert_no_center.mp3');
audioNoSlot.play();
}