-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
292 lines (257 loc) · 9.48 KB
/
functions.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
async function fetchHackathons() {
const response = await fetch('https://kontests.net/api/v1/all');
const data = await response.json();
// console.log(data);
return data;
};
async function getSites(){
const allSites = await fetch("https://kontests.net/api/v1/sites")
const siteData = await allSites.json()
return siteData;
}
function displayAmount(amt){
hackathonAmount.innerHTML = `Browse Hackathons from ${amt} different sites`;
}
function displaySites(data){
data.forEach(element =>{
siteHolder.innerHTML = siteHolder.innerHTML + ` <a class="navbar-item option2">${element[0]}</a>`;
});
}
function displayHackathons(list) {
cardHolder.innerHTML = "";
errorHolder.innerHTML = "";
list.forEach(element => {
let name = checkForBlank(element.name);
let link = checkForBlank(element.url);
let site = checkForBlank(element.site);
let in24 = checkForBlank(element.in_24_hours);
let status = checkForBlank(element.status);
let start = checkForBlank(element.start_time);
let end = checkForBlank(element.end_time);
let duration = getCurrentDuration(start, end);
cardHolder.innerHTML = cardHolder.innerHTML +
`<div class="card cardDetail">
<div class="card-content">
<p class="title">
${name}
</p>
<p class="subtitle duration" style="color:#4d608f;" data-time = ${duration[1]}, data-status = ${duration[0]} ${"Ending"} data-endUTC = ${end}>
${site} - ${codingOrBefore(status)}
<br> <br>
${duration[0]} in: ${formatTime(duration[1])}
<p>
${formatDate(start)} - ${formatDate(end)}
</p>
</div>
<footer class="card-footer">
<p class="card-footer-item">
<a href="${link}" target="_blank">
<button class="button is-info zoom">View
</button>
</a>
</p>
</footer>
</div>`;
});
if (list.length === 0) {
errorHolder.innerHTML = `<p class='centerError'>No results found with criteria... <br> Status: ${codingOrBefore(runStatus)}<br> Starts in 24 hours: ${in24} <br> Number of Sites: ${returnNumSites(listOfSites)} </p>`;
}
}
function checkForBlank(str){
if (str.length <= 1){
return "Unknown";
}
return str;
}
function formatTime(secondsInput) {
// Outputs time in seconds to days:hours:mintues:seconds
let days, hours, mintues, seconds
days = (Math.floor(secondsInput / 86400)).toString().padStart(2, "0");
secondsInput %= 86400;
hours = (Math.floor(secondsInput / 3600)).toString().padStart(2, "0");
secondsInput %= 3600;
mintues = ("" + Math.floor(secondsInput / 60)).padStart(2, "0");
seconds = (secondsInput % 60).toString().padStart(2, "0");
return `${days} : ${hours} : ${mintues} : ${seconds}`
}
function formatDate(UTCString) {
let date = new Date(UTCString)
return `${date.toDateString()} ${date.toLocaleTimeString()}`
}
function codingOrBefore(CB) {
if (CB === "CODING")
return ("Currently running");
if (CB === "BEFORE")
return ("Not started");
else {
return "either";
}
}
function returnNumSites(array) {
if (array.length === 0 || array.length === 11)
return "All sites"
let r = `${array.length} site`
if (array.length > 1)
r= r + "s"
return r
}
function getCurrentDuration(startUTC, endUTC) {
let startDate = new Date(startUTC);
let startDateTime = Math.floor(startDate.getTime() / 1000)
let endDate = new Date(endUTC);
let endDateTime = Math.floor(endDate.getTime() / 1000)
let curTime = Math.floor(Date.now() / 1000);
if (startDateTime > curTime) {
return ["Starting", (startDateTime - curTime)]
}
else {
return ["Ending", (endDateTime - curTime)];
}
}
function decrementDuration() {
cardDurations = document.querySelectorAll(".duration")
cardDurations.forEach(element => {
// Decreases the dataset time by 1
element.dataset.time = parseInt(element.dataset.time) - 1;
// Find index of breakline to seprate the name and
let index = element.innerHTML.search("<br>")
// Replaces innerHTML with part before the <br> then adds the upadted time to it
element.innerHTML = element.innerHTML.substring(0, index) + "<br>" + "<br>" + element.dataset.status + " in: " + formatTime(element.dataset.time)
//Sees if timer expired
if (element.dataset.time <= 0) {
if (element.dataset.status === "Ending") { //If ending say it competion is done
element.innerHTML = element.innerHTML.replace("Currently running", "Competion Over")
element.innerHTML = element.innerHTML.substring(0, element.innerHTML.search("<br>") + 4)
}
else {
//If not update dataset and restart timer
element.dataset.status = "Starting"
element.dataset.time = getCurrentDuration(0, element.dataset.endutc)[1]
element.dataset.status = "Ending"
element.innerHTML = element.innerHTML.replace("Not started", "Currently running")
}
}
})
}
function onOption0Click() {
if (this === options0[0]) {
// Check if first option is clicked if yes sets to either
if (options0[0].classList.contains("has-background-success")) {
options0[0].classList.remove("has-background-success")
runStatus = "either"
}
// If not sets background green and runStatus to coding then darkens second option if needed
else {
options0[0].classList.add("has-background-success")
runStatus = "CODING"
if (options0[1].classList.contains("has-background-success"))
options0[1].classList.remove("has-background-success")
}
}
else if (this === options0[1]) {
// Check if second option is clicked if yes and is clicked sets to either
if (options0[1].classList.contains("has-background-success")) {
options0[1].classList.remove("has-background-success")
runStatus = "either"
}
else { // If not already clicked sets background green and runStatus to Before then darkens second option if needed
options0[1].classList.add("has-background-success")
runStatus = "BEFORE"
if (options0[0].classList.contains("has-background-success"))
options0[0].classList.remove("has-background-success")
}
}
}
function onOption1Click() {
if (this === options1[0]) {
// Check if first option is clicked if yes sets to either
if (options1[0].classList.contains("has-background-success")) {
options1[0].classList.remove("has-background-success")
in24 = "either"
}
// If not sets background green and in24 to coding then darkens second option if needed
else {
options1[0].classList.add("has-background-success")
in24 = "Yes"
if (options1[1].classList.contains("has-background-success"))
options1[1].classList.remove("has-background-success")
}
}
else if (this === options1[1]) {
// Check if second option is clicked if yes and is clicked sets to either
if (options1[1].classList.contains("has-background-success")) {
options1[1].classList.remove("has-background-success")
in24 = "either"
}
else { // If not already clicked sets background green and in24 to Before then darkens second option if needed
options1[1].classList.add("has-background-success")
in24 = "No"
if (options1[0].classList.contains("has-background-success"))
options1[0].classList.remove("has-background-success")
}
}
}
function onOption2Click() {
if (!this.classList.contains("has-background-success")) {
this.classList.add("has-background-success");
listOfSites.push(this.innerHTML.trim());
//console.log(listOfSites);
}
else {
this.classList.remove("has-background-success");
// console.log(this.innerHTML)
//console.log(listOfSites.indexOf(this.innerHTML.trim()))
listOfSites.splice(listOfSites.indexOf(this.innerHTML.trim()), 1);
//console.log(listOfSites);
}
}
function checkStatus(hackathon) {
//console.log(hackathon.status, runStatus)
if (hackathon.status === runStatus || runStatus === "either")
return false
return true
}
function checkIn24(hackathon) {
if (hackathon.in_24_hours === in24 || in24 === "either")
return false
return true
}
function checkSites(hackathon) {
if (listOfSites.includes(hackathon.site) || listOfSites.length === 0)
return false
return true
}
function onSearchClick() {
let searchList = [];
hackathonsdata.forEach(element => searchList.push(element));
for (let i = 0; i < searchList.length; i++) {
if (checkStatus(searchList[i]) || checkIn24(searchList[i]) || checkSites(searchList[i])) {
searchList.splice(i, 1);
i--;
}
}
displayHackathons(searchList);
}
function createListeners(){
options0 = document.querySelectorAll(".option0");
options1 = document.querySelectorAll(".option1");
options2 = document.querySelectorAll(".option2");
searchButton = document.querySelector("#searchButton");
options0.forEach(option0 => {
option0.addEventListener("click", onOption0Click);
});
options1.forEach(option1 => {
option1.addEventListener("click", onOption1Click);
});
options2.forEach(option2 => {
option2.addEventListener("click", onOption2Click);
});
searchButton.addEventListener("click", onSearchClick);
}
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
}