-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
152 lines (133 loc) · 4.63 KB
/
fetch.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
document.addEventListener('DOMContentLoaded', function() {
let elems = document.querySelectorAll('.modal');
window.instances = M.Modal.init(elems,{dismissible:false});
});
window.addEventListener('load',async ()=>{
if(checkDatabase()){;
kuchbhi();
}
// load("465441","12-05-2021");
});
async function kuchbhi(){
let date = new Date();
let day = date.getDate();
let month = date.getMonth()+1;
if(month.toString().length==1) month = "0"+month;
const year = date.getFullYear();
date = `${day}-${month}-${year}`;
let weekIteration = 0;
let promises = [];
while(weekIteration<4){
let _day = day+7*weekIteration;
if(_day.toString().length==1) _day = "0"+_day;
promises.push(load(window.pincode,`${_day}-${month}-${year}`));
weekIteration++;
}
promises = await Promise.all(promises);
window.results = promises;
let temp = 0;
cleanUp(0)
for(let promise of promises){
if(promise.length!=0){
temp++;
for(let r of promise){
render(r['date'],r['center'],r['available_capacity'],r['slots'],r['age'])
}
}
}
document.getElementById('target').style.display = "block"
console.log(temp,'clenaup')
cleanUp(temp);
}
function checkDatabase(){
const pincode = localStorage.getItem('pincode');
if(!pincode){
window.instances[0].open();
}
else{
window.pincode = pincode;
// document.getElementById('pincode').value = pincode;
}
return pincode;
}
function changePincode(){
window.instances[0].open();
}
function setPincode(e){
const pincode = document.getElementById('setPincode');
if(String(pincode.value).length!=6){
return alert('Enter valid pincode')
}
localStorage.setItem('pincode',pincode.value);
window.pincode = pincode.value;
window.instances[0].close();
document.getElementById('filter').checked = false
kuchbhi();
}
function render(date,center,available_capacity,slots,age,filter=false){
if(filter){
if(age!=18) return;
}
let target = document.getElementById('target');
let listItem = document.createElement('li');
listItem.className = "collection-item"
listItem.innerText = `Date:${date}\nCenter:${center}\nSlots remaining: ${available_capacity}\nCan you book now: ${slots.length==0?'No':'Yes'}\nMin Age: ${age}`;
target.appendChild(listItem);
}
function filter(){
const results = window.results;
const fil = document.getElementById('filter').checked;
console.log(fil)
while (document.getElementById('target').hasChildNodes()) {
document.getElementById('target').removeChild(document.getElementById('target').firstChild);
}
for(let res of results){
for(let r of res){
render(r['date'],r['center'],r['available_capacity'],r['slots'],r['age'],fil)
}
}
}
async function load(pincode,date){
let res = await fetch(`https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pincode}&date=${date}`);
res = await res.json();
const result = [];
for(let center of res['centers']){
let sessions = center['sessions'];
for(let session of sessions){
if(session['available_capacity']>0){
let _result = {
center:center['address'],
date:session['date'],
available_capacity:session['available_capacity'],
slots:session['slots'],
age:session['min_age_limit']
}
result.push(_result);
}
}
}
return result;
}
function cleanUp(result){
if(result==0){
document.getElementById('loading').style.display = "block"
document.getElementById('loading').innerText = "No Slots Available";
while (document.getElementById('target').hasChildNodes()) {
document.getElementById('target').removeChild(document.getElementById('target').firstChild);
}
}
else{
document.getElementById('loading').style.display = 'none';
}
}
function getValues(e){
e.preventDefault();
// const date = String(document.getElementById('date').value).split('-').reverse().join('-');
const pincode = document.getElementById('pincode').value;
while (document.getElementById('target').hasChildNodes()) {
document.getElementById('target').removeChild(document.getElementById('target').firstChild);
}
document.getElementById('loading').innerText = "Loading...";
document.getElementById('loading').style.display = "block";
load(pincode,date);
}