// main.js // Run the init() function when the page has loaded window.addEventListener('DOMContentLoaded', init); function init() { // Get the reviews from localStorage let reviews = getReviewsFromStorage(); // Add each recipe to the
element addReviewsToDocument(reviews); // Add the event listeners to the form elements initFormHandler(); } /** * @returns {Array} An array of reviews found in localStorage */ function getReviewsFromStorage() { let result = JSON.parse(localStorage.getItem('reviews')) if (result) { return result; } return new Array(0); } /** * @param {Array} reviews An array of reviews */ function addReviewsToDocument(reviews) { let mainEl = document.querySelector('main') reviews.forEach(review=> { let newReview = document.createElement('review-card') newReview.data = review mainEl.append(newReview); }) } /** * Takes in an array of reviews, converts it to a string, and then * saves that string to 'reviews' in localStorage * @param {Array} reviews An array of reviews */ function saveReviewsToStorage(reviews) { localStorage.setItem('reviews', JSON.stringify(reviews)); } /** * Adds the necesarry event handlers to
and the clear storage *