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
|
//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
|
// Run the init() function when the page has loaded
|
||||||
window.addEventListener("DOMContentLoaded", init);
|
window.addEventListener("DOMContentLoaded", init);
|
||||||
@ -10,6 +10,9 @@ function init(){
|
|||||||
setupUpdate();
|
setupUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populates the relevant data to the details from local storage review
|
||||||
|
*/
|
||||||
function setupInfo(){
|
function setupInfo(){
|
||||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||||
let currReview = getReviewFromStorage(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(){
|
function setupDelete(){
|
||||||
let deleteBtn = document.getElementById("delete-btn");
|
let deleteBtn = document.getElementById("delete-btn");
|
||||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
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(){
|
function setupUpdate(){
|
||||||
let updateBtn = document.getElementById("update-btn");
|
let updateBtn = document.getElementById("update-btn");
|
||||||
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||||
@ -141,10 +150,11 @@ function setupUpdate(){
|
|||||||
form.addEventListener("submit", function(){
|
form.addEventListener("submit", function(){
|
||||||
/*
|
/*
|
||||||
* User submits the form for their review.
|
* 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 formData = new FormData(form);
|
||||||
let newData = {};
|
let newData = {};
|
||||||
|
//iterate through formData and add to newData
|
||||||
for (let [key, value] of formData) {
|
for (let [key, value] of formData) {
|
||||||
console.log(`${key}`);
|
console.log(`${key}`);
|
||||||
console.log(`${value}`);
|
console.log(`${value}`);
|
||||||
@ -169,12 +179,23 @@ function setupUpdate(){
|
|||||||
|
|
||||||
newData["reviewID"] = currID;
|
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);
|
updateReviewToStorage(currID, newData);
|
||||||
|
|
||||||
updateDiv.classList.add("hidden");
|
updateDiv.classList.add("hidden");
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//adding tag to form functionality
|
||||||
|
//TODO: disable duplicate tags (use set?)
|
||||||
let tagAddBtn = document.getElementById("tag-add-btn");
|
let tagAddBtn = document.getElementById("tag-add-btn");
|
||||||
tagAddBtn.addEventListener("click", ()=> {
|
tagAddBtn.addEventListener("click", ()=> {
|
||||||
let tagField = document.getElementById("tag-form");
|
let tagField = document.getElementById("tag-form");
|
||||||
|
@ -11,6 +11,9 @@ export function newReviewToStorage(review){
|
|||||||
// set the review entry to the review object
|
// set the review entry to the review object
|
||||||
localStorage.setItem(`review${nextReviewId}`, JSON.stringify(review));
|
localStorage.setItem(`review${nextReviewId}`, JSON.stringify(review));
|
||||||
|
|
||||||
|
// adding to the tag keys
|
||||||
|
addTagsToStorage(nextReviewId, review["tags"]);
|
||||||
|
|
||||||
//updating our activeIDS list
|
//updating our activeIDS list
|
||||||
let tempIdArr = JSON.parse(localStorage.getItem("activeIDS"));
|
let tempIdArr = JSON.parse(localStorage.getItem("activeIDS"));
|
||||||
tempIdArr.push(nextReviewId);
|
tempIdArr.push(nextReviewId);
|
||||||
@ -52,6 +55,9 @@ export function deleteReviewFromStorage(ID){
|
|||||||
if (activeIDS[i] == ID) {
|
if (activeIDS[i] == ID) {
|
||||||
activeIDS.splice(i,1);
|
activeIDS.splice(i,1);
|
||||||
localStorage.setItem("activeIDS", JSON.stringify(activeIDS));
|
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}`);
|
localStorage.removeItem(`review${ID}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -60,6 +66,45 @@ export function deleteReviewFromStorage(ID){
|
|||||||
console.error(`could not find review${ID} in localStorage`);
|
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
|
// legacy function
|
||||||
export function getAllReviewsFromStorage() {
|
export function getAllReviewsFromStorage() {
|
||||||
if (!(localStorage.getItem("activeIDS"))) {
|
if (!(localStorage.getItem("activeIDS"))) {
|
||||||
|
Loading…
Reference in New Issue
Block a user