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

112 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-11-08 03:18:05 +00:00
// main.js
2022-11-10 21:22:32 +00:00
import {getReviewsFromStorage, saveReviewsToStorage} from './localStorage.js';
2022-11-08 03:18:05 +00:00
// Run the init() function when the page has loaded
window.addEventListener('DOMContentLoaded', init);
function init() {
2022-11-08 23:57:02 +00:00
// Get the reviews from localStorage
let reviews = getReviewsFromStorage();
2022-11-08 03:18:05 +00:00
// Add each recipe to the <main> element
2022-11-08 23:57:02 +00:00
addReviewsToDocument(reviews);
2022-11-08 03:18:05 +00:00
// Add the event listeners to the form elements
initFormHandler();
}
/**
2022-11-08 23:57:02 +00:00
* @param {Array<Object>} reviews An array of reviews
2022-11-08 03:18:05 +00:00
*/
2022-11-08 23:57:02 +00:00
function addReviewsToDocument(reviews) {
2022-11-08 03:18:05 +00:00
let mainEl = document.querySelector('main')
2022-11-08 23:57:02 +00:00
reviews.forEach(review=> {
let newReview = document.createElement('review-card')
newReview.data = review
mainEl.append(newReview);
2022-11-08 03:18:05 +00:00
})
}
/**
* Adds the necesarry event handlers to <form> and the clear storage
* <button>.
*/
function initFormHandler() {
2022-11-10 21:22:32 +00:00
//accessing form components
let tagContainer = document.getElementById('tag-container-form');
2022-11-08 03:18:05 +00:00
let theForm = document.querySelector('form')
let submitButt = document.querySelector('button[type="submit"]')
2022-11-10 21:22:32 +00:00
2022-11-08 03:18:05 +00:00
submitButt.addEventListener('click', function(){
2022-11-10 21:22:32 +00:00
/*
* User submits the form for their review.
* We create reviewCard and put in storage
*/
2022-11-08 03:18:05 +00:00
let formData = new FormData(theForm);
let reviewObject = {}
2022-11-08 03:18:05 +00:00
for (let [key, value] of formData) {
console.log(`${key}`)
console.log(`${value}`)
reviewObject[`${key}`] = `${value}`
2022-11-08 03:18:05 +00:00
}
2022-11-10 21:22:32 +00:00
reviewObject['tags'] = []
let deleteTags = document.querySelectorAll('.tag')
for(let i = 0; i < deleteTags.length; i ++) {
reviewObject['tags'].push(deleteTags[i].innerHTML);
tagContainer.removeChild(deleteTags[i]);
}
2022-11-08 03:18:05 +00:00
let newReview = document.createElement('review-card')
newReview.data = reviewObject
2022-11-08 03:18:05 +00:00
let mainEl = document.querySelector('main')
mainEl.append(newReview)
2022-11-08 03:18:05 +00:00
let aList = getReviewsFromStorage()
aList.push(reviewObject)
saveReviewsToStorage(aList)
document.getElementById("new-food-entry").reset();
2022-11-08 03:18:05 +00:00
});
let clearButt = document.querySelector('.danger')
clearButt.addEventListener('click', function() {
localStorage.clear();
let mainEl = document.querySelector('main')
while (mainEl.firstChild) {
2022-11-08 03:18:05 +00:00
mainEl.removeChild(mainEl.firstChild);
}
let deleteTags = document.querySelectorAll('.tag')
for(let i = 0; i < deleteTags.length; i ++) {
tagContainer.removeChild(deleteTags[i]);
}
//clears reviews AS WELL as resets form
document.getElementById("new-food-entry").reset();
2022-11-08 03:18:05 +00:00
});
let tagAddButton = document.getElementById('tagAdd');
tagAddButton.addEventListener('click', ()=> {
2022-11-10 21:22:32 +00:00
let tagField = document.getElementById('tag-form');
if (tagField.value.length > 0) {
let p = document.createElement('p');
p.innerHTML = tagField.value;
p.setAttribute('class','tag')
tagContainer.append(p);
tagField.value = '';
//adding deletion feature to each individual tag
let deleteTags = document.querySelectorAll('.tag')
for(let i = 0; i < deleteTags.length; i ++) {
deleteTags[i].addEventListener('click',()=> {
tagContainer.removeChild(deleteTags[i]);
})
}
}
});
}