-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
api-events.js
164 lines (151 loc) · 4.81 KB
/
api-events.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
const API_ENDPOINT = "https://www.vrms.io/api/recurringevents";
/**
* Fetches event data from VRMS API, then sorts and filters the data
* @param {URL} url
* @return {Object} response data
*/
async function getEventData() {
try {
const resp = await fetch(API_ENDPOINT);
const responseData = await resp.json();
return formatEventData(responseData);
} catch (err) {
console.error(err);
}
}
/**
* Inserts the recurring events into the html
* @param {Object} eventData - An array objects of the type returned by displayObject()
* @param {String} page - page that is using eventData ("events" or "project-meetings")
*/
function insertEventSchedule(eventData, page) {
for (const [key, value] of Object.entries(eventData)) {
let placeToInsert = document.querySelector(`#${key}-List`);
placeToInsert.innerHTML = "";
// check if the day has any events
if (!value.length) {
placeToInsert.insertAdjacentHTML(
"beforeend",
`<li>There are no meetings scheduled.</li>`
);
} else {
value.forEach((event) => {
if (event) {
// If a project doesn't have an hflaWebsiteUrl, redirect to the project's Github page
if (event.hflaWebsiteUrl == "") {
event.hflaWebsiteUrl = event.githubUrl
}
let eventHtml;
// insert the correct html for the current page
if (page === "events") {
eventHtml = `<li>${event.start} - ${event.end} </li><li><a href="${event.hflaWebsiteUrl}">${event.name}</a> ${event.meetingName}</li>`;
} else {
if(event.dsc != "") event.meetingName += ", ";
eventHtml = `<li>${event.start} - ${event.end} <a href="${event.hflaWebsiteUrl}">${event.name}</a> ${event.meetingName} ${event.dsc}</li>`;
}
placeToInsert.insertAdjacentHTML("beforeend", eventHtml);}
});
}
}
}
/**
* Formats event data
* @param {Object} data - array of event objects
* @return {Object} filtered and sorted data
*/
function formatEventData(data) {
const filteredData = filterDataFromApi(data);
const sortedData = sortData(filteredData);
return sortedData;
}
/**
* Filters out the needed data from the api endpoint
*/
function filterDataFromApi(responseData) {
const return_obj = {
Monday: [],
Tuesday: [],
Wednesday: [],
Thursday: [],
Friday: [],
Saturday: [],
Sunday: [],
};
responseData.forEach((item) => {
let day_of_week = getDayString(item.date);
return_obj[day_of_week].push(display_object(item));
});
return return_obj;
}
/**
* Sorts Filtered Date from the api end point by their start time
*/
function sortData(filteredData) {
for (const [key, value] of Object.entries(filteredData)) {
value.sort(function (a, b) {
return convertTime12to24(a.start) - convertTime12to24(b.start) || a.name.localeCompare(b.name);
});
}
return filteredData;
}
/**
* @param {Date} time - A valid javscript time string. Example: "2020-05-13T02:00:00.000Z"
* @return {String} - A time string formatted in the 12 hour format and converted to your timezone. Example: "10:00 pm"
*/
function localeTimeIn12Format(time) {
return new Date(time)
.toLocaleTimeString(
{},
{
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
hour12: true,
hour: "numeric",
minute: "numeric",
}
)
.toLowerCase();
}
/**
* @param {Date} date - A valid javscript time string. Example: "2020-05-13T02:00:00.000Z"
* @return {String} - The name of the day represented by the time string. Example 2020-05-13 was a wednesday. I.e rv = 'Wednesday'
*/
function getDayString(date) {
let new_date = new Date(date);
let weekday = new_date.getDay();
let options = { weekday: "long" };
return new Intl.DateTimeFormat("en-US", options).format(new_date);
}
/**
* Given a time string of 12 hour format like '06:00 pm' returns the integer of that string in 24 hour format -> 0600
* @param {String} time12h - A 12 hour format time string
* @return {Integer} An integer representing the input time string in 24 hour format
*/
function convertTime12to24(time12h) {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier.toLowerCase() === "pm") {
hours = parseInt(hours, 10) + 12;
}
return parseInt(`${hours}${minutes}`);
}
/**
* Function that represent the individual object extracted from the api
*/
function display_object(item) {
if (item && item.project) {
const rv_object = {
meetingName: item.name,
name: item.project.name,
dsc: item.description,
start: localeTimeIn12Format(item.startTime),
end: localeTimeIn12Format(item.endTime),
hflaWebsiteUrl: item.project.hflaWebsiteUrl,
githubUrl: item.project.githubUrl,
};
return rv_object;
}
}
export { getEventData, insertEventSchedule };