2022-11-08 03:18:05 +00:00
|
|
|
// main.js
|
2022-11-15 00:53:07 +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
|
2022-11-15 00:53:07 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
2022-11-08 03:18:05 +00:00
|
|
|
|
|
|
|
function init() {
|
2022-11-15 00:53:07 +00:00
|
|
|
// Get the reviews from localStorage
|
|
|
|
let reviews = getReviewsFromStorage();
|
|
|
|
// Add each reviews to the <main> element
|
|
|
|
addReviewsToDocument(reviews);
|
|
|
|
// Add the event listeners to the form elements
|
|
|
|
initFormHandler();
|
2022-11-08 03:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-15 00:53:07 +00:00
|
|
|
let mainEl = document.querySelector("main");
|
|
|
|
reviews.forEach(review => {
|
|
|
|
let newReview = document.createElement("review-card");
|
|
|
|
newReview.data = review;
|
|
|
|
//TODO: want to append it to whatever the box is in layout
|
|
|
|
mainEl.append(newReview);
|
|
|
|
});
|
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-12 22:16:03 +00:00
|
|
|
|
2022-11-15 02:07:33 +00:00
|
|
|
/*
|
2022-11-15 00:53:07 +00:00
|
|
|
//btn to create form (could be its own function?)
|
|
|
|
let createBtn = document.getElementById("create");
|
|
|
|
createBtn.addEventListener("click", function(){
|
|
|
|
window.location.assign("./CreatePage.html");
|
2022-11-15 02:11:57 +00:00
|
|
|
});*/
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
//accessing form components
|
|
|
|
let tagContainer = document.getElementById("tag-container-form");
|
2022-11-15 02:11:57 +00:00
|
|
|
let form = document.querySelector("form");
|
2022-11-10 21:22:32 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
form.addEventListener("submit", function(){
|
2022-11-15 02:11:57 +00:00
|
|
|
/*
|
2022-11-10 21:22:32 +00:00
|
|
|
* User submits the form for their review.
|
|
|
|
* We create reviewCard and put in storage
|
|
|
|
*/
|
2022-11-15 00:53:07 +00:00
|
|
|
let formData = new FormData(form);
|
|
|
|
let reviewObject = {};
|
|
|
|
for (let [key, value] of formData) {
|
|
|
|
console.log(`${key}`);
|
|
|
|
console.log(`${value}`);
|
|
|
|
if (`${key}` !== "tag-form") {
|
|
|
|
reviewObject[`${key}`] = `${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reviewObject["tags"] = [];
|
2022-11-10 21:22:32 +00:00
|
|
|
|
2022-11-15 00:53:07 +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-15 00:53:07 +00:00
|
|
|
let newReview = document.createElement("review-card");
|
|
|
|
newReview.data = reviewObject;
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-15 00:53:07 +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-15 00:53:07 +00:00
|
|
|
let storedReviews = getReviewsFromStorage();
|
|
|
|
storedReviews.push(reviewObject);
|
|
|
|
saveReviewsToStorage(storedReviews);
|
|
|
|
document.getElementById("new-food-entry").reset();
|
|
|
|
});
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
// DEV-MODE: for testing purposes
|
|
|
|
let clearBtn = document.querySelector(".danger");
|
|
|
|
clearBtn.addEventListener("click", function() {
|
|
|
|
localStorage.clear();
|
|
|
|
let mainEl = document.querySelector("main");
|
|
|
|
while (mainEl.firstChild) {
|
|
|
|
mainEl.removeChild(mainEl.firstChild);
|
|
|
|
}
|
|
|
|
let deleteTags = document.querySelectorAll(".tag");
|
|
|
|
for(let i = 0; i < deleteTags.length; i ++) {
|
|
|
|
tagContainer.removeChild(deleteTags[i]);
|
|
|
|
}
|
2022-11-09 05:51:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
//clears reviews AS WELL as resets form
|
|
|
|
document.getElementById("new-food-entry").reset();
|
2022-11-09 05:51:03 +00:00
|
|
|
|
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
});
|
2022-11-08 03:18:05 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let tagAddBtn = document.getElementById("tagAdd");
|
|
|
|
tagAddBtn.addEventListener("click", ()=> {
|
|
|
|
let tagField = document.getElementById("tag-form");
|
|
|
|
if (tagField.value.length > 0) {
|
|
|
|
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-15 00:53:07 +00:00
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
tagField.value = "";
|
2022-11-09 05:51:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
|
|
|
});
|
2022-11-09 05:51:03 +00:00
|
|
|
|
2022-11-11 20:07:58 +00:00
|
|
|
}
|