let newIngredient = { display: function(){ const selector = document.getElementById("unitSelector"); document.getElementById("newIngName").placeholder = "NAME"; document.getElementById("newIngCategory").placeholder = "CATEGORY"; document.getElementById("newIngQuantity").placeholder = "QUANTITY"; document.getElementById("bottleSizeLabel").style.display = "none"; selector.value = "g"; selector.onchange = ()=>{this.unitChange()}; document.getElementById("submitNewIng").onclick = ()=>{this.submit()}; document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")}); }, unitChange: function(){ const select = document.getElementById("unitSelector"); const bottleLabel = document.getElementById("bottleSizeLabel"); if(select.value === "bottle"){ bottleLabel.style.display = "block"; }else{ bottleLabel.style.display = "none"; } }, submit: function(){ let unitSelector = document.getElementById("unitSelector"); let options = document.querySelectorAll("#unitSelector option"); const quantityValue = parseFloat(document.getElementById("newIngQuantity").value); let unit = unitSelector.value; let newIngredient = { ingredient: { name: document.getElementById("newIngName").value, category: document.getElementById("newIngCategory").value, unitType: options[unitSelector.selectedIndex].getAttribute("type") }, quantity: quantityValue, defaultUnit: unit } //Change the ingredient if it is a special unit type (ie "bottle") if(unit === "bottle"){ newIngredient.ingredient.unitType = document.getElementById("bottleUnits").value; newIngredient.ingredient.unitSize = parseFloat(document.getElementById("bottleSize").value); } let loader = document.getElementById("loaderContainer"); loader.style.display = "flex"; fetch("/ingredients/create", { method: "POST", headers: { "Content-Type": "application/json;charset=utf-8" }, body: JSON.stringify(newIngredient) }) .then((response) => response.json()) .then((response)=>{ if(typeof(response) === "string"){ controller.createBanner(response, "error"); }else{ merchant.addIngredients([response]); state.updateIngredients(); controller.openStrand("ingredients"); controller.createBanner("INGREDIENT CREATED", "success"); } }) .catch((err)=>{ controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error"); }) .finally(()=>{ loader.style.display = "none"; }); }, submitSpreadsheet: function(){ event.preventDefault(); controller.closeModal(); const file = document.getElementById("spreadsheetInput").files[0]; let data = new FormData(); data.append("ingredients", file); let loader = document.getElementById("loaderContainer"); loader.style.display = "flex"; fetch("/ingredients/create/spreadsheet", { method: "post", body: data }) .then(response => response.json()) .then((response)=>{ if(typeof(response) === "string"){ controller.createBanner(response, "error"); }else{ merchant.addIngredients(response); state.updateIngredients(); controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success"); controller.openStrand("ingredients"); } }) .catch((err)=>{ controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error"); }) .finally(()=>{ loader.style.display = "none"; }); } } module.exports = newIngredient;