mirror of
https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
synced 2024-11-10 05:34:44 +00:00
tag storage
This commit is contained in:
parent
4c649e7770
commit
1484a882b2
@ -1,5 +1,5 @@
|
||||
//reviewDetails.js
|
||||
import {deleteReviewFromStorage, getReviewFromStorage, updateReviewToStorage} from "./localStorage.js";
|
||||
import {addTagsToStorage, deleteReviewFromStorage, deleteTagsFromStorage, getReviewFromStorage, updateReviewToStorage} from "./localStorage.js";
|
||||
|
||||
// Run the init() function when the page has loaded
|
||||
window.addEventListener("DOMContentLoaded", init);
|
||||
@ -10,6 +10,9 @@ function init(){
|
||||
setupUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the relevant data to the details from local storage review
|
||||
*/
|
||||
function setupInfo(){
|
||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||
let currReview = getReviewFromStorage(currID);
|
||||
@ -51,6 +54,9 @@ function setupInfo(){
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up delete button to delete review from storage and switch to homepage
|
||||
*/
|
||||
function setupDelete(){
|
||||
let deleteBtn = document.getElementById("delete-btn");
|
||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||
@ -63,6 +69,9 @@ function setupDelete(){
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up update button to reveal form and update info in storage and the current page
|
||||
*/
|
||||
function setupUpdate(){
|
||||
let updateBtn = document.getElementById("update-btn");
|
||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||
@ -141,10 +150,11 @@ function setupUpdate(){
|
||||
form.addEventListener("submit", function(){
|
||||
/*
|
||||
* User submits the form for their review.
|
||||
* We create reviewCard and put in storage
|
||||
* We create reviewCard data, replace in storage, and update tags
|
||||
*/
|
||||
let formData = new FormData(form);
|
||||
let newData = {};
|
||||
//iterate through formData and add to newData
|
||||
for (let [key, value] of formData) {
|
||||
console.log(`${key}`);
|
||||
console.log(`${value}`);
|
||||
@ -169,12 +179,23 @@ function setupUpdate(){
|
||||
|
||||
newData["reviewID"] = currID;
|
||||
|
||||
//Get diff of tags and update storage
|
||||
//Note: we can either get the difference twice or keep track when we delete and add
|
||||
//if keep track also need to make sure that a tag doesn't end up on both list
|
||||
let deletedTags = currReview["tags"].filter(x => !newData["tags"].includes(x));
|
||||
let addedTags = newData["tags"].filter(x => !currReview["tags"].includes(x));
|
||||
|
||||
deleteTagsFromStorage(currID, deletedTags);
|
||||
addTagsToStorage(currID, addedTags);
|
||||
|
||||
updateReviewToStorage(currID, newData);
|
||||
|
||||
updateDiv.classList.add("hidden");
|
||||
|
||||
});
|
||||
|
||||
//adding tag to form functionality
|
||||
//TODO: disable duplicate tags (use set?)
|
||||
let tagAddBtn = document.getElementById("tag-add-btn");
|
||||
tagAddBtn.addEventListener("click", ()=> {
|
||||
let tagField = document.getElementById("tag-form");
|
||||
|
@ -10,6 +10,9 @@ export function newReviewToStorage(review){
|
||||
|
||||
// set the review entry to the review object
|
||||
localStorage.setItem(`review${nextReviewId}`, JSON.stringify(review));
|
||||
|
||||
// adding to the tag keys
|
||||
addTagsToStorage(nextReviewId, review["tags"]);
|
||||
|
||||
//updating our activeIDS list
|
||||
let tempIdArr = JSON.parse(localStorage.getItem("activeIDS"));
|
||||
@ -52,6 +55,9 @@ export function deleteReviewFromStorage(ID){
|
||||
if (activeIDS[i] == ID) {
|
||||
activeIDS.splice(i,1);
|
||||
localStorage.setItem("activeIDS", JSON.stringify(activeIDS));
|
||||
//get review to delete all the tags(may wanna just add ID to a different list that will delete review and tag list in background)(also don't wanna linear search)
|
||||
let currReview = JSON.parse(localStorage.getItem(`review${ID}`));
|
||||
deleteTagsFromStorage(ID, currReview["tags"]);
|
||||
localStorage.removeItem(`review${ID}`);
|
||||
return;
|
||||
}
|
||||
@ -60,6 +66,45 @@ export function deleteReviewFromStorage(ID){
|
||||
console.error(`could not find review${ID} in localStorage`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete ID from the specified tags' storage
|
||||
* @param {string} ID to delete from lists
|
||||
* @param {string[]} deletedTags to modify storage of
|
||||
*/
|
||||
export function deleteTagsFromStorage(ID, deletedTags) {
|
||||
for(let i in deletedTags){
|
||||
//get local storage of each tag and remove id from tag list
|
||||
let tagArr = JSON.parse(localStorage.getItem("!"+ deletedTags[i]));
|
||||
for(let j in tagArr){
|
||||
if(tagArr[j] == ID){
|
||||
tagArr.splice(j,1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(tagArr.length != 0){
|
||||
localStorage.setItem("!" + deletedTags[i], JSON.stringify(tagArr));
|
||||
} else {
|
||||
localStorage.removeItem("!" + deletedTags[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add ID from the specified tags' storage
|
||||
* @param {string} ID to add to lists
|
||||
* @param {string[]} addedTags to modify storage of
|
||||
*/
|
||||
export function addTagsToStorage(ID, addedTags) {
|
||||
for(let i in addedTags){
|
||||
let tagArr = JSON.parse(localStorage.getItem("!" + addedTags[i]));
|
||||
if(!tagArr){
|
||||
tagArr = [];
|
||||
}
|
||||
tagArr.push(ID);
|
||||
localStorage.setItem("!" + addedTags[i], JSON.stringify(tagArr));
|
||||
}
|
||||
}
|
||||
|
||||
// legacy function
|
||||
export function getAllReviewsFromStorage() {
|
||||
if (!(localStorage.getItem("activeIDS"))) {
|
||||
|
Loading…
Reference in New Issue
Block a user