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
|
|
|
// get next id
|
2022-11-18 02:45:53 +00:00
|
|
|
|
2022-11-18 07:56:07 +00:00
|
|
|
// creates the key
|
|
|
|
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");
|
|
|
|
|
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reviewObject["tags"] = [];
|
|
|
|
|
|
|
|
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 07:13:53 +00:00
|
|
|
let nextReviewId = newReviewToStorage(reviewObject);
|
2022-11-18 02:45:53 +00:00
|
|
|
sessionStorage.setItem("currID", JSON.stringify(nextReviewId));
|
|
|
|
|
2022-11-18 07:56:07 +00:00
|
|
|
window.location.assign("./ReviewDetails.html");
|
2022-11-18 02:45:53 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
let tagAddBtn = document.getElementById("tagAdd");
|
|
|
|
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 = "";
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|