mirror of
				https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
				synced 2025-10-31 03:46:50 +00:00 
			
		
		
		
	| @@ -1,18 +1,18 @@ | |||||||
| // ReviewCard.js | // ReviewCard.js | ||||||
|  |  | ||||||
| class ReviewCard extends HTMLElement { | class ReviewCard extends HTMLElement { | ||||||
|   // Called once when document.createElement('review-card') is called, or | 	// Called once when document.createElement('review-card') is called, or | ||||||
|   // the element is written into the DOM directly as <review-card> | 	// the element is written into the DOM directly as <review-card> | ||||||
|   constructor() { | 	constructor() { | ||||||
|     super();  | 		super();  | ||||||
|  |  | ||||||
|  |  | ||||||
|     let shadowEl = this.attachShadow({mode:'open'}); | 		let shadowEl = this.attachShadow({mode:"open"}); | ||||||
|  |  | ||||||
|     let articleEl = document.createElement('article'); | 		let articleEl = document.createElement("article"); | ||||||
|  |  | ||||||
|     let styleEl = document.createElement('style'); | 		let styleEl = document.createElement("style"); | ||||||
|     styleEl.textContent = ` | 		styleEl.textContent = ` | ||||||
|       * { |       * { | ||||||
|         font-family: sans-serif; |         font-family: sans-serif; | ||||||
|         margin: 0; |         margin: 0; | ||||||
| @@ -82,17 +82,17 @@ class ReviewCard extends HTMLElement { | |||||||
|         font-size: 12px; |         font-size: 12px; | ||||||
|       } |       } | ||||||
|     `; |     `; | ||||||
|     articleEl.append(styleEl); | 		articleEl.append(styleEl); | ||||||
|     shadowEl.append(articleEl); | 		shadowEl.append(articleEl); | ||||||
|     this.shadowEl = shadowEl; | 		this.shadowEl = shadowEl; | ||||||
|     //attach event listener to each recipe-card | 		//attach event listener to each recipe-card | ||||||
|     this.addEventListener('click', (event) => { | 		this.addEventListener("click", (event) => { | ||||||
|       console.log(event.target); | 			console.log(event.target); | ||||||
|       console.log(event.target.data); | 			console.log(event.target.data); | ||||||
|       //Option 1: sending current data to second html page using localStorage (could also just store index) | 			//Option 1: sending current data to second html page using localStorage (could also just store index) | ||||||
|       sessionStorage.setItem('current', JSON.stringify(event.target.data)); | 			sessionStorage.setItem("current", JSON.stringify(event.target.data)); | ||||||
|       window.location.assign("./ReviewDetails.html"); | 			window.location.assign("./ReviewDetails.html"); | ||||||
|       /* | 			/* | ||||||
|       //Option 2: sending current data to second html page using string query w/ url (currently not storing value) |       //Option 2: sending current data to second html page using string query w/ url (currently not storing value) | ||||||
|       let reviewFields = window.location.search.slice(1).split("&"); |       let reviewFields = window.location.search.slice(1).split("&"); | ||||||
|       for(let i = 0; i < reviewFields.length; i++) { |       for(let i = 0; i < reviewFields.length; i++) { | ||||||
| @@ -103,10 +103,10 @@ class ReviewCard extends HTMLElement { | |||||||
|         console.log(value); |         console.log(value); | ||||||
|         // What you want to do with name and value... |         // What you want to do with name and value... | ||||||
|       }*/ |       }*/ | ||||||
|     }); | 		}); | ||||||
|   } | 	} | ||||||
|  |  | ||||||
|   /** | 	/** | ||||||
|    * Called when the .data property is set on this element. |    * Called when the .data property is set on this element. | ||||||
|    * |    * | ||||||
|    * For Example: |    * For Example: | ||||||
| @@ -125,29 +125,29 @@ class ReviewCard extends HTMLElement { | |||||||
|    *                          "tags": string array |    *                          "tags": string array | ||||||
|    *                        } |    *                        } | ||||||
|    */ |    */ | ||||||
|   set data(data) { | 	set data(data) { | ||||||
|     // If nothing was passed in, return | 		// If nothing was passed in, return | ||||||
|     if (!data) return; | 		if (!data) return; | ||||||
|  |  | ||||||
|     // Select the <article> we added to the Shadow DOM in the constructor | 		// Select the <article> we added to the Shadow DOM in the constructor | ||||||
|     let articleEl = this.shadowEl.querySelector('article'); | 		let articleEl = this.shadowEl.querySelector("article"); | ||||||
|      |      | ||||||
|     // setting the article elements for the review card | 		// setting the article elements for the review card | ||||||
|  |  | ||||||
|     //image setup | 		//image setup | ||||||
|     let mealImg = document.createElement('img'); | 		let mealImg = document.createElement("img"); | ||||||
|     mealImg.setAttribute('id', 'a-mealImg'); | 		mealImg.setAttribute("id", "a-mealImg"); | ||||||
|     mealImg.setAttribute('src',data['mealImg']); | 		mealImg.setAttribute("src",data["mealImg"]); | ||||||
|     mealImg.setAttribute('alt',data['imgAlt']); | 		mealImg.setAttribute("alt",data["imgAlt"]); | ||||||
|  |  | ||||||
|     //meal name setup | 		//meal name setup | ||||||
|     let mealLabel = document.createElement('label'); | 		let mealLabel = document.createElement("label"); | ||||||
|     mealLabel.setAttribute('id', 'a-mealName'); | 		mealLabel.setAttribute("id", "a-mealName"); | ||||||
|     mealLabel.setAttribute('class','meal-name'); | 		mealLabel.setAttribute("class","meal-name"); | ||||||
|     mealLabel.innerHTML = data['mealName']; | 		mealLabel.innerHTML = data["mealName"]; | ||||||
|  |  | ||||||
|     //restaurant name setup | 		//restaurant name setup | ||||||
| /* | 		/* | ||||||
|     //review page link |     //review page link | ||||||
|     //giving it functionality to save the review card's info to session storage for loading the review page |     //giving it functionality to save the review card's info to session storage for loading the review page | ||||||
|     let reviewLink = document.createElement('a'); |     let reviewLink = document.createElement('a'); | ||||||
| @@ -167,53 +167,53 @@ class ReviewCard extends HTMLElement { | |||||||
|       sessionStorage.setItem('currReview', JSON.stringify(currReview)); |       sessionStorage.setItem('currReview', JSON.stringify(currReview)); | ||||||
|     }); |     }); | ||||||
| */ | */ | ||||||
|     let restaurantLabel = document.createElement('label'); | 		let restaurantLabel = document.createElement("label"); | ||||||
|     restaurantLabel.setAttribute('id', 'a-restaurant'); | 		restaurantLabel.setAttribute("id", "a-restaurant"); | ||||||
|     restaurantLabel.setAttribute('class','restaurant-name'); | 		restaurantLabel.setAttribute("class","restaurant-name"); | ||||||
|     restaurantLabel.innerHTML = data['restaurant']; | 		restaurantLabel.innerHTML = data["restaurant"]; | ||||||
|  |  | ||||||
|     //comment section setup (display set to none) | 		//comment section setup (display set to none) | ||||||
|     let comments = document.createElement('p'); | 		let comments = document.createElement("p"); | ||||||
|     comments.setAttribute('id', 'a-comments'); | 		comments.setAttribute("id", "a-comments"); | ||||||
|     comments.style.display = 'none'; | 		comments.style.display = "none"; | ||||||
|     comments.innerText = data['comments']; | 		comments.innerText = data["comments"]; | ||||||
|  |  | ||||||
|     //other info: rating | 		//other info: rating | ||||||
|     let ratingDiv = document.createElement('div'); | 		let ratingDiv = document.createElement("div"); | ||||||
|     ratingDiv.setAttribute('class', 'rating'); | 		ratingDiv.setAttribute("class", "rating"); | ||||||
|     let starsImg = document.createElement('img'); | 		let starsImg = document.createElement("img"); | ||||||
|     starsImg.setAttribute('id', 'a-rating'); | 		starsImg.setAttribute("id", "a-rating"); | ||||||
|     starsImg.setAttribute('src', './source/assets/images/icons/'+data['rating']+'-star.svg'); | 		starsImg.setAttribute("src", "./source/assets/images/icons/"+data["rating"]+"-star.svg"); | ||||||
|     starsImg.setAttribute('alt', data['rating'] +' stars'); | 		starsImg.setAttribute("alt", data["rating"] +" stars"); | ||||||
|     starsImg.setAttribute('num', data['rating']); | 		starsImg.setAttribute("num", data["rating"]); | ||||||
|     ratingDiv.append(starsImg); | 		ratingDiv.append(starsImg); | ||||||
|  |  | ||||||
|     //added tags | 		//added tags | ||||||
|     let tagContainer = document.createElement('div'); | 		let tagContainer = document.createElement("div"); | ||||||
|     tagContainer.setAttribute('class', 'tag-container'); | 		tagContainer.setAttribute("class", "tag-container"); | ||||||
|     tagContainer.setAttribute('id', 'a-tags'); | 		tagContainer.setAttribute("id", "a-tags"); | ||||||
|     tagContainer.setAttribute('list', data['tags']); | 		tagContainer.setAttribute("list", data["tags"]); | ||||||
|     if(data['tags']){ | 		if(data["tags"]){ | ||||||
|       for (let i = 0; i < data['tags'].length; i++) { | 			for (let i = 0; i < data["tags"].length; i++) { | ||||||
|         let newTag = document.createElement('label'); | 				let newTag = document.createElement("label"); | ||||||
|         newTag.setAttribute('class','tag'); | 				newTag.setAttribute("class","tag"); | ||||||
|         newTag.innerHTML = data['tags'][i] + "   "; | 				newTag.innerHTML = data["tags"][i] + "   "; | ||||||
|         tagContainer.append(newTag); | 				tagContainer.append(newTag); | ||||||
|       } | 			} | ||||||
|     } | 		} | ||||||
|  |  | ||||||
|     articleEl.append(mealImg); | 		articleEl.append(mealImg); | ||||||
|     articleEl.append(mealLabel); | 		articleEl.append(mealLabel); | ||||||
|     //articleEl.append(reviewLink) | 		//articleEl.append(reviewLink) | ||||||
|     articleEl.append(restaurantLabel); | 		articleEl.append(restaurantLabel); | ||||||
|     articleEl.append(ratingDiv); | 		articleEl.append(ratingDiv); | ||||||
|     articleEl.append(tagContainer); | 		articleEl.append(tagContainer); | ||||||
|     articleEl.append(comments); | 		articleEl.append(comments); | ||||||
|  |  | ||||||
|  |  | ||||||
|   } | 	} | ||||||
|  |  | ||||||
|     /** | 	/** | ||||||
|    * Called when getting the .data property of this element. |    * Called when getting the .data property of this element. | ||||||
|    * |    * | ||||||
|    * For Example: |    * For Example: | ||||||
| @@ -232,39 +232,39 @@ class ReviewCard extends HTMLElement { | |||||||
|    *                          "tags": string array |    *                          "tags": string array | ||||||
|    *                        } |    *                        } | ||||||
|    */ |    */ | ||||||
|   get data() { | 	get data() { | ||||||
|  |  | ||||||
|     let dataContainer = {}; | 		let dataContainer = {}; | ||||||
|      |      | ||||||
|     // getting the article elements for the review card | 		// getting the article elements for the review card | ||||||
|  |  | ||||||
|     //get image | 		//get image | ||||||
|     let mealImg = this.shadowEl.getElementById('a-mealImg'); | 		let mealImg = this.shadowEl.getElementById("a-mealImg"); | ||||||
|     dataContainer['mealImg'] = mealImg.getAttribute('src'); | 		dataContainer["mealImg"] = mealImg.getAttribute("src"); | ||||||
|     dataContainer['imgAlt'] = mealImg.getAttribute('alt'); | 		dataContainer["imgAlt"] = mealImg.getAttribute("alt"); | ||||||
|  |  | ||||||
|     //get meal name | 		//get meal name | ||||||
|     let mealLabel = this.shadowEl.getElementById('a-mealName'); | 		let mealLabel = this.shadowEl.getElementById("a-mealName"); | ||||||
|     dataContainer['mealName'] = mealLabel.innerHTML; | 		dataContainer["mealName"] = mealLabel.innerHTML; | ||||||
|  |  | ||||||
|     //get comment section | 		//get comment section | ||||||
|     let comments = this.shadowEl.getElementById('a-comments'); | 		let comments = this.shadowEl.getElementById("a-comments"); | ||||||
|     console.log(comments); | 		console.log(comments); | ||||||
|     dataContainer['comments'] = comments.innerText; | 		dataContainer["comments"] = comments.innerText; | ||||||
|  |  | ||||||
|     //get other info: rating | 		//get other info: rating | ||||||
|     let starsImg = this.shadowEl.getElementById('a-rating'); | 		let starsImg = this.shadowEl.getElementById("a-rating"); | ||||||
|     dataContainer['rating'] = starsImg.getAttribute('num'); | 		dataContainer["rating"] = starsImg.getAttribute("num"); | ||||||
|  |  | ||||||
|     //get restaurant name | 		//get restaurant name | ||||||
|     let restaurantLabel = this.shadowEl.getElementById('a-restaurant'); | 		let restaurantLabel = this.shadowEl.getElementById("a-restaurant"); | ||||||
|     dataContainer['restaurant'] = restaurantLabel.innerHTML; | 		dataContainer["restaurant"] = restaurantLabel.innerHTML; | ||||||
|  |  | ||||||
|     //get tags | 		//get tags | ||||||
|     let tagContainer = this.shadowEl.getElementById('a-tags'); | 		let tagContainer = this.shadowEl.getElementById("a-tags"); | ||||||
|     dataContainer['tags'] = tagContainer.getAttribute('list').split(","); | 		dataContainer["tags"] = tagContainer.getAttribute("list").split(","); | ||||||
|  |  | ||||||
|     return dataContainer; | 		return dataContainer; | ||||||
|   } | 	} | ||||||
| } | } | ||||||
| customElements.define('review-card', ReviewCard); | customElements.define("review-card", ReviewCard); | ||||||
|   | |||||||
| @@ -1,127 +1,125 @@ | |||||||
| //reviewDetails.js | //reviewDetails.js | ||||||
| import {getReviewsFromStorage, saveReviewsToStorage} from './localStorage.js'; | import {getReviewsFromStorage, saveReviewsToStorage} 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); | ||||||
|  |  | ||||||
| function init(){ | function init(){ | ||||||
|     let main = document.querySelector('main'); | 	setupDelete(); | ||||||
|     let reviews = getReviewsFromStorage(); | 	setupUpdate(); | ||||||
|     setupDelete(); |  | ||||||
|     setupUpdate(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| function setupDelete(){ | function setupDelete(){ | ||||||
|     let deleteBtn = document.getElementById('delete'); | 	let deleteBtn = document.getElementById("delete"); | ||||||
|     let reviews = getReviewsFromStorage(); | 	let reviews = getReviewsFromStorage(); | ||||||
|     let current = JSON.parse(sessionStorage.getItem('current')); | 	let current = JSON.parse(sessionStorage.getItem("current")); | ||||||
|     deleteBtn.addEventListener('click', function(){ | 	deleteBtn.addEventListener("click", function(){ | ||||||
|         if(window.confirm("Are you sure you want to delete this entry?")){ | 		if(window.confirm("Are you sure you want to delete this entry?")){ | ||||||
|             //delete function | 			//delete function | ||||||
|             if(current){ | 			if(current){ | ||||||
|                 console.log(current); | 				console.log(current); | ||||||
|                 for(let i = 0; i < reviews.length; i++){ | 				for(let i = 0; i < reviews.length; i++){ | ||||||
|                     console.log(reviews[i]); | 					console.log(reviews[i]); | ||||||
|                     if(reviews[i]['mealName'] == current['mealName'] && reviews[i]['restaurant'] == current['restaurant']){ | 					if(reviews[i]["mealName"] == current["mealName"] && reviews[i]["restaurant"] == current["restaurant"]){ | ||||||
|                         console.log("match found"); | 						console.log("match found"); | ||||||
|                         reviews.splice(i,1); | 						reviews.splice(i,1); | ||||||
|                         saveReviewsToStorage(reviews); | 						saveReviewsToStorage(reviews); | ||||||
|                         sessionStorage.removeItem('current'); | 						sessionStorage.removeItem("current"); | ||||||
|                         window.location.assign("./index.html"); | 						window.location.assign("./index.html"); | ||||||
|                         break; | 						break; | ||||||
|                     } | 					} | ||||||
|                 }; | 				} | ||||||
|             } | 			} | ||||||
|         } | 		} | ||||||
|     }); | 	}); | ||||||
| } | } | ||||||
|  |  | ||||||
| function setupUpdate(){ | function setupUpdate(){ | ||||||
|     let updateBtn = document.getElementById('update'); | 	let updateBtn = document.getElementById("update"); | ||||||
|     let reviews = getReviewsFromStorage(); | 	let reviews = getReviewsFromStorage(); | ||||||
|     let current = JSON.parse(sessionStorage.getItem('current')); | 	let current = JSON.parse(sessionStorage.getItem("current")); | ||||||
|     let form = document.getElementById('update-food-entry'); | 	let form = document.getElementById("update-food-entry"); | ||||||
|     updateBtn.addEventListener('click', function(){ | 	updateBtn.addEventListener("click", function(){ | ||||||
|         //update function | 		//update function | ||||||
|         if(current){ | 		if(current){ | ||||||
|             console.log(current); | 			console.log(current); | ||||||
|             form.style.display = 'block'; | 			form.style.display = "block"; | ||||||
|             let tagContainer = document.getElementById('tag-container-form'); | 			let tagContainer = document.getElementById("tag-container-form"); | ||||||
|             console.log(document.querySelectorAll('#update-food-entry input')); | 			console.log(document.querySelectorAll("#update-food-entry input")); | ||||||
|  |  | ||||||
|             //Set value of each input element to current's values | 			//Set value of each input element to current's values | ||||||
|             document.getElementById('mealImg').defaultValue = current['mealImg']; | 			document.getElementById("mealImg").defaultValue = current["mealImg"]; | ||||||
|             document.getElementById('imgAlt').defaultValue = current['imgAlt']; | 			document.getElementById("imgAlt").defaultValue = current["imgAlt"]; | ||||||
|             document.getElementById('mealName').defaultValue = current['mealName']; | 			document.getElementById("mealName").defaultValue = current["mealName"]; | ||||||
|             document.getElementById('comments').textContent = current['comments']; | 			document.getElementById("comments").textContent = current["comments"]; | ||||||
|             document.getElementById('rating-' + `${current['rating']}`).checked = true; | 			document.getElementById("rating-" + `${current["rating"]}`).checked = true; | ||||||
|             document.getElementById('restaurant').defaultValue = current['restaurant']; | 			document.getElementById("restaurant").defaultValue = current["restaurant"]; | ||||||
|  |  | ||||||
|             if(current['tags']){ | 			if(current["tags"]){ | ||||||
|                 for (let i = 0; i < current['tags'].length; i++) { | 				for (let i = 0; i < current["tags"].length; i++) { | ||||||
|                   let newTag = document.createElement('label'); | 					let newTag = document.createElement("label"); | ||||||
|                   newTag.setAttribute('class','tag'); | 					newTag.setAttribute("class","tag"); | ||||||
|                   newTag.innerHTML = current['tags'][i] + "   "; | 					newTag.innerHTML = current["tags"][i] + "   "; | ||||||
|                   newTag.addEventListener('click',()=> { | 					newTag.addEventListener("click",()=> { | ||||||
|                     tagContainer.removeChild(newTag); | 						tagContainer.removeChild(newTag); | ||||||
|                   }); | 					}); | ||||||
|                   tagContainer.append(newTag); | 					tagContainer.append(newTag); | ||||||
|                 } | 				} | ||||||
|             } | 			} | ||||||
|  |  | ||||||
|             //Take formdata values as newData when submit | 			//Take formdata values as newData when submit | ||||||
|             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 and put in storage | ||||||
|                 */ |                 */ | ||||||
|                 let formData = new FormData(form); | 				let formData = new FormData(form); | ||||||
|                 let newData = {}; | 				let 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}`); | ||||||
|                   if (`${key}` !== "tag-form") { | 					if (`${key}` !== "tag-form") { | ||||||
|                     newData[`${key}`] = `${value}`; | 						newData[`${key}`] = `${value}`; | ||||||
|                   } | 					} | ||||||
|                 } | 				} | ||||||
|                 newData['tags'] = []; | 				newData["tags"] = []; | ||||||
|              |              | ||||||
|                 let tags = document.querySelectorAll('.tag'); | 				let tags = document.querySelectorAll(".tag"); | ||||||
|                 for(let i = 0; i < tags.length; i ++) { | 				for(let i = 0; i < tags.length; i ++) { | ||||||
|                   newData['tags'].push(tags[i].innerHTML); | 					newData["tags"].push(tags[i].innerHTML); | ||||||
|                   tagContainer.removeChild(tags[i]); | 					tagContainer.removeChild(tags[i]); | ||||||
|                 } | 				} | ||||||
|  |  | ||||||
|                 for(let i = 0; i < reviews.length; i++){ | 				for(let i = 0; i < reviews.length; i++){ | ||||||
|                     console.log(reviews[i]); | 					console.log(reviews[i]); | ||||||
|                     if(reviews[i]['mealName'] == current['mealName'] && reviews[i]['restaurant'] == current['restaurant']){ | 					if(reviews[i]["mealName"] == current["mealName"] && reviews[i]["restaurant"] == current["restaurant"]){ | ||||||
|                         console.log("match found"); | 						console.log("match found"); | ||||||
|                         reviews.splice(i,1,newData); | 						reviews.splice(i,1,newData); | ||||||
|                         saveReviewsToStorage(reviews); | 						saveReviewsToStorage(reviews); | ||||||
|                         sessionStorage.setItem('current', JSON.stringify(newData)); | 						sessionStorage.setItem("current", JSON.stringify(newData)); | ||||||
|                         break; | 						break; | ||||||
|                     } | 					} | ||||||
|                 }; | 				} | ||||||
|  |  | ||||||
|                 form.style.display = 'none'; | 				form.style.display = "none"; | ||||||
|  |  | ||||||
|             }); | 			}); | ||||||
|  |  | ||||||
|             let tagAddBtn = document.getElementById('tagAdd'); | 			let tagAddBtn = document.getElementById("tagAdd"); | ||||||
|             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) { | ||||||
|                 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"); | ||||||
|                 tagLabel.addEventListener('click',()=> { | 					tagLabel.addEventListener("click",()=> { | ||||||
|                   tagContainer.removeChild(tagLabel); | 						tagContainer.removeChild(tagLabel); | ||||||
|                 }); | 					}); | ||||||
|                  |                  | ||||||
|                 tagContainer.append(tagLabel); | 					tagContainer.append(tagLabel); | ||||||
|                 tagField.value = ''; | 					tagField.value = ""; | ||||||
|               } | 				} | ||||||
|             }); | 			}); | ||||||
|         } | 		} | ||||||
|     }); | 	}); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,13 +1,13 @@ | |||||||
| // 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); | ||||||
|  |  | ||||||
| function init() { | function init() { | ||||||
|     let result = sessionStorage.getItem('currReview') | 	let result = sessionStorage.getItem("currReview"); | ||||||
|  |  | ||||||
|     let main = document.querySelector('main'); | 	let main = document.querySelector("main"); | ||||||
|      |      | ||||||
|     main.innerHTML = result | 	main.innerHTML = result; | ||||||
|     let p = document.createElement('p') | 	let p = document.createElement("p"); | ||||||
|     p.innerHTML = JSON.parse(result)['comments'] | 	p.innerHTML = JSON.parse(result)["comments"]; | ||||||
|     main.append(p) | 	main.append(p); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
|  * @returns {Array<Object>} An array of reviews found in localStorage |  * @returns {Array<Object>} An array of reviews found in localStorage | ||||||
|  */ |  */ | ||||||
| export function getReviewsFromStorage() { | export function getReviewsFromStorage() { | ||||||
| 	let result = JSON.parse(localStorage.getItem('reviews')) | 	let result = JSON.parse(localStorage.getItem("reviews")); | ||||||
| 	if (result) { | 	if (result) { | ||||||
| 		return result; | 		return result; | ||||||
| 	} | 	} | ||||||
| @@ -15,5 +15,5 @@ export function getReviewsFromStorage() { | |||||||
|  * @param {Array<Object>} reviews An array of reviews |  * @param {Array<Object>} reviews An array of reviews | ||||||
|  */ |  */ | ||||||
| export function saveReviewsToStorage(reviews) { | export function saveReviewsToStorage(reviews) { | ||||||
| 	localStorage.setItem('reviews', JSON.stringify(reviews)); | 	localStorage.setItem("reviews", JSON.stringify(reviews)); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import {strict as assert} from "node:assert" | import {strict as assert} from "node:assert"; | ||||||
| import {describe, it, beforeEach} from "mocha"; | import {describe, it, beforeEach} from "mocha"; | ||||||
| import {saveReviewsToStorage, getReviewsFromStorage} from "./localStorage.js"; | import {saveReviewsToStorage, getReviewsFromStorage} from "./localStorage.js"; | ||||||
|  |  | ||||||
| @@ -40,7 +40,7 @@ describe("test app localStorage interaction", () => { | |||||||
| 					"rating": i, | 					"rating": i, | ||||||
| 					"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`] | 					"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`] | ||||||
| 				} | 				} | ||||||
| 			) | 			); | ||||||
| 			saveReviewsToStorage(reviews); | 			saveReviewsToStorage(reviews); | ||||||
| 			assert.deepEqual(getReviewsFromStorage(), reviews); | 			assert.deepEqual(getReviewsFromStorage(), reviews); | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -1,29 +1,29 @@ | |||||||
| // main.js | // main.js | ||||||
| import {getReviewsFromStorage, saveReviewsToStorage} from './localStorage.js'; | import {getReviewsFromStorage, saveReviewsToStorage} 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); | ||||||
|  |  | ||||||
| function init() { | function init() { | ||||||
|   // Get the reviews from localStorage | 	// Get the reviews from localStorage | ||||||
|   let reviews = getReviewsFromStorage(); | 	let reviews = getReviewsFromStorage(); | ||||||
|   // Add each reviews to the <main> element | 	// Add each reviews to the <main> element | ||||||
|   addReviewsToDocument(reviews); | 	addReviewsToDocument(reviews); | ||||||
|   // Add the event listeners to the form elements | 	// Add the event listeners to the form elements | ||||||
|   initFormHandler(); | 	initFormHandler(); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @param {Array<Object>} reviews An array of reviews |  * @param {Array<Object>} reviews An array of reviews | ||||||
|  */ |  */ | ||||||
| function addReviewsToDocument(reviews) { | function addReviewsToDocument(reviews) { | ||||||
|   let mainEl = document.querySelector('main'); | 	let mainEl = document.querySelector("main"); | ||||||
|   reviews.forEach(review => { | 	reviews.forEach(review => { | ||||||
|     let newReview = document.createElement('review-card'); | 		let newReview = document.createElement("review-card"); | ||||||
|     newReview.data = review; | 		newReview.data = review; | ||||||
|     //TODO: want to append it to whatever the box is in layout  | 		//TODO: want to append it to whatever the box is in layout  | ||||||
|     mainEl.append(newReview); | 		mainEl.append(newReview); | ||||||
|   }); | 	}); | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -33,86 +33,86 @@ function addReviewsToDocument(reviews) { | |||||||
|  */ |  */ | ||||||
| function initFormHandler() { | function initFormHandler() { | ||||||
|  |  | ||||||
|   //btn to create form (could be its own function?) | 	//btn to create form (could be its own function?) | ||||||
|   let createBtn = document.getElementById('create'); | 	let createBtn = document.getElementById("create"); | ||||||
|   createBtn.addEventListener('click', function(){ | 	createBtn.addEventListener("click", function(){ | ||||||
|     window.location.assign('./CreatePage.html'); | 		window.location.assign("./CreatePage.html"); | ||||||
|   }); | 	}); | ||||||
|  |  | ||||||
|   //accessing form components | 	//accessing form components | ||||||
|   let tagContainer = document.getElementById('tag-container-form'); | 	let tagContainer = document.getElementById("tag-container-form"); | ||||||
|   let form = document.querySelector('form'); | 	let form = document.querySelector("form"); | ||||||
|    |    | ||||||
|   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 and put in storage | ||||||
|     */ |     */ | ||||||
|     let formData = new FormData(form); | 		let formData = new FormData(form); | ||||||
|     let reviewObject = {}; | 		let reviewObject = {}; | ||||||
|     for (let [key, value] of formData) { | 		for (let [key, value] of formData) { | ||||||
|       console.log(`${key}`); | 			console.log(`${key}`); | ||||||
|       console.log(`${value}`); | 			console.log(`${value}`); | ||||||
|       if (`${key}` !== "tag-form") { | 			if (`${key}` !== "tag-form") { | ||||||
|         reviewObject[`${key}`] = `${value}`; | 				reviewObject[`${key}`] = `${value}`; | ||||||
|       } | 			} | ||||||
|     } | 		} | ||||||
|     reviewObject['tags'] = []; | 		reviewObject["tags"] = []; | ||||||
|  |  | ||||||
|     let tags = document.querySelectorAll('.tag'); | 		let tags = document.querySelectorAll(".tag"); | ||||||
|     for(let i = 0; i < tags.length; i ++) { | 		for(let i = 0; i < tags.length; i ++) { | ||||||
|       reviewObject['tags'].push(tags[i].innerHTML); | 			reviewObject["tags"].push(tags[i].innerHTML); | ||||||
|       tagContainer.removeChild(tags[i]); | 			tagContainer.removeChild(tags[i]); | ||||||
|     } | 		} | ||||||
|      |      | ||||||
|  |  | ||||||
|     let newReview = document.createElement('review-card'); | 		let newReview = document.createElement("review-card"); | ||||||
|     newReview.data = reviewObject; | 		newReview.data = reviewObject; | ||||||
|  |  | ||||||
|     //TODO: want to append it to whatever the box is in layout  | 		//TODO: want to append it to whatever the box is in layout  | ||||||
|     let mainEl = document.querySelector('main'); | 		let mainEl = document.querySelector("main"); | ||||||
|     mainEl.append(newReview); | 		mainEl.append(newReview); | ||||||
|  |  | ||||||
|     let storedReviews = getReviewsFromStorage(); | 		let storedReviews = getReviewsFromStorage(); | ||||||
|     storedReviews.push(reviewObject); | 		storedReviews.push(reviewObject); | ||||||
|     saveReviewsToStorage(storedReviews); | 		saveReviewsToStorage(storedReviews); | ||||||
|     document.getElementById("new-food-entry").reset(); | 		document.getElementById("new-food-entry").reset(); | ||||||
|   }); | 	}); | ||||||
|  |  | ||||||
|   // DEV-MODE: for testing purposes  | 	// DEV-MODE: for testing purposes  | ||||||
|   let clearBtn = document.querySelector('.danger'); | 	let clearBtn = document.querySelector(".danger"); | ||||||
|   clearBtn.addEventListener('click', function() { | 	clearBtn.addEventListener("click", function() { | ||||||
|     localStorage.clear(); | 		localStorage.clear(); | ||||||
|     let mainEl = document.querySelector('main'); | 		let mainEl = document.querySelector("main"); | ||||||
|     while (mainEl.firstChild) { | 		while (mainEl.firstChild) { | ||||||
|       mainEl.removeChild(mainEl.firstChild); | 			mainEl.removeChild(mainEl.firstChild); | ||||||
|     } | 		} | ||||||
|     let deleteTags = document.querySelectorAll('.tag'); | 		let deleteTags = document.querySelectorAll(".tag"); | ||||||
|     for(let i = 0; i < deleteTags.length; i ++) { | 		for(let i = 0; i < deleteTags.length; i ++) { | ||||||
|       tagContainer.removeChild(deleteTags[i]); | 			tagContainer.removeChild(deleteTags[i]); | ||||||
|     } | 		} | ||||||
|      |      | ||||||
|     //clears reviews AS WELL as resets form | 		//clears reviews AS WELL as resets form | ||||||
|     document.getElementById("new-food-entry").reset(); | 		document.getElementById("new-food-entry").reset(); | ||||||
|       |       | ||||||
|       |       | ||||||
|   }); | 	}); | ||||||
|  |  | ||||||
|   let tagAddBtn = document.getElementById('tagAdd'); | 	let tagAddBtn = document.getElementById("tagAdd"); | ||||||
|   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) { | ||||||
|       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"); | ||||||
|       tagLabel.addEventListener('click',()=> { | 			tagLabel.addEventListener("click",()=> { | ||||||
|         tagContainer.removeChild(tagLabel); | 				tagContainer.removeChild(tagLabel); | ||||||
|       }); | 			}); | ||||||
|        |        | ||||||
|       tagContainer.append(tagLabel); | 			tagContainer.append(tagLabel); | ||||||
|       tagField.value = ''; | 			tagField.value = ""; | ||||||
|  |  | ||||||
|     } | 		} | ||||||
|   }); | 	}); | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user