2022-12-05 01:53:13 +00:00
|
|
|
// homepage.js
|
2022-12-03 02:02:52 +00:00
|
|
|
import { getIDsByTag, getIDsFromStorage, getReviewFromStorage, getTopIDsFromStorage } from "./localStorage.js";
|
2022-11-08 03:18:05 +00:00
|
|
|
|
|
|
|
// Run the init() function when the page has loaded
|
2022-11-15 00:53:07 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
2022-11-08 03:18:05 +00:00
|
|
|
|
|
|
|
function init() {
|
2022-11-30 10:49:49 +00:00
|
|
|
//initial population of review container
|
|
|
|
sortAndFilter(false, null);
|
|
|
|
//Add the event listeners to dropdown and search bar
|
2022-11-15 00:53:07 +00:00
|
|
|
initFormHandler();
|
2022-11-08 03:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-08 23:57:02 +00:00
|
|
|
* @param {Array<Object>} reviews An array of reviews
|
2022-11-08 03:18:05 +00:00
|
|
|
*/
|
2022-11-08 23:57:02 +00:00
|
|
|
function addReviewsToDocument(reviews) {
|
2022-11-21 19:58:26 +00:00
|
|
|
let reviewBox = document.getElementById("review-container");
|
2022-12-03 02:02:52 +00:00
|
|
|
reviews.forEach((review) => {
|
2022-11-15 00:53:07 +00:00
|
|
|
let newReview = document.createElement("review-card");
|
|
|
|
newReview.data = review;
|
2022-11-21 19:58:26 +00:00
|
|
|
reviewBox.append(newReview);
|
2022-11-15 00:53:07 +00:00
|
|
|
});
|
2022-11-08 03:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-30 10:49:49 +00:00
|
|
|
* Adds the necessary event handlers to search-btn and sort
|
2022-11-08 03:18:05 +00:00
|
|
|
*/
|
|
|
|
function initFormHandler() {
|
2022-11-30 04:51:45 +00:00
|
|
|
//grabbing search field
|
|
|
|
let searchField = document.getElementById("search-bar");
|
|
|
|
let searchBtn = document.getElementById("search-btn");
|
2022-11-30 10:49:49 +00:00
|
|
|
let searchTag = null;
|
2022-11-30 04:51:45 +00:00
|
|
|
//adding search functionality
|
2022-12-03 02:02:52 +00:00
|
|
|
searchBtn.addEventListener("click", function () {
|
2022-11-30 10:49:49 +00:00
|
|
|
searchTag = searchField.value;
|
|
|
|
sortAndFilter(searchTag);
|
|
|
|
});
|
|
|
|
|
2022-12-01 00:11:42 +00:00
|
|
|
//for clearing tag filter
|
|
|
|
let clearSearchBtn = document.getElementById("clear-search");
|
2022-12-03 02:02:52 +00:00
|
|
|
clearSearchBtn.addEventListener("click", function () {
|
2022-12-01 00:11:42 +00:00
|
|
|
searchTag = null;
|
|
|
|
searchField.value = "";
|
|
|
|
sortAndFilter(searchTag);
|
2022-12-02 01:01:05 +00:00
|
|
|
});
|
2022-12-01 00:11:42 +00:00
|
|
|
|
2022-11-30 10:49:49 +00:00
|
|
|
//sort by selected method
|
|
|
|
let sortMethod = document.getElementById("sort");
|
2022-12-03 02:02:52 +00:00
|
|
|
sortMethod.addEventListener("input", function () {
|
2022-11-30 10:49:49 +00:00
|
|
|
sortAndFilter(searchTag);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deciphers sort and filter to populate the review-container
|
|
|
|
* @param {string} searchTag tag name to filter by
|
|
|
|
*/
|
2022-12-03 02:02:52 +00:00
|
|
|
function sortAndFilter(searchTag) {
|
2022-11-30 10:49:49 +00:00
|
|
|
let reviewBox = document.getElementById("review-container");
|
|
|
|
let sortMethod = document.getElementById("sort");
|
|
|
|
//clear review container
|
2022-12-03 02:02:52 +00:00
|
|
|
while (reviewBox.firstChild) {
|
2022-11-30 10:49:49 +00:00
|
|
|
reviewBox.removeChild(reviewBox.firstChild);
|
|
|
|
}
|
|
|
|
let reviewIDs = [];
|
|
|
|
//sort method: most recent
|
2022-12-03 02:02:52 +00:00
|
|
|
if (sortMethod.value == "recent") {
|
2022-11-30 10:49:49 +00:00
|
|
|
//tag filtered most recent
|
2022-12-03 02:02:52 +00:00
|
|
|
if (searchTag) {
|
2022-11-30 10:49:49 +00:00
|
|
|
reviewIDs = getIDsByTag(searchTag);
|
2022-12-03 02:02:52 +00:00
|
|
|
}
|
2022-11-30 10:49:49 +00:00
|
|
|
//most recent
|
|
|
|
else {
|
|
|
|
reviewIDs = getIDsFromStorage();
|
|
|
|
}
|
|
|
|
//reversed for recency
|
2022-12-01 00:11:42 +00:00
|
|
|
loadReviews(0, reviewIDs);
|
2022-12-03 02:02:52 +00:00
|
|
|
}
|
2022-11-30 10:49:49 +00:00
|
|
|
//sort method: top rated
|
2022-12-03 02:02:52 +00:00
|
|
|
else if (sortMethod.value == "top") {
|
2022-11-30 10:49:49 +00:00
|
|
|
//tag filtered top rated
|
2022-12-03 02:02:52 +00:00
|
|
|
if (searchTag) {
|
2022-11-30 10:49:49 +00:00
|
|
|
//intersection of top ids list and ids by tag in top ids order
|
2022-12-03 02:02:52 +00:00
|
|
|
reviewIDs = getTopIDsFromStorage().filter((x) => getIDsByTag(searchTag).includes(x));
|
|
|
|
}
|
2022-11-30 10:49:49 +00:00
|
|
|
//top rated
|
|
|
|
else {
|
|
|
|
reviewIDs = getTopIDsFromStorage();
|
|
|
|
}
|
|
|
|
loadReviews(0, reviewIDs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate review-container with 9 more reviews
|
|
|
|
* @param {number} index review index to begin with
|
|
|
|
* @param {number[]} reviewIDs ordered array of reviews
|
|
|
|
*/
|
2022-12-03 02:02:52 +00:00
|
|
|
function loadReviews(index, reviewIDs) {
|
2022-11-30 10:49:49 +00:00
|
|
|
let reviewBox = document.getElementById("review-container");
|
2022-12-05 01:53:13 +00:00
|
|
|
let footer = document.querySelector("footer");
|
2022-12-01 00:11:42 +00:00
|
|
|
// label if there are no reviews to display
|
2022-12-03 02:02:52 +00:00
|
|
|
if (reviewIDs.length == 0) {
|
2022-12-01 00:11:42 +00:00
|
|
|
let emptyLabel = document.createElement("label");
|
|
|
|
emptyLabel.setAttribute("id", "empty");
|
|
|
|
emptyLabel.innerText = "No Reviews To Display";
|
|
|
|
reviewBox.append(emptyLabel);
|
|
|
|
} else {
|
|
|
|
let emptyLabel = document.getElementById("empty");
|
2022-12-03 02:02:52 +00:00
|
|
|
if (emptyLabel) {
|
2022-12-01 00:11:42 +00:00
|
|
|
reviewBox.removeChild(emptyLabel);
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 10:49:49 +00:00
|
|
|
let moreBtn = document.getElementById("more-btn");
|
|
|
|
//delete load more button if exists
|
2022-12-03 02:02:52 +00:00
|
|
|
if (moreBtn) {
|
2022-12-05 01:53:13 +00:00
|
|
|
footer.removeChild(moreBtn);
|
2022-11-30 10:49:49 +00:00
|
|
|
}
|
|
|
|
let reviewArr = [];
|
|
|
|
//check if there are more than 9 reviews left
|
2022-12-03 02:02:52 +00:00
|
|
|
if (index + 9 > reviewIDs.length - 1) {
|
2022-11-30 10:49:49 +00:00
|
|
|
//add remaining reviews to review container
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = index; i < reviewIDs.length; i++) {
|
2022-11-30 10:49:49 +00:00
|
|
|
reviewArr.push(getReviewFromStorage(reviewIDs[i]));
|
2022-11-30 04:51:45 +00:00
|
|
|
}
|
|
|
|
addReviewsToDocument(reviewArr);
|
2022-11-30 10:49:49 +00:00
|
|
|
} else {
|
|
|
|
//add 9 more reviews to container
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = index; i < index + 9; i++) {
|
2022-11-30 10:49:49 +00:00
|
|
|
reviewArr.push(getReviewFromStorage(reviewIDs[i]));
|
|
|
|
}
|
|
|
|
addReviewsToDocument(reviewArr);
|
|
|
|
//create and add load more button
|
|
|
|
moreBtn = document.createElement("button");
|
|
|
|
moreBtn.setAttribute("id", "more-btn");
|
|
|
|
moreBtn.innerText = "Load More";
|
|
|
|
//if load more clicked, load 9 more
|
2022-12-03 02:02:52 +00:00
|
|
|
moreBtn.addEventListener("click", function () {
|
|
|
|
loadReviews(index + 9, reviewIDs);
|
|
|
|
});
|
2022-12-05 01:53:13 +00:00
|
|
|
footer.append(moreBtn);
|
2022-11-30 10:49:49 +00:00
|
|
|
}
|
2022-11-11 20:07:58 +00:00
|
|
|
}
|
2022-12-01 00:18:18 +00:00
|
|
|
|
2022-12-05 02:32:15 +00:00
|
|
|
//setting up service worker
|
2022-12-01 00:18:18 +00:00
|
|
|
const registerServiceWorker = async () => {
|
|
|
|
if ("serviceWorker" in navigator) {
|
|
|
|
try {
|
2022-12-03 02:02:52 +00:00
|
|
|
await navigator.serviceWorker.register("./sw.js", { scope: "./" });
|
2022-12-01 00:18:18 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`Registration failed with ${error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-12-03 02:02:52 +00:00
|
|
|
registerServiceWorker();
|