-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
223 lines (194 loc) · 6.79 KB
/
main.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
function renderUpdatedAt(datetimeString) {
// appends green circle when updatedAt is less than an hour
// appends yellow circle when updatedAt is less than three hours
// appends red circle when updatedAt is more than three hours
// Convert the input string to a Date object
const inputDate = new Date(datetimeString);
// Get the current time
const currentTime = new Date();
// Calculate the time difference in milliseconds
const timeDifference = inputDate.getTime() - currentTime.getTime();
// Convert the time difference to minutes
const timeDifferenceInMinutes = timeDifference / (1000 * 60);
// Check if the time difference is less than 60 minutes
if (Math.abs(timeDifferenceInMinutes) <= 60) {
return datetimeString + " 🟢";
} else if (Math.abs(timeDifferenceInMinutes) <= 180) {
return datetimeString + " 🟡";
} else {
return datetimeString + " 🔴";
}
}
// Generate dates used in filters, which contains today along with the next seven days.
//
// It returns two format of the same date. First is the one used in runnerData and second
// is used to display.
//
// Example output:
//
// ['Sat Feb 03, 2024', 'Feb 3, Sat']
// ['Sun Feb 04, 2024', 'Feb 4, Sun']
// ['Mon Feb 05, 2024', 'Feb 5, Mon']
// ['Tue Feb 06, 2024', 'Feb 6, Tue']
// ['Thu Feb 08, 2024', 'Feb 8, Thu']
// ['Fri Feb 09, 2024', 'Feb 9, Fri']
// ['Sat Feb 10, 2024', 'Feb 10, Sat']
// ['Sun Feb 11, 2024', 'Feb 11, Sun']
//
const generateDates = () => {
const today = new Date();
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const dates = []
for (let i = 0; i < 8; i++) {
const nextDate = new Date(today);
nextDate.setDate(today.getDate() + i);
const dayOfWeek = daysOfWeek[nextDate.getDay()];
const month = nextDate.toLocaleString('en', { month: 'short' });
const day = String(nextDate.getDate()).padStart(2, '0');
const day2 = nextDate.getDate();
const year = nextDate.getFullYear();
const filterDate = `${dayOfWeek} ${month} ${day}, ${year}`;
const displayDate = `${month} ${day2}, ${dayOfWeek}`;
dates.push([filterDate, displayDate]);
}
return dates;
}
const renderDateFilters = (runnerData) => {
const dates = generateDates();
const dateFilters = [];
dateFilters.push(`
<p>
<nav class="nav nav-pills nav-justified" id="date-filters-nav">
<a class="nav-item nav-link active" href="#" data-date="All">All</a>
`);
for (let i = 0; i < dates.length; i++) {
const [filterDate, displayDate] = dates[i];
dateFilters.push(`
<a class="nav-item nav-link" href="#" data-date="${filterDate}">${displayDate}</a>
`);
}
dateFilters.push(`
</nav>
</p>
`);
$("#date-filters").html(dateFilters.join("\n"));
$('#date-filters-nav a').on('click', function (e) {
$('#date-filters-nav a.active').removeClass("active");
e.preventDefault();
$(this).addClass('active');
const date = $(this).data("date");
console.log("select date: "+ date);
renderVenueVacancies(runnerData, date);
});
};
const renderVenueVacancies = (runnerData, dateFilter) => {
const card = [];
card.push(`<div id="accordion">`);
for (let venue in runnerData.venues) {
var venueData = runnerData.venues[venue];
var venueTitle = `${venueData.city} - ${venueData.name}`;
var venueSite = venueData.site;
var venueDaysVisible = venueData.visibleDays;
var venueNote = venueData.note;
var venueRemark = `${venueDaysVisible} days visible`;
if (venueNote) {
venueRemark += `; ${venueNote}`;
}
var dateVacancies = runnerData.vacancies[venue];
if (!dateVacancies) {
continue;
}
var errored = dateVacancies.errored;
var venueVacancyCount = 0;
var rows = [];
for (let date in dateVacancies) {
if (dateFilter !== "All" & date != dateFilter) {
continue;
}
var vacancies = dateVacancies[date];
var hasShownDate = false;
for (let i = 0; i < vacancies.length; i++) {
var vacancy = vacancies[i];
if (dateFilter == "All") {
rows.push(`
<tr>
<td scope="row">${hasShownDate ? "" : date}</td>
<td>${vacancy.start_time} - ${vacancy.end_time}</td>
<td>(${vacancy.duration})</td>
<td>${vacancy.court_info}</td>
</tr>
`);
hasShownDate = true;
venueVacancyCount++;
} else {
rows.push(`
<tr>
<td scope="row">${vacancy.start_time} - ${vacancy.end_time}</td>
<td>(${vacancy.duration})</td>
<td>${vacancy.court_info}</td>
</tr>
`);
venueVacancyCount++;
}
}
}
var table = `
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tbody>
${rows.join("\n")}
</tbody>
</table>
</div>
`;
var alertInfo = errored
? `
<li class="list-inline-item">
<span class="badge badge-danger">⚠️ Fail to sync</span></li>
`
: "";
var collapsedTable = errored
? ""
: `
<div id="collapse${venue}" class="collapse show" aria-labelledby="heading${venue}" data-parent="#accordion">
<div class="card-body">
${table}
</div>
</div>
`;
card.push(`
<div class="card">
<div class="card-header" id="heading${venue}">
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center" style="background: transparent;border: transparent;">
<ul class="list-inline" style="margin-bottom: 0">
<li class="list-inline-item">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse${venue}" aria-expanded="true" aria-controls="collapse${venue}">
<h5 style="margin-bottom: 0">${venueTitle}</h5>
</button>
</li>
<li class="list-inline-item"><a href="${venueSite}" target="_blank">(Link)</a></li>
<li class="list-inline-item"><small><i>(${venueRemark})</i></small></li>
${alertInfo}
</ul>
<span class="badge badge-primary badge-pill">${venueVacancyCount > 0 ? venueVacancyCount : ""}</span>
</li>
</ul>
</div>
${collapsedTable}
</div>
`);
}
card.push("</div>");
$("#vacancies-list").html(card.join("\n"));
$(".collapse").collapse();
};
$(document).ready(function () {
console.log(runnerData);
const updatedAt = renderUpdatedAt(runnerData.updated_at);
$("#last-updated-at").text(
`Last updated at: ${updatedAt} (update every ~10 min)`,
);
renderDateFilters(runnerData);
renderVenueVacancies(runnerData, "All");
});