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-11 08:03:56 +00:00
|
|
|
// Add each reviews 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-11 08:03:56 +00:00
|
|
|
let mainEl = document.querySelector('main');
|
2022-11-08 23:57:02 +00:00
|
|
|
reviews.forEach(review=> {
|
2022-11-11 08:03:56 +00:00
|
|
|
let newReview = document.createElement('review-card');
|
|
|
|
newReview.data = review;
|
|
|
|
newReview.addEventListener('click', function(){
|
|
|
|
//TODO: create a separate file for specs - frontend
|
|
|
|
var newWin = window.open('about:blank','_self');
|
|
|
|
});
|
|
|
|
//TODO: want to append it to whatever the box is in layout
|
2022-11-08 23:57:02 +00:00
|
|
|
mainEl.append(newReview);
|
2022-11-11 08:03:56 +00:00
|
|
|
});
|
2022-11-08 03:18:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-11 08:03:56 +00:00
|
|
|
* Adds the necessary event handlers to <form> and the clear storage
|
2022-11-08 03:18:05 +00:00
|
|
|
* <button>.
|
|
|
|
*/
|
|
|
|
function initFormHandler() {
|
2022-11-10 21:22:32 +00:00
|
|
|
//accessing form components
|
|
|
|
let tagContainer = document.getElementById('tag-container-form');
|
2022-11-11 08:03:56 +00:00
|
|
|
let form = document.querySelector('form');
|
2022-11-10 21:22:32 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
form.addEventListener('submit', 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-11 08:03:56 +00:00
|
|
|
let formData = new FormData(form);
|
|
|
|
let reviewObject = {};
|
2022-11-08 03:18:05 +00:00
|
|
|
for (let [key, value] of formData) {
|
2022-11-11 08:03:56 +00:00
|
|
|
console.log(`${key}`);
|
|
|
|
console.log(`${value}`);
|
|
|
|
if (`${key}` !== "tag-form") {
|
|
|
|
reviewObject[`${key}`] = `${value}`;
|
|
|
|
}
|
2022-11-08 03:18:05 +00:00
|
|
|
}
|
2022-11-11 08:03:56 +00:00
|
|
|
reviewObject['tags'] = [];
|
2022-11-10 21:22:32 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
let tags = document.querySelectorAll('.tag');
|
|
|
|
for(let i = 0; i < tags.length; i ++) {
|
|
|
|
reviewObject['tags'].push(tags[i].innerHTML);
|
|
|
|
tagContainer.removeChild(tags[i]);
|
2022-11-10 21:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
let newReview = document.createElement('review-card');
|
|
|
|
newReview.data = reviewObject;
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
//TODO: want to append it to whatever the box is in layout
|
|
|
|
let mainEl = document.querySelector('main');
|
|
|
|
mainEl.append(newReview);
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
let storedReviews = getReviewsFromStorage();
|
|
|
|
storedReviews.push(reviewObject);
|
|
|
|
saveReviewsToStorage(storedReviews);
|
2022-11-09 05:51:03 +00:00
|
|
|
document.getElementById("new-food-entry").reset();
|
2022-11-08 03:18:05 +00:00
|
|
|
});
|
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
// DEV-MODE: for testing purposes
|
|
|
|
let clearBtn = document.querySelector('.danger');
|
|
|
|
clearBtn.addEventListener('click', function() {
|
2022-11-08 03:18:05 +00:00
|
|
|
localStorage.clear();
|
2022-11-11 08:03:56 +00:00
|
|
|
let mainEl = document.querySelector('main');
|
2022-11-09 05:51:03 +00:00
|
|
|
while (mainEl.firstChild) {
|
2022-11-08 03:18:05 +00:00
|
|
|
mainEl.removeChild(mainEl.firstChild);
|
2022-11-09 05:51:03 +00:00
|
|
|
}
|
2022-11-11 08:03:56 +00:00
|
|
|
let deleteTags = document.querySelectorAll('.tag');
|
2022-11-09 05:51:03 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
let tagAddBtn = document.getElementById('tagAdd');
|
|
|
|
tagAddBtn.addEventListener('click', ()=> {
|
2022-11-10 21:22:32 +00:00
|
|
|
let tagField = document.getElementById('tag-form');
|
2022-11-09 05:51:03 +00:00
|
|
|
if (tagField.value.length > 0) {
|
2022-11-11 08:03:56 +00:00
|
|
|
let tagLabel = document.createElement('label');
|
|
|
|
tagLabel.innerHTML = tagField.value;
|
|
|
|
tagLabel.setAttribute('class','tag');
|
|
|
|
tagLabel.addEventListener('click',()=> {
|
|
|
|
tagContainer.removeChild(tagLabel);
|
|
|
|
});
|
2022-11-09 05:51:03 +00:00
|
|
|
|
2022-11-11 08:03:56 +00:00
|
|
|
tagContainer.append(tagLabel);
|
2022-11-09 05:51:03 +00:00
|
|
|
tagField.value = '';
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|