cse110-fa22-group29/source/assets/scripts/localStorage.js

268 lines
8.0 KiB
JavaScript
Raw Permalink Normal View History

/**
* Creates a new review to storage and performs related meta tasks
* @param {Object} review to store
* @return {number} ID of the newly added review
*/
export function newReviewToStorage(review) {
//grabbing the nextID, and putting our review object in storage associated with the ID
let nextReviewId = JSON.parse(localStorage.getItem("nextID"));
review["reviewID"] = nextReviewId;
// set the review entry to the review object
localStorage.setItem(`review${nextReviewId}`, JSON.stringify(review));
2022-11-27 23:48:00 +00:00
// adding to the tag keys
addTagsToStorage(nextReviewId, review["tags"]);
//adding to the star storage
let starArr = JSON.parse(localStorage.getItem(`star${review["rating"]}`));
if (!starArr) {
starArr = [];
}
starArr.push(nextReviewId);
localStorage.setItem(`star${review["rating"]}`, JSON.stringify(starArr));
//updating our activeIDS list
let tempIdArr = JSON.parse(localStorage.getItem("activeIDS"));
tempIdArr.push(nextReviewId);
localStorage.setItem("activeIDS", JSON.stringify(tempIdArr));
//increment nextID for next review creation
localStorage.setItem("nextID", JSON.stringify(nextReviewId + 1));
return nextReviewId;
}
/**
* Gets a single review by ID from storage
* @param {string} ID of the review to get
* @returns {Object} review object corresponding to param ID
*/
export function getReviewFromStorage(ID) {
return JSON.parse(localStorage.getItem(`review${ID}`));
}
/**
* Updates a single review by ID to storage
* @param {string} ID of review to update
* @param {Object} review to store
*/
export function updateReviewToStorage(ID, review) {
2022-11-28 01:31:13 +00:00
let oldReview = JSON.parse(localStorage.getItem(`review${ID}`));
2022-12-02 00:00:51 +00:00
let starArr = JSON.parse(localStorage.getItem(`star${review["rating"]}`));
//activeID update recency
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
for (let i in activeIDS) {
if (activeIDS[i] == ID) {
activeIDS.splice(i, 1);
2022-12-02 00:00:51 +00:00
activeIDS.push(ID);
break;
}
}
localStorage.setItem("activeIDS", JSON.stringify(activeIDS));
2022-11-28 01:31:13 +00:00
//star local storage update
if (oldReview["rating"] !== review["rating"]) {
//first delete from previous rating array in storage
let oldStarArr = JSON.parse(localStorage.getItem(`star${oldReview["rating"]}`));
for (let i in oldStarArr) {
if (oldStarArr[i] == ID) {
//removing from corresponding rating array and updating local Storage
oldStarArr.splice(i, 1);
break;
}
}
if (oldStarArr.length != 0) {
localStorage.setItem(`star${oldReview["rating"]}`, JSON.stringify(oldStarArr));
} else {
localStorage.removeItem(`star${oldReview["rating"]}`);
}
//then add ID to array corresponding to new review rating
2022-12-02 00:00:51 +00:00
let newStarArr = starArr;
if (!newStarArr) {
newStarArr = [];
}
newStarArr.push(ID);
localStorage.setItem(`star${review["rating"]}`, JSON.stringify(newStarArr));
} else if (starArr.length !== 1) {
2022-12-02 00:00:51 +00:00
//stars update recency if unchanged
for (let i in starArr) {
if (starArr[i] == ID) {
starArr.splice(i, 1);
2022-12-02 00:00:51 +00:00
starArr.push(ID);
break;
}
}
localStorage.setItem(`star${review["rating"]}`, JSON.stringify(starArr));
}
//specifically the unchanged tags update recency
let repeatedTags = review["tags"].filter((x) => oldReview["tags"].includes(x));
2022-12-02 00:00:51 +00:00
let tagArr = [];
for (let i in repeatedTags) {
2022-12-03 02:31:26 +00:00
tagArr = JSON.parse(localStorage.getItem(`!${repeatedTags[i].toLocaleLowerCase()}`));
if (tagArr.length !== 1) {
for (let j in tagArr) {
if (tagArr[j] == ID) {
tagArr.splice(j, 1);
2022-12-02 00:00:51 +00:00
tagArr.push(ID);
break;
}
}
2022-12-03 02:31:26 +00:00
localStorage.setItem(`!${repeatedTags[i].toLocaleLowerCase()}`, JSON.stringify(tagArr));
2022-12-02 00:00:51 +00:00
}
}
2022-11-28 01:31:13 +00:00
//Get diff of tags and update storage
let deletedTags = oldReview["tags"].filter((x) => !review["tags"].includes(x));
let addedTags = review["tags"].filter((x) => !oldReview["tags"].includes(x));
2022-11-29 02:48:55 +00:00
deleteTagsFromStorage(ID, deletedTags);
addTagsToStorage(ID, addedTags);
2022-11-28 01:31:13 +00:00
// set the review entry with ID to the review object
localStorage.setItem(`review${ID}`, JSON.stringify(review));
}
/**
* Deletes a review by ID from storage
* @param {string} ID of the review to delete
*/
export function deleteReviewFromStorage(ID) {
//removing id number from activeIDS and star{rating}
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
let reviewRating = JSON.parse(localStorage.getItem(`review${ID}`))["rating"];
let starArr = JSON.parse(localStorage.getItem(`star${reviewRating}`));
for (let i in starArr) {
if (starArr[i] == ID) {
//removing from corresponding rating array and updating local Storage
starArr.splice(i, 1);
break;
}
}
if (starArr.length != 0) {
localStorage.setItem(`star${reviewRating}`, JSON.stringify(starArr));
} else {
localStorage.removeItem(`star${reviewRating}`);
}
for (let i in activeIDS) {
if (activeIDS[i] == ID) {
activeIDS.splice(i, 1);
2022-11-18 07:56:07 +00:00
localStorage.setItem("activeIDS", JSON.stringify(activeIDS));
2022-11-27 23:48:00 +00:00
let currReview = JSON.parse(localStorage.getItem(`review${ID}`));
deleteTagsFromStorage(ID, currReview["tags"]);
2022-11-18 07:56:07 +00:00
localStorage.removeItem(`review${ID}`);
return;
}
}
console.error(`could not find review${ID} in localStorage`);
}
2022-11-27 23:48:00 +00:00
/**
* Delete ID from the specified tags' storage
* @param {string} ID to delete from lists
* @param {string[]} deletedTags to modify storage of
*/
2022-11-29 02:48:55 +00:00
function deleteTagsFromStorage(ID, deletedTags) {
for (let i in deletedTags) {
2022-11-27 23:48:00 +00:00
//get local storage of each tag and remove id from tag list
2022-12-03 02:31:26 +00:00
let tagName = "!" + deletedTags[i].toLocaleLowerCase();
2022-11-29 22:16:17 +00:00
let tagArr = JSON.parse(localStorage.getItem(tagName));
for (let j in tagArr) {
if (tagArr[j] == ID) {
tagArr.splice(j, 1);
2022-11-29 22:27:48 +00:00
break;
2022-11-27 23:48:00 +00:00
}
}
if (tagArr.length != 0) {
2022-11-29 22:16:17 +00:00
localStorage.setItem(tagName, JSON.stringify(tagArr));
2022-11-27 23:48:00 +00:00
} else {
2022-11-29 22:16:17 +00:00
localStorage.removeItem(tagName);
2022-11-27 23:48:00 +00:00
}
}
}
/**
* Add ID from the specified tags' storage
* @param {string} ID to add to lists
* @param {string[]} addedTags to modify storage of
*/
2022-11-29 02:48:55 +00:00
function addTagsToStorage(ID, addedTags) {
for (let i in addedTags) {
2022-12-03 02:31:26 +00:00
let tagName = "!" + addedTags[i].toLocaleLowerCase();
2022-11-29 22:16:17 +00:00
let tagArr = JSON.parse(localStorage.getItem(tagName));
if (!tagArr) {
2022-11-27 23:48:00 +00:00
tagArr = [];
}
tagArr.push(ID);
2022-11-29 22:16:17 +00:00
localStorage.setItem(tagName, JSON.stringify(tagArr));
2022-11-27 23:48:00 +00:00
}
}
2022-12-01 00:11:42 +00:00
/**
* Test Helper Function to get all reviews from local storage
* @returns {Object} all active reviews from local storage
*/
2022-11-29 02:48:55 +00:00
export function getAllReviewsFromStorage() {
if (!localStorage.getItem("activeIDS")) {
2022-11-29 02:48:55 +00:00
// we wanna init the active ID array and start the nextID count
localStorage.setItem("activeIDS", JSON.stringify([]));
localStorage.setItem("nextID", JSON.stringify(0));
2022-11-29 02:48:55 +00:00
}
//iterate thru activeIDS
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
let reviews = [];
for (let i = 0; i < activeIDS.length; i++) {
2022-11-29 02:48:55 +00:00
let currReview = JSON.parse(localStorage.getItem(`review${activeIDS[i]}`));
reviews.push(currReview);
}
return reviews;
}
/**
2022-12-01 00:11:42 +00:00
* Get all IDs of active reviews (order: most recent)
* @returns {number[]} list of all active IDs by recency
*/
export function getIDsFromStorage() {
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));
}
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
2022-12-01 00:11:42 +00:00
return activeIDS.reverse();
}
/**
2022-12-01 00:11:42 +00:00
* Returns all review IDs which contain the same tag specified (order: most recent)
* @param {string} tag to filter by
2022-12-01 00:11:42 +00:00
* @returns {number[]} list of IDs of reviews that all contain the specified tag by recency
*/
export function getIDsByTag(tag) {
2022-12-03 02:31:26 +00:00
let tagArr = JSON.parse(localStorage.getItem("!" + tag.toLocaleLowerCase()));
if (!tagArr) {
2022-12-01 00:11:42 +00:00
tagArr = [];
}
return tagArr.reverse();
}
/**
* Returns the top rated review IDs in order.
* @returns {number[]} list of IDs of reviews in order of top rating (most recent if equal rating)
*/
export function getTopIDsFromStorage() {
let resultArr = [];
for (let i = 5; i > 0; i--) {
let starArr = JSON.parse(localStorage.getItem(`star${i}`));
if (!starArr) {
continue;
}
resultArr = resultArr.concat(starArr.reverse());
}
return resultArr;
}