recipeDetails.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. let recipeDetails = {
  2. display: function(recipe){
  3. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  4. document.getElementById("recipeName").innerText = recipe.name;
  5. if(merchant.pos === "none"){
  6. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  7. }
  8. //ingredient list
  9. let ingredientsDiv = document.getElementById("recipeIngredientList");
  10. while(ingredientsDiv.children.length > 0){
  11. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  12. }
  13. let template = document.getElementById("recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. let recipeDiv = template.cloneNode(true);
  16. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  18. recipeDiv.onclick = ()=>{
  19. controller.openStrand("ingredients");
  20. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  21. }
  22. ingredientsDiv.appendChild(recipeDiv);
  23. }
  24. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  25. },
  26. remove: function(recipe){
  27. let loader = document.getElementById("loaderContainer");
  28. loader.style.display = "flex";
  29. fetch(`/recipe/remove/${recipe.id}`, {
  30. method: "delete"
  31. })
  32. .then((response) => response.json())
  33. .then((response)=>{
  34. if(typeof(response) === "string"){
  35. banner.createError(response);
  36. }else{
  37. merchant.removeRecipe(recipe);
  38. banner.createNotification("RECIPE REMOVED");
  39. controller.openStrand("recipeBook");
  40. }
  41. })
  42. .catch((err)=>{
  43. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  44. })
  45. .finally(()=>{
  46. loader.style.display = "none";
  47. });
  48. }
  49. }
  50. module.exports = recipeDetails;