2022-11-12 22:16:03 +00:00
|
|
|
//reviewDetails.js
|
2022-12-03 02:02:52 +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
|
|
|
|
2022-11-30 00:13:58 +00:00
|
|
|
/**
|
|
|
|
* Populates the relevant data to the details from local storage review.
|
|
|
|
*/
|
2022-12-03 02:02:52 +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-27 23:48:00 +00:00
|
|
|
/**
|
|
|
|
* Populates the relevant data to the details from local storage review
|
|
|
|
*/
|
2022-12-03 02:02:52 +00:00
|
|
|
function setupInfo() {
|
2022-11-21 00:27:27 +00:00
|
|
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
|
|
|
let currReview = getReviewFromStorage(currID);
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
//meal image
|
2022-11-27 01:38:19 +00:00
|
|
|
let mealImg = document.getElementById("d-meal-img");
|
2022-12-03 02:02:52 +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-12-03 02:02:52 +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-12-03 02:02:52 +00:00
|
|
|
starsImg.setAttribute("src", "./assets/images/" + currReview["rating"] + "-star.svg");
|
|
|
|
starsImg.setAttribute("alt", currReview["rating"] + " stars");
|
|
|
|
|
2022-11-21 00:27:27 +00:00
|
|
|
//tags
|
|
|
|
let tagContainer = document.getElementById("d-tags");
|
2022-12-03 02:02:52 +00:00
|
|
|
if (currReview["tags"]) {
|
2022-11-21 00:27:27 +00:00
|
|
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
|
|
|
let newTag = document.createElement("label");
|
2022-12-03 02:02:52 +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-30 00:13:58 +00:00
|
|
|
/**
|
|
|
|
* Sets up delete button to delete reveiw from storage and switch to homepage.
|
|
|
|
*/
|
2022-12-03 02:02:52 +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-12-03 02:02:52 +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
|
|
|
}
|
|
|
|
|
2022-11-30 00:13:58 +00:00
|
|
|
/**
|
|
|
|
* Sets up update button to reveal form and update info in storage and the current page.
|
|
|
|
*/
|
2022-12-03 02:02:52 +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-12-03 02:02:52 +00:00
|
|
|
updateBtn.addEventListener("click", function () {
|
2022-11-15 00:53:07 +00:00
|
|
|
//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"];
|
|
|
|
|
2022-11-29 02:48:55 +00:00
|
|
|
//Set used to track tags and ensure no duplicates
|
|
|
|
let tagSet = new Set();
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
if (currReview["tags"]) {
|
2022-11-18 07:13:53 +00:00
|
|
|
while (tagContainer.firstChild) {
|
|
|
|
tagContainer.removeChild(tagContainer.firstChild);
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 04:51:45 +00:00
|
|
|
let tagSetVal;
|
2022-11-18 07:13:53 +00:00
|
|
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
2022-12-03 02:31:26 +00:00
|
|
|
tagSetVal = currReview["tags"][i].toLocaleLowerCase();
|
2022-11-29 22:16:17 +00:00
|
|
|
tagSet.add(tagSetVal);
|
2022-11-18 07:13:53 +00:00
|
|
|
let newTag = document.createElement("label");
|
2022-12-03 02:02:52 +00:00
|
|
|
newTag.setAttribute("class", "tag");
|
2022-11-18 07:13:53 +00:00
|
|
|
newTag.innerHTML = currReview["tags"][i];
|
2022-12-03 02:02:52 +00:00
|
|
|
newTag.addEventListener("click", () => {
|
2022-11-18 07:13:53 +00:00
|
|
|
tagContainer.removeChild(newTag);
|
2022-11-29 22:16:17 +00:00
|
|
|
tagSet.delete(tagSetVal);
|
2022-11-18 07:13:53 +00:00
|
|
|
});
|
|
|
|
tagContainer.append(newTag);
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// Declaring variable storing image data url
|
|
|
|
let imgDataURL = "";
|
|
|
|
|
|
|
|
// Accessing components related to taking photo
|
|
|
|
let videoMode = true;
|
|
|
|
let player = document.getElementById("player");
|
|
|
|
let canvas = document.getElementById("photoCanvas");
|
|
|
|
let photoButton = document.getElementById("photoButton");
|
2022-12-03 02:02:52 +00:00
|
|
|
let context = canvas.getContext("2d");
|
2022-11-30 23:59:43 +00:00
|
|
|
|
|
|
|
// Event listener for the photo taking/reset button
|
2022-12-03 02:02:52 +00:00
|
|
|
photoButton.addEventListener("click", () => {
|
2022-11-30 23:59:43 +00:00
|
|
|
// capturing the current video frame
|
|
|
|
if (videoMode) {
|
|
|
|
videoMode = false;
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// setting up the appropriate components for displaying the photo preview
|
|
|
|
photoButton.innerText = "Retake";
|
|
|
|
player.setAttribute("hidden", "");
|
|
|
|
canvas.removeAttribute("hidden", "");
|
|
|
|
|
|
|
|
// displaying the captured snapshot on a canvas and saving it as a data url
|
|
|
|
context.drawImage(player, 0, 0, canvas.width, canvas.height);
|
|
|
|
imgDataURL = canvas.toDataURL();
|
|
|
|
}
|
|
|
|
// returning to displaying the video stream
|
|
|
|
else {
|
|
|
|
videoMode = true;
|
|
|
|
|
|
|
|
// setting up the appropriate components for the video stream
|
|
|
|
photoButton.innerText = "Take Photo";
|
|
|
|
canvas.setAttribute("hidden", "");
|
|
|
|
player.removeAttribute("hidden", "");
|
|
|
|
}
|
|
|
|
});
|
2022-11-20 21:51:28 +00:00
|
|
|
|
|
|
|
/*
|
2022-12-03 02:02:52 +00:00
|
|
|
* change the input source of the image between local file and taking photo
|
|
|
|
* depending on user's selection
|
|
|
|
*/
|
2022-11-20 21:51:28 +00:00
|
|
|
let select = document.getElementById("select");
|
2022-11-30 23:59:43 +00:00
|
|
|
const input = document.getElementById("mealImg");
|
2022-12-03 02:02:52 +00:00
|
|
|
select.addEventListener("change", function () {
|
|
|
|
// Select a photo with HTML file selector
|
2022-11-20 21:51:28 +00:00
|
|
|
if (select.value == "file") {
|
2022-11-30 23:59:43 +00:00
|
|
|
// enabling file upload components and hiding photo taking components
|
|
|
|
input.removeAttribute("hidden", "");
|
|
|
|
player.setAttribute("hidden", "");
|
|
|
|
canvas.setAttribute("hidden", "");
|
|
|
|
photoButton.setAttribute("hidden", "");
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// stopping the video stream
|
|
|
|
player.srcObject.getVideoTracks()[0].stop();
|
2022-11-20 21:51:28 +00:00
|
|
|
}
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// Take a photo
|
2022-11-20 21:51:28 +00:00
|
|
|
else {
|
2022-11-30 23:59:43 +00:00
|
|
|
// enabling photo taking components and hiding file upload components
|
|
|
|
videoMode = true;
|
|
|
|
photoButton.innerText = "Take Photo";
|
|
|
|
input.setAttribute("hidden", "");
|
|
|
|
player.removeAttribute("hidden", "");
|
|
|
|
photoButton.removeAttribute("hidden", "");
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// getting video stream from user's camera then displaying it on a video element
|
2022-12-03 02:02:52 +00:00
|
|
|
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
|
2022-11-30 23:59:43 +00:00
|
|
|
player.srcObject = stream;
|
|
|
|
});
|
2022-11-20 21:51:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//addressing sourcing image from local file
|
2022-12-03 02:02:52 +00:00
|
|
|
document.getElementById("mealImg").addEventListener("change", function () {
|
2022-11-20 21:51:28 +00:00
|
|
|
const reader = new FileReader();
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-20 21:51:28 +00:00
|
|
|
//store image data URL after successful image load
|
2022-12-03 02:02:52 +00:00
|
|
|
reader.addEventListener(
|
|
|
|
"load",
|
|
|
|
() => {
|
|
|
|
imgDataURL = reader.result;
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2022-11-20 21:51:28 +00:00
|
|
|
//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-12-05 01:53:13 +00:00
|
|
|
//cancel button on update form hids form not making any changes
|
|
|
|
let cancelBtn = document.getElementById("cancel-btn");
|
|
|
|
cancelBtn.addEventListener("click", function(){
|
|
|
|
updateDiv.classList.add("hidden");
|
|
|
|
})
|
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
//Take formdata values as newData when submit
|
2022-12-03 02:02:52 +00:00
|
|
|
form.addEventListener("submit", function () {
|
2022-11-18 07:13:53 +00:00
|
|
|
/*
|
2022-12-03 02:02:52 +00:00
|
|
|
* User submits the form for their review.
|
|
|
|
* We create reviewCard data, replace in storage, and update tags
|
|
|
|
*/
|
2022-11-18 07:13:53 +00:00
|
|
|
let formData = new FormData(form);
|
|
|
|
let newData = {};
|
2022-11-27 23:48:00 +00:00
|
|
|
//iterate through formData and add to newData
|
2022-11-18 07:13:53 +00:00
|
|
|
for (let [key, value] of formData) {
|
|
|
|
if (`${key}` !== "tag-form") {
|
|
|
|
newData[`${key}`] = `${value}`;
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
2022-11-30 00:13:58 +00:00
|
|
|
// Account for the case where image is not updated
|
2022-11-30 23:59:43 +00:00
|
|
|
if (`${key}` === "mealImg" && imgDataURL === "") {
|
2022-11-20 22:27:24 +00:00
|
|
|
newData["mealImg"] = currReview["mealImg"];
|
2022-12-03 02:02:52 +00:00
|
|
|
} else if (`${key}` === "mealImg") {
|
2022-11-20 21:51:28 +00:00
|
|
|
newData["mealImg"] = imgDataURL;
|
|
|
|
}
|
2022-11-18 07:13:53 +00:00
|
|
|
}
|
|
|
|
newData["tags"] = [];
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-18 07:13:53 +00:00
|
|
|
let tags = document.querySelectorAll(".tag");
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < tags.length; i++) {
|
2022-11-18 07:13:53 +00:00
|
|
|
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-12-03 02:02:52 +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-18 07:13:53 +00:00
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-30 00:13:58 +00:00
|
|
|
// Adding tag to form functionality
|
2022-11-18 07:13:53 +00:00
|
|
|
let tagAddBtn = document.getElementById("tag-add-btn");
|
2022-12-03 02:02:52 +00:00
|
|
|
tagAddBtn.addEventListener("click", () => {
|
2022-11-18 07:13:53 +00:00
|
|
|
let tagField = document.getElementById("tag-form");
|
|
|
|
if (tagField.value.length > 0) {
|
2022-12-03 02:31:26 +00:00
|
|
|
let tagSetVal = tagField.value.toLocaleLowerCase();
|
2022-12-03 02:02:52 +00:00
|
|
|
if (!tagSet.has(tagSetVal)) {
|
2022-11-29 02:48:55 +00:00
|
|
|
let tagLabel = document.createElement("label");
|
|
|
|
tagLabel.innerHTML = tagField.value;
|
2022-12-03 02:02:52 +00:00
|
|
|
tagLabel.setAttribute("class", "tag");
|
2022-11-29 22:16:17 +00:00
|
|
|
tagSet.add(tagSetVal);
|
2022-12-03 02:02:52 +00:00
|
|
|
tagLabel.addEventListener("click", () => {
|
2022-11-29 02:48:55 +00:00
|
|
|
tagContainer.removeChild(tagLabel);
|
2022-11-29 22:16:17 +00:00
|
|
|
tagSet.delete(tagSetVal);
|
2022-11-29 02:48:55 +00:00
|
|
|
});
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-29 02:48:55 +00:00
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
} else {
|
|
|
|
window.alert("No duplicate tags allowed");
|
|
|
|
}
|
2022-11-18 07:13:53 +00:00
|
|
|
tagField.value = "";
|
|
|
|
}
|
|
|
|
});
|
2022-11-15 00:53:07 +00:00
|
|
|
});
|
2022-11-12 22:16:03 +00:00
|
|
|
}
|