2022-11-12 22:16:03 +00:00
|
|
|
//reviewDetails.js
|
2022-11-18 07:13:53 +00:00
|
|
|
import {deleteReviewFromStorage, getReviewFromStorage, updateReviewToStorage} 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-21 00:27:27 +00:00
|
|
|
setupInfo();
|
2022-11-15 00:53:07 +00:00
|
|
|
setupDelete();
|
|
|
|
setupUpdate();
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
function setupInfo(){
|
|
|
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
|
|
|
let currReview = getReviewFromStorage(currID);
|
|
|
|
|
|
|
|
//meal image
|
2022-11-27 01:38:19 +00:00
|
|
|
let mealImg = document.getElementById("d-meal-img");
|
2022-11-21 20:29:06 +00:00
|
|
|
mealImg.setAttribute("src",currReview["mealImg"]);
|
|
|
|
mealImg.addEventListener("error", function(e) {
|
2022-11-24 01:20:55 +00:00
|
|
|
mealImg.setAttribute("src", "./assets/images/default_plate.png");
|
2022-11-21 20:29:06 +00:00
|
|
|
e.onerror = null;
|
|
|
|
});
|
2022-11-21 00:27:27 +00:00
|
|
|
|
|
|
|
//meal name
|
2022-11-27 01:38:19 +00:00
|
|
|
let mealLabel = document.getElementById("d-meal-name");
|
2022-11-21 00:27:27 +00:00
|
|
|
mealLabel.innerHTML = currReview["mealName"];
|
|
|
|
|
|
|
|
//restaurant name
|
2022-11-21 06:22:31 +00:00
|
|
|
let restaurantLabel = document.getElementById("d-restaurant");
|
2022-11-21 00:27:27 +00:00
|
|
|
restaurantLabel.innerHTML = currReview["restaurant"];
|
2022-11-21 06:22:31 +00:00
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
//comments
|
|
|
|
let comments = document.getElementById("d-comments");
|
|
|
|
comments.innerText = currReview["comments"];
|
2022-11-21 06:22:31 +00:00
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
//rating
|
|
|
|
let starsImg = document.getElementById("d-rating");
|
2022-11-22 00:12:17 +00:00
|
|
|
starsImg.setAttribute("src", "./assets/images/"+currReview["rating"]+"-star.svg");
|
2022-11-21 00:27:27 +00:00
|
|
|
starsImg.setAttribute("alt", currReview["rating"] +" stars");
|
2022-11-21 06:22:31 +00:00
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
//tags
|
|
|
|
let tagContainer = document.getElementById("d-tags");
|
|
|
|
if(currReview["tags"]){
|
|
|
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
|
|
|
let newTag = document.createElement("label");
|
2022-11-21 23:27:45 +00:00
|
|
|
newTag.setAttribute("class","d-tag");
|
2022-11-21 00:27:27 +00:00
|
|
|
newTag.innerHTML = currReview["tags"][i];
|
|
|
|
tagContainer.append(newTag);
|
|
|
|
}
|
2022-11-21 06:22:31 +00:00
|
|
|
}
|
2022-11-21 00:27:27 +00:00
|
|
|
}
|
|
|
|
|
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"));
|
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 07:13:53 +00:00
|
|
|
deleteReviewFromStorage(currID);
|
2022-11-18 07:56:07 +00:00
|
|
|
sessionStorage.removeItem("currID");
|
2022-11-18 07:13:53 +00:00
|
|
|
window.location.assign("./index.html");
|
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"));
|
2022-11-18 07:13:53 +00:00
|
|
|
let currReview = getReviewFromStorage(currID);
|
2022-11-21 23:27:45 +00:00
|
|
|
let form = document.getElementById("new-food-entry");
|
2022-11-21 21:52:44 +00:00
|
|
|
let updateDiv = document.getElementById("update-form");
|
2022-11-15 00:53:07 +00:00
|
|
|
updateBtn.addEventListener("click", function(){
|
|
|
|
//update function
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-21 21:52:44 +00:00
|
|
|
updateDiv.classList.remove("hidden");
|
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
let tagContainer = document.getElementById("tag-container-form");
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
//Set value of each input element to current's values
|
|
|
|
document.getElementById("mealImg").defaultValue = currReview["mealImg"];
|
|
|
|
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"];
|
|
|
|
|
|
|
|
if(currReview["tags"]){
|
|
|
|
while (tagContainer.firstChild) {
|
|
|
|
tagContainer.removeChild(tagContainer.firstChild);
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
2022-11-18 07:13:53 +00:00
|
|
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
|
|
|
let newTag = document.createElement("label");
|
|
|
|
newTag.setAttribute("class","tag");
|
|
|
|
newTag.innerHTML = currReview["tags"][i];
|
|
|
|
newTag.addEventListener("click",()=> {
|
|
|
|
tagContainer.removeChild(newTag);
|
|
|
|
});
|
|
|
|
tagContainer.append(newTag);
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 21:51:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* change the input source of the image between local file and URL
|
|
|
|
* depending on user's selection
|
|
|
|
*/
|
|
|
|
let select = document.getElementById("select");
|
|
|
|
select.addEventListener("change", function() {
|
2022-11-20 23:23:22 +00:00
|
|
|
const input = document.getElementById("source");
|
2022-11-20 21:51:28 +00:00
|
|
|
|
|
|
|
if (select.value == "file") {
|
2022-11-20 23:23:22 +00:00
|
|
|
input.innerHTML = `
|
|
|
|
Source:
|
|
|
|
<input type="file" accept="image/*" id="mealImg" name="mealImg">
|
|
|
|
`;
|
2022-11-20 21:51:28 +00:00
|
|
|
}
|
|
|
|
//TODO: change to photo taking for sprint 3
|
|
|
|
else {
|
2022-11-20 23:23:22 +00:00
|
|
|
input.innerHTML = `
|
|
|
|
Source:
|
|
|
|
<input type="text" id="mealImg" name="mealImg">
|
|
|
|
`;
|
2022-11-20 21:51:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//addressing sourcing image from local file
|
|
|
|
let imgDataURL = "";
|
|
|
|
document.getElementById("mealImg").addEventListener("change", function() {
|
2022-11-20 22:27:24 +00:00
|
|
|
console.log("reading used");
|
2022-11-20 21:51:28 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
//store image data URL after successful image load
|
|
|
|
reader.addEventListener("load", ()=>{
|
|
|
|
imgDataURL = reader.result;
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
//convert image file into data URL for local storage
|
|
|
|
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
|
2022-11-20 23:23:22 +00:00
|
|
|
});
|
2022-11-20 21:51:28 +00:00
|
|
|
|
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
//Take formdata values as newData when submit
|
|
|
|
form.addEventListener("submit", function(){
|
|
|
|
/*
|
|
|
|
* User submits the form for their review.
|
|
|
|
* We create reviewCard and put in storage
|
|
|
|
*/
|
|
|
|
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}`;
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
2022-11-20 22:27:24 +00:00
|
|
|
//Account for the case where image is not updated
|
|
|
|
if (`${key}` === "mealImg" && document.getElementById("mealImg").value === "") {
|
|
|
|
newData["mealImg"] = currReview["mealImg"];
|
|
|
|
}
|
|
|
|
else if (`${key}` === "mealImg" && select.value == "file") {
|
2022-11-20 21:51:28 +00:00
|
|
|
newData["mealImg"] = imgDataURL;
|
|
|
|
}
|
2022-11-18 07:13:53 +00:00
|
|
|
}
|
|
|
|
newData["tags"] = [];
|
|
|
|
|
|
|
|
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-22 01:03:10 +00:00
|
|
|
newData["reviewID"] = currID;
|
2022-11-18 02:45:53 +00:00
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
updateReviewToStorage(currID, newData);
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-21 23:27:45 +00:00
|
|
|
updateDiv.classList.add("hidden");
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
let tagAddBtn = document.getElementById("tag-add-btn");
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
tagField.value = "";
|
|
|
|
}
|
|
|
|
});
|
2022-11-15 00:53:07 +00:00
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|