cse110-fa22-group29/source/assets/scripts/CreatePage.js

132 lines
3.5 KiB
JavaScript
Raw Normal View History

import { newReviewToStorage } from "./localStorage.js";
window.addEventListener("DOMContentLoaded", init);
2022-11-29 23:39:43 +00:00
/**
* Delegates the functionality for creating review cards.
*/
function init() {
2022-11-18 07:56:07 +00:00
initFormHandler();
}
2022-11-29 23:39:43 +00:00
/**
* Creates a form and associates a new ID with the new review card.
*/
function initFormHandler() {
2022-11-29 23:39:43 +00:00
// Accesses form components
let tagContainer = document.getElementById("tag-container-form");
let form = document.querySelector("form");
2022-11-29 23:39:43 +00:00
// Event listener for reading form data
let select = document.getElementById("select");
select.addEventListener("change", function() {
const input = document.getElementById("source");
2022-11-29 23:39:43 +00:00
// Select a photo with HTML file selector
if (select.value == "file") {
input.innerHTML = `
Source:
<input type="file" accept="image/*" id="mealImg" name="mealImg">
`;
}
2022-11-29 23:39:43 +00:00
// Upload text URL input
else {
input.innerHTML = `
Source:
<input type="text" id="mealImg" name="mealImg">
`;
}
});
2022-11-29 23:39:43 +00:00
// Addresses sourcing image from local file
let imgDataURL = "";
document.getElementById("mealImg").addEventListener("change", function() {
const reader = new FileReader();
2022-11-29 23:39:43 +00:00
// Store image data URL after successful image load
reader.addEventListener("load", ()=>{
imgDataURL = reader.result;
}, false);
2022-11-29 23:39:43 +00:00
// Convert image file into data URL for local storage
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
});
form.addEventListener("submit", function(e){
2022-11-29 23:39:43 +00:00
// Create reviewObject and put in storage
e.preventDefault();
let formData = new FormData(form);
let reviewObject = {};
2022-11-29 23:39:43 +00:00
// Adds data to the reviewObject from form data
for (let [key, value] of formData) {
console.log(`${key}`);
console.log(`${value}`);
if (`${key}` !== "tag-form") {
reviewObject[`${key}`] = `${value}`;
}
if (`${key}` === "mealImg" && select.value == "file") {
reviewObject["mealImg"] = imgDataURL;
}
}
2022-11-29 23:39:43 +00:00
// Makes sure that ratings is filled
2022-11-20 22:36:03 +00:00
if(reviewObject["rating"] != null){
2022-11-29 23:39:43 +00:00
//Adds rags separately as an array
2022-11-20 22:36:03 +00:00
reviewObject["tags"] = [];
2022-11-29 23:39:43 +00:00
// Grabs tags
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-29 23:39:43 +00:00
// Assigns the new review with a new ID
2022-11-20 22:36:03 +00:00
let nextReviewId = newReviewToStorage(reviewObject);
sessionStorage.setItem("currID", JSON.stringify(nextReviewId));
2022-11-29 23:39:43 +00:00
// Redirects to a page that shows the newly created review
2022-11-20 22:36:03 +00:00
window.location.assign("./ReviewDetails.html");
2022-11-29 23:39:43 +00:00
}
// Does not let user proceed if rating is not complete
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-29 23:39:43 +00:00
// Event listener for tag functionality
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();
tagAddBtn.addEventListener("click", ()=> {
let tagField = document.getElementById("tag-form");
2022-11-29 23:39:43 +00:00
// If there is a tag, it'll display the tag
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");
}
tagField.value = "";
}
});
}