2022-11-08 03:18:05 +00:00
|
|
|
// main.js
|
2022-11-18 07:13:53 +00:00
|
|
|
import {getAllReviewsFromStorage} 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-15 00:53:07 +00:00
|
|
|
// Get the reviews from localStorage
|
2022-11-18 07:13:53 +00:00
|
|
|
let reviews = getAllReviewsFromStorage();
|
2022-11-15 00:53:07 +00:00
|
|
|
// Add each reviews to the <main> element
|
|
|
|
addReviewsToDocument(reviews);
|
|
|
|
// Add the event listeners to the form elements
|
|
|
|
initFormHandler();
|
2022-11-08 03:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
|
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-11-15 00:53:07 +00:00
|
|
|
reviews.forEach(review => {
|
|
|
|
let newReview = document.createElement("review-card");
|
|
|
|
newReview.data = review;
|
|
|
|
//TODO: want to append it to whatever the box is in layout
|
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-11 08:03:56 +00:00
|
|
|
* Adds the necessary event handlers to <form> and the clear storage
|
2022-11-08 03:18:05 +00:00
|
|
|
* <button>.
|
|
|
|
*/
|
|
|
|
function initFormHandler() {
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
//btn to create form (could be its own function?)
|
2022-11-18 02:45:53 +00:00
|
|
|
let createBtn = document.getElementById("create-btn");
|
2022-11-15 00:53:07 +00:00
|
|
|
createBtn.addEventListener("click", function(){
|
|
|
|
window.location.assign("./CreatePage.html");
|
2022-11-18 02:45:53 +00:00
|
|
|
});
|
2022-11-11 20:07:58 +00:00
|
|
|
}
|
2022-12-01 00:18:18 +00:00
|
|
|
|
|
|
|
const registerServiceWorker = async () => {
|
|
|
|
if ("serviceWorker" in navigator) {
|
|
|
|
try {
|
|
|
|
const registration = await navigator.serviceWorker.register("./sw.js", {
|
|
|
|
scope: "./",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Registration failed with ${error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
registerServiceWorker();
|