module.exports = { isPopulated: false, recipeDivList: [], display: function(Recipe){ if(!this.isPopulated){ this.populateRecipes(); if(merchant.pos !== "none"){ document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)}; } document.getElementById("recipeSearch").oninput = ()=>{this.search()}; document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()}; this.isPopulated = true; } }, populateRecipes: function(){ let recipeList = document.getElementById("recipeList"); let template = document.getElementById("recipe").content.children[0]; this.recipeDivList = []; while(recipeList.children.length > 0){ recipeList.removeChild(recipeList.firstChild); } for(let i = 0; i < merchant.recipes.length; i++){ let recipeDiv = template.cloneNode(true); recipeDiv.onclick = ()=>{controller.openSidebar("recipeDetails", merchant.recipes[i])}; recipeDiv._name = merchant.recipes[i].name; recipeList.appendChild(recipeDiv); recipeDiv.children[0].innerText = merchant.recipes[i].name; recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`; this.recipeDivList.push(recipeDiv); } }, search: function(){ let input = document.getElementById("recipeSearch").value.toLowerCase(); let recipeList = document.getElementById("recipeList"); let clearButton = document.getElementById("recipeClearButton"); let matchingRecipes = []; for(let i = 0; i < this.recipeDivList.length; i++){ if(this.recipeDivList[i]._name.toLowerCase().includes(input)){ matchingRecipes.push(this.recipeDivList[i]); } } while(recipeList.children.length > 0){ recipeList.removeChild(recipeList.firstChild); } for(let i = 0; i < matchingRecipes.length; i++){ recipeList.appendChild(matchingRecipes[i]); } if(input === ""){ clearButton.style.display = "none"; }else{ clearButton.style.display = "inline"; } }, clearSorting: function(){ document.getElementById("recipeSearch").value = ""; this.search(); }, posUpdate: function(Recipe){ let loader = document.getElementById("loaderContainer"); loader.style.display = "flex"; let url = `/recipe/update/${merchant.pos}`; fetch(url, { method: "GET", headers: { "Content-Type": "application/json;charset=utf-8" }, }) .then(response => response.json()) .then((response)=>{ console.log(response); let newRecipes = []; for(let i = 0; i < response.new.length; i++){ newRecipes.push(new Recipe( response.new[i]._id, response.new[i].name, response.new[i].price, merchant, [] )); } if(newRecipes.length > 0){ merchant.editRecipes(newRecipes); } let removeRecipes = []; for(let i = 0; i < response.removed.length; i++){ for(let j = 0; j < merchant.recipes.length; j++){ if(response.removed[i]._id === merchant.recipes[j].id){ removeRecipes.push(merchant.recipes[j], true); break; } } } if(removeRecipes.length > 0){ merchant.editRecipes(removeRecipes, true); } }) .catch((err)=>{ console.log(err); banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE"); }) .finally(()=>{ loader.style.display = "none"; }); } }