2022-11-12 22:16:03 +00:00
|
|
|
//reviewDetails.js
|
2022-11-15 00:53:07 +00:00
|
|
|
import {getReviewsFromStorage, saveReviewsToStorage} from "./localStorage.js";
|
2022-11-12 22:16:03 +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-12 22:16:03 +00:00
|
|
|
|
|
|
|
function init(){
|
2022-11-15 00:53:07 +00:00
|
|
|
setupDelete();
|
|
|
|
setupUpdate();
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupDelete(){
|
2022-11-17 20:25:18 +00:00
|
|
|
let deleteBtn = document.getElementById("delete-btn");
|
2022-11-18 02:45:53 +00:00
|
|
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
|
|
|
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
|
2022-11-15 00:53:07 +00:00
|
|
|
deleteBtn.addEventListener("click", function(){
|
|
|
|
if(window.confirm("Are you sure you want to delete this entry?")){
|
2022-11-18 02:45:53 +00:00
|
|
|
for (let i in activeIDS) {
|
|
|
|
if (activeIDS[i] == currID) {
|
|
|
|
activeIDS.splice(i,1);
|
|
|
|
localStorage.setItem('activeIDS', JSON.stringify(activeIDS));
|
|
|
|
sessionStorage.removeItem('currID');
|
|
|
|
localStorage.removeItem(`review${currID}`);
|
|
|
|
window.location.assign("./index.html");
|
|
|
|
break;
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupUpdate(){
|
2022-11-17 20:25:18 +00:00
|
|
|
let updateBtn = document.getElementById("update-btn");
|
2022-11-18 02:45:53 +00:00
|
|
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
|
|
|
let currReview = JSON.parse(localStorage.getItem(`review${currID}`));
|
2022-11-15 00:53:07 +00:00
|
|
|
let form = document.getElementById("update-food-entry");
|
|
|
|
updateBtn.addEventListener("click", function(){
|
|
|
|
//update function
|
2022-11-18 02:45:53 +00:00
|
|
|
if(currReview){
|
2022-11-15 00:53:07 +00:00
|
|
|
form.style.display = "block";
|
|
|
|
let tagContainer = document.getElementById("tag-container-form");
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
//Set value of each input element to current's values
|
2022-11-18 02:45:53 +00:00
|
|
|
document.getElementById("mealImg").defaultValue = currReview["mealImg"];
|
|
|
|
document.getElementById("imgAlt").defaultValue = currReview["imgAlt"];
|
|
|
|
document.getElementById("mealName").defaultValue = currReview["mealName"];
|
|
|
|
document.getElementById("comments").textContent = currReview["comments"];
|
|
|
|
document.getElementById("s" + `${currReview["rating"]}`).checked = true;
|
|
|
|
document.getElementById("restaurant").defaultValue = currReview["restaurant"];
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
if(currReview["tags"]){
|
|
|
|
while (tagContainer.firstChild) {
|
|
|
|
tagContainer.removeChild(tagContainer.firstChild);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
2022-11-15 00:53:07 +00:00
|
|
|
let newTag = document.createElement("label");
|
|
|
|
newTag.setAttribute("class","tag");
|
2022-11-18 02:45:53 +00:00
|
|
|
newTag.innerHTML = currReview["tags"][i];
|
2022-11-15 00:53:07 +00:00
|
|
|
newTag.addEventListener("click",()=> {
|
|
|
|
tagContainer.removeChild(newTag);
|
|
|
|
});
|
|
|
|
tagContainer.append(newTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Take formdata values as newData when submit
|
|
|
|
form.addEventListener("submit", function(){
|
|
|
|
/*
|
2022-11-12 22:16:03 +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 newData = {};
|
|
|
|
for (let [key, value] of formData) {
|
|
|
|
console.log(`${key}`);
|
|
|
|
console.log(`${value}`);
|
|
|
|
if (`${key}` !== "tag-form") {
|
|
|
|
newData[`${key}`] = `${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newData["tags"] = [];
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let tags = document.querySelectorAll(".tag");
|
|
|
|
for(let i = 0; i < tags.length; i ++) {
|
|
|
|
newData["tags"].push(tags[i].innerHTML);
|
|
|
|
tagContainer.removeChild(tags[i]);
|
|
|
|
}
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
newData["reviewID"] = currID;
|
|
|
|
|
|
|
|
|
|
|
|
localStorage.setItem("review"+currID, JSON.stringify(newData));
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
form.style.display = "none";
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-17 20:25:18 +00:00
|
|
|
let tagAddBtn = document.getElementById("tag-add-btn");
|
2022-11-15 00:53:07 +00:00
|
|
|
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-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
tagField.value = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|