mirror of
				https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
				synced 2025-10-31 03:46:50 +00:00 
			
		
		
		
	No tag duplicates
This commit is contained in:
		| @@ -3,11 +3,7 @@ import { newReviewToStorage } from "./localStorage.js"; | |||||||
| window.addEventListener("DOMContentLoaded", init); | window.addEventListener("DOMContentLoaded", init); | ||||||
|  |  | ||||||
| function init() { | function init() { | ||||||
| 	// get next id |  | ||||||
|  |  | ||||||
| 	// creates the key |  | ||||||
| 	initFormHandler(); | 	initFormHandler(); | ||||||
|      |  | ||||||
| } | } | ||||||
|  |  | ||||||
| function initFormHandler() { | function initFormHandler() { | ||||||
| @@ -91,19 +87,26 @@ function initFormHandler() { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	let tagAddBtn = document.getElementById("tag-add-btn"); | 	let tagAddBtn = document.getElementById("tag-add-btn"); | ||||||
|  | 	//Set used to track tags and ensure no duplicates | ||||||
|  | 	let tagSet = new Set(); | ||||||
| 	tagAddBtn.addEventListener("click", ()=> { | 	tagAddBtn.addEventListener("click", ()=> { | ||||||
| 		let tagField = document.getElementById("tag-form"); | 		let tagField = document.getElementById("tag-form"); | ||||||
| 		if (tagField.value.length > 0) { | 		if (tagField.value.length > 0) { | ||||||
|  | 			if (!tagSet.has(tagField.value.toLowerCase())){ | ||||||
| 				let tagLabel = document.createElement("label"); | 				let tagLabel = document.createElement("label"); | ||||||
| 				tagLabel.innerHTML = tagField.value; | 				tagLabel.innerHTML = tagField.value; | ||||||
| 				tagLabel.setAttribute("class","tag"); | 				tagLabel.setAttribute("class","tag"); | ||||||
|  | 				tagSet.add(tagField.value.toLowerCase()); | ||||||
| 				tagLabel.addEventListener("click",()=> { | 				tagLabel.addEventListener("click",()=> { | ||||||
| 					tagContainer.removeChild(tagLabel); | 					tagContainer.removeChild(tagLabel); | ||||||
|  | 					tagSet.delete(tagField.value.toLowerCase()); | ||||||
| 				}); | 				}); | ||||||
| 		 | 		 | ||||||
| 				tagContainer.append(tagLabel); | 				tagContainer.append(tagLabel); | ||||||
|  | 			} else { | ||||||
|  | 				window.alert("No duplicate tags allowed"); | ||||||
|  | 			} | ||||||
| 			tagField.value = ""; | 			tagField.value = ""; | ||||||
|  |  | ||||||
| 		} | 		} | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| //reviewDetails.js | //reviewDetails.js | ||||||
| import {addTagsToStorage, deleteReviewFromStorage, deleteTagsFromStorage, getReviewFromStorage, updateReviewToStorage} from "./localStorage.js"; | import {deleteReviewFromStorage, 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); | ||||||
| @@ -92,16 +92,21 @@ function setupUpdate(){ | |||||||
| 		document.getElementById("s" + `${currReview["rating"]}`).checked = true; | 		document.getElementById("s" + `${currReview["rating"]}`).checked = true; | ||||||
| 		document.getElementById("restaurant").defaultValue = currReview["restaurant"]; | 		document.getElementById("restaurant").defaultValue = currReview["restaurant"]; | ||||||
|  |  | ||||||
|  | 		//Set used to track tags and ensure no duplicates | ||||||
|  | 		let tagSet = new Set(); | ||||||
|  |  | ||||||
| 		if(currReview["tags"]){ | 		if(currReview["tags"]){ | ||||||
| 			while (tagContainer.firstChild) { | 			while (tagContainer.firstChild) { | ||||||
| 				tagContainer.removeChild(tagContainer.firstChild); | 				tagContainer.removeChild(tagContainer.firstChild); | ||||||
| 			} | 			} | ||||||
| 			for (let i = 0; i < currReview["tags"].length; i++) { | 			for (let i = 0; i < currReview["tags"].length; i++) { | ||||||
|  | 				tagSet.add(currReview["tags"][i].toLowerCase()); | ||||||
| 				let newTag = document.createElement("label"); | 				let newTag = document.createElement("label"); | ||||||
| 				newTag.setAttribute("class","tag"); | 				newTag.setAttribute("class","tag"); | ||||||
| 				newTag.innerHTML = currReview["tags"][i]; | 				newTag.innerHTML = currReview["tags"][i]; | ||||||
| 				newTag.addEventListener("click",()=> { | 				newTag.addEventListener("click",()=> { | ||||||
| 					tagContainer.removeChild(newTag); | 					tagContainer.removeChild(newTag); | ||||||
|  | 					tagSet.delete(currReview["tags"][i].toLowerCase()); | ||||||
| 				}); | 				}); | ||||||
| 				tagContainer.append(newTag); | 				tagContainer.append(newTag); | ||||||
| 			} | 			} | ||||||
| @@ -186,19 +191,24 @@ function setupUpdate(){ | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		//adding tag to form functionality | 		//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"); | ||||||
| 			if (tagField.value.length > 0) { | 			if (tagField.value.length > 0) { | ||||||
|  | 				if (!tagSet.has(tagField.value.toLowerCase())){ | ||||||
| 					let tagLabel = document.createElement("label"); | 					let tagLabel = document.createElement("label"); | ||||||
| 					tagLabel.innerHTML = tagField.value; | 					tagLabel.innerHTML = tagField.value; | ||||||
| 					tagLabel.setAttribute("class","tag"); | 					tagLabel.setAttribute("class","tag"); | ||||||
|  | 					tagSet.add(tagField.value.toLowerCase()); | ||||||
| 					tagLabel.addEventListener("click",()=> { | 					tagLabel.addEventListener("click",()=> { | ||||||
| 						tagContainer.removeChild(tagLabel); | 						tagContainer.removeChild(tagLabel); | ||||||
|  | 						tagSet.delete(tagField.value.toLowerCase()); | ||||||
| 					}); | 					}); | ||||||
| 			 | 			 | ||||||
| 					tagContainer.append(tagLabel); | 					tagContainer.append(tagLabel); | ||||||
|  | 				} else { | ||||||
|  | 					window.alert("No duplicate tags allowed"); | ||||||
|  | 				} | ||||||
| 				tagField.value = ""; | 				tagField.value = ""; | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
|   | |||||||
| @@ -45,8 +45,8 @@ export function updateReviewToStorage(ID, review){ | |||||||
| 	//Get diff of tags and update storage | 	//Get diff of tags and update storage | ||||||
| 	let deletedTags = oldReview["tags"].filter(x => !review["tags"].includes(x)); | 	let deletedTags = oldReview["tags"].filter(x => !review["tags"].includes(x)); | ||||||
| 	let addedTags = review["tags"].filter(x => !oldReview["tags"].includes(x)); | 	let addedTags = review["tags"].filter(x => !oldReview["tags"].includes(x)); | ||||||
| 	deleteTagsFromStorage(currID, deletedTags); | 	deleteTagsFromStorage(ID, deletedTags); | ||||||
| 	addTagsToStorage(currID, addedTags); | 	addTagsToStorage(ID, addedTags); | ||||||
|  |  | ||||||
| 	// set the review entry with ID to the review object | 	// set the review entry with ID to the review object | ||||||
| 	localStorage.setItem(`review${ID}`, JSON.stringify(review)); | 	localStorage.setItem(`review${ID}`, JSON.stringify(review)); | ||||||
| @@ -63,7 +63,6 @@ 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}`)); | 			let currReview = JSON.parse(localStorage.getItem(`review${ID}`)); | ||||||
| 			deleteTagsFromStorage(ID, currReview["tags"]); | 			deleteTagsFromStorage(ID, currReview["tags"]); | ||||||
| 			localStorage.removeItem(`review${ID}`); | 			localStorage.removeItem(`review${ID}`); | ||||||
| @@ -79,10 +78,11 @@ export function deleteReviewFromStorage(ID){ | |||||||
|  * @param {string} ID to delete from lists |  * @param {string} ID to delete from lists | ||||||
|  * @param {string[]} deletedTags to modify storage of |  * @param {string[]} deletedTags to modify storage of | ||||||
|  */ |  */ | ||||||
| export function deleteTagsFromStorage(ID, deletedTags) { | function deleteTagsFromStorage(ID, deletedTags) { | ||||||
| 	for(let i in deletedTags){ | 	for(let i in deletedTags){ | ||||||
| 		//get local storage of each tag and remove id from tag list | 		//get local storage of each tag and remove id from tag list | ||||||
| 		let tagArr = JSON.parse(localStorage.getItem("!"+ deletedTags[i])); | 		let tagName = "!"+ deletedTags[i]; | ||||||
|  | 		let tagArr = JSON.parse(localStorage.getItem(tagName.toLowerCase())); | ||||||
| 		for(let j in tagArr){ | 		for(let j in tagArr){ | ||||||
| 			if(tagArr[j] == ID){ | 			if(tagArr[j] == ID){ | ||||||
| 				tagArr.splice(j,1); | 				tagArr.splice(j,1); | ||||||
| @@ -90,9 +90,9 @@ export function deleteTagsFromStorage(ID, deletedTags) { | |||||||
| 			break; | 			break; | ||||||
| 		} | 		} | ||||||
| 		if(tagArr.length != 0){ | 		if(tagArr.length != 0){ | ||||||
| 			localStorage.setItem("!" + deletedTags[i], JSON.stringify(tagArr)); | 			localStorage.setItem(tagName.toLowerCase(), JSON.stringify(tagArr)); | ||||||
| 		} else { | 		} else { | ||||||
| 			localStorage.removeItem("!" + deletedTags[i]); | 			localStorage.removeItem(tagName.toLowerCase()); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -102,34 +102,18 @@ export function deleteTagsFromStorage(ID, deletedTags) { | |||||||
|  * @param {string} ID to add to lists |  * @param {string} ID to add to lists | ||||||
|  * @param {string[]} addedTags to modify storage of |  * @param {string[]} addedTags to modify storage of | ||||||
|  */ |  */ | ||||||
| export function addTagsToStorage(ID, addedTags) { | function addTagsToStorage(ID, addedTags) { | ||||||
| 	for(let i in addedTags){ | 	for(let i in addedTags){ | ||||||
| 		let tagArr = JSON.parse(localStorage.getItem("!" + addedTags[i])); | 		let tagName = "!" + addedTags[i]; | ||||||
|  | 		let tagArr = JSON.parse(localStorage.getItem(tagName.toLowerCase())); | ||||||
| 		if(!tagArr){ | 		if(!tagArr){ | ||||||
| 			tagArr = []; | 			tagArr = []; | ||||||
| 		} | 		} | ||||||
| 		tagArr.push(ID); | 		tagArr.push(ID); | ||||||
| 		localStorage.setItem("!" + addedTags[i], JSON.stringify(tagArr)); | 		localStorage.setItem(tagName.toLowerCase(), JSON.stringify(tagArr)); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // legacy function |  | ||||||
| export function getAllReviewsFromStorage() { |  | ||||||
| 	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; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Returns the top n reviews by ID. If there are less than n reviews, returns the most possible.  |  * Returns the top n reviews by ID. If there are less than n reviews, returns the most possible.  | ||||||
|  * @param {number} n number of reviews to return |  * @param {number} n number of reviews to return | ||||||
| @@ -147,3 +131,20 @@ export function getTopReviewsFromStorage(n) { | |||||||
| export function getReviewsByTag(tag) { | export function getReviewsByTag(tag) { | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // legacy function | ||||||
|  | export function getAllReviewsFromStorage() { | ||||||
|  | 	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; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user