cse110-fa22-group29/source/assets/scripts/localStorage.js
rheabhutada02 35f44049a2 updated crud features to work better
Co-authored-by: look-its-ashton <look-its-ashton@users.noreply.github.com>
Co-authored-by: Kara Hoagland <KH-Cl@users.noreply.github.com>
Co-authored-by: Gavyn Ezell <ezellgavyn@gmail.com>
2022-11-17 18:45:53 -08:00

28 lines
924 B
JavaScript

/**
* @returns {Array<Object>} An array of reviews found in localStorage
*/
export function getReviewsFromStorage() {
if (!(localStorage.getItem("activeIDS"))) {
// we wanna init the active ID array and start the nextID count
localStorage.setItem("activeIDS", JSON.stringify([]));
localStorage.setItem("nextID", JSON.stringify(0));
}
//iterate thru activeIDS
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
let reviews = []
for (let i = 0; i < activeIDS.length; i++) {
let currReview = JSON.parse(localStorage.getItem('review'+activeIDS[i]));
reviews.push(currReview);
}
return reviews;
}
/**
* Takes in an array of reviews, converts it to a string, and then
* saves that string to 'reviews' in localStorage
* @param {Array<Object>} reviews An array of reviews
*/
export function saveReviewsToStorage(reviews) {
localStorage.setItem(`review${reviewId}`, JSON.stringify(reviews));
}