Skip to content

Commit

Permalink
Fix project meeting time not displaying on VRMS project page (#3939)
Browse files Browse the repository at this point in the history
* strengthen loop that appends meeting times to project page, refactor code to be more readable, and remove legacy code

* remove unnecessary API URL

* refactoring comments, variable and function names to improve readability

* fix display bug with non matching casing for Access The Data
  • Loading branch information
MattPereira authored Feb 6, 2023
1 parent 40fe386 commit 1e0c082
Showing 1 changed file with 24 additions and 78 deletions.
102 changes: 24 additions & 78 deletions assets/js/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,91 +118,37 @@ let meetingsHeader = document.querySelector('.meetingsHeader');
let projectTitle = scriptTag.getAttribute("projectTitle");
let meetingsFound = [];

// Assigns local JSON Data to localData variable
{% assign localData = site.data.external.vrms_data %}
// Grab the meeting time data from the vrms_data.json file
{% assign vrmsData = site.data.external.vrms_data %}
// Escapes JSON for injections. See: #2134. If this is no longer the case, perform necessary edits, and remove this comment.
let localData = JSON.parse(decodeURIComponent("{{ localData | jsonify | uri_escape }}"));
const API_URL = 'https://www.vrms.io/api/recurringevents';

// Function schedules data that is passed in
function schedule(scheduleData) {

// Loops through data and assigns information to variables in a readable format
for (let i = 0; i <= scheduleData.length - 1; i++) {
let startTime = timeFormat(new Date(scheduleData[i].startTime));
let endTime = timeFormat(new Date(scheduleData[i].endTime));
let webURL = scheduleData[i].project.hflaWebsiteUrl;
let projectName = scheduleData[i].project.name;
let description = scheduleData[i].description;
let day = new Date(scheduleData[i].date).toString().substring(0,3);
let scheduleClass;

// Function that adds data to proper header
function scheduleDay() {
// Assigns different class to data depending if it utilized local JSON file or the VRMS fetch data
if (scheduleData == localData) {
scheduleClass = "localData";
} else {
scheduleClass = "updated";
}
if (projectTitle === projectName) {
meetingsList.insertAdjacentHTML("beforeend", `<li class="${scheduleClass} meetingTime">${day} ${startTime} - ${endTime} <br>${description}</li>`);
const vrmsData = JSON.parse(decodeURIComponent("{{ vrmsData | jsonify | uri_escape }}"));

// Loops through the VRMS data and inserts each meeting time into the HTML of the correct project page
function appendMeetingTimes(scheduleData) {

for (const event of scheduleData) {
try {
// Assigning information to variables in a readable format
const startTime = timeFormat(new Date(event.startTime));
const endTime = timeFormat(new Date(event.endTime));
const webURL = event.project.hflaWebsiteUrl;
const projectName = event.project.name;
const description = event.description;
const day = new Date(event.date).toString().substring(0,3);

// only append the meeting times to the correct project page
if (projectTitle.toLowerCase() === projectName.toLowerCase()) {
meetingsList.insertAdjacentHTML("beforeend", `<li class="meetingTime">${day} ${startTime} - ${endTime} <br>${description}</li>`);
meetingsFound.push(day);
}
}
scheduleDay(day);
}
}

// Function checks for differences between two arrays
function arr_diff (a1, a2) {

let a = [], diff = [];

for (let i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}

for (let i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
} catch (e) {
console.error(e);
}
}

for (let k in a) {
diff.push(k);
}

return diff;
}

// Function clears the schedule
function clearSchedule() {
let prevDayList = meetingsList.getElementsByClassName("localData");

while (prevDayList[0]) {
prevDayList[0].parentNode.removeChild(prevDayList[0]);
}
}
}

// Executes schedule with localData
schedule(localData);

// Fetchs JSON file from VRMS_API
fetch(API_URL, {method: 'GET'})
.then(response => response.json())
.then((data) => {
// Checks for differences between localData and VRMS data
if (arr_diff(data, localData)) {
clearSchedule();
schedule(data);
}
})
.catch(err => {
console.log(err);
})
appendMeetingTimes(vrmsData);


if (meetingsFound.length >= 1) {
Expand Down

0 comments on commit 1e0c082

Please sign in to comment.