2022-11-18 07:13:53 +00:00
|
|
|
import { newReviewToStorage } from "./localStorage.js";
|
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
|
|
|
function init() {
|
2022-11-18 07:56:07 +00:00
|
|
|
initFormHandler();
|
2022-11-18 02:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initFormHandler() {
|
|
|
|
|
|
|
|
//accessing form components
|
|
|
|
let tagContainer = document.getElementById("tag-container-form");
|
|
|
|
let form = document.querySelector("form");
|
2022-11-19 20:10:55 +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-19 20:10:55 +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-19 20:10:55 +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-19 20:10:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//addressing sourcing image from local file
|
|
|
|
let imgDataURL = "";
|
|
|
|
document.getElementById("mealImg").addEventListener("change", function() {
|
|
|
|
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-19 20:10:55 +00:00
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
form.addEventListener("submit", function(e){
|
|
|
|
/*
|
|
|
|
* User submits the form for their review.
|
|
|
|
* We create reviewCard and put in storage
|
|
|
|
*/
|
|
|
|
e.preventDefault();
|
|
|
|
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}`;
|
|
|
|
}
|
2022-11-19 20:10:55 +00:00
|
|
|
if (`${key}` === "mealImg" && select.value == "file") {
|
|
|
|
reviewObject["mealImg"] = imgDataURL;
|
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
}
|
2022-11-20 22:36:03 +00:00
|
|
|
if(reviewObject["rating"] != null){
|
|
|
|
reviewObject["tags"] = [];
|
2022-11-18 02:45:53 +00:00
|
|
|
|
2022-11-20 22:36:03 +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-18 02:45:53 +00:00
|
|
|
|
2022-11-20 22:36:03 +00:00
|
|
|
let nextReviewId = newReviewToStorage(reviewObject);
|
|
|
|
sessionStorage.setItem("currID", JSON.stringify(nextReviewId));
|
2022-11-18 02:45:53 +00:00
|
|
|
|
2022-11-20 22:36:03 +00:00
|
|
|
window.location.assign("./ReviewDetails.html");
|
|
|
|
} else{
|
2022-11-20 22:47:26 +00:00
|
|
|
window.alert("NO! FILL IN STARS");
|
2022-11-20 22:36:03 +00:00
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-11-19 19:42:30 +00:00
|
|
|
let tagAddBtn = document.getElementById("tag-add-btn");
|
2022-11-29 02:48:55 +00:00
|
|
|
//Set used to track tags and ensure no duplicates
|
|
|
|
let tagSet = new Set();
|
2022-11-18 02:45:53 +00:00
|
|
|
tagAddBtn.addEventListener("click", ()=> {
|
|
|
|
let tagField = document.getElementById("tag-form");
|
|
|
|
if (tagField.value.length > 0) {
|
2022-11-29 22:16:17 +00:00
|
|
|
let tagSetVal = tagField.value.toLowerCase();
|
|
|
|
if (!tagSet.has(tagSetVal)){
|
2022-11-29 02:48:55 +00:00
|
|
|
let tagLabel = document.createElement("label");
|
|
|
|
tagLabel.innerHTML = tagField.value;
|
|
|
|
tagLabel.setAttribute("class","tag");
|
|
|
|
tagSet.add(tagField.value.toLowerCase());
|
|
|
|
tagLabel.addEventListener("click",()=> {
|
|
|
|
tagContainer.removeChild(tagLabel);
|
|
|
|
tagSet.delete(tagField.value.toLowerCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
} else {
|
|
|
|
window.alert("No duplicate tags allowed");
|
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
tagField.value = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|