recipeDetails.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ingredientsDiv.appendChild(recipeDiv);
  19. }
  20. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  21. },
  22. remove: function(recipe){
  23. let loader = document.getElementById("loaderContainer");
  24. loader.style.display = "flex";
  25. fetch(`/recipe/remove/${recipe.id}`, {
  26. method: "delete"
  27. })
  28. .then((response) => response.json())
  29. .then((response)=>{
  30. if(typeof(response) === "string"){
  31. banner.createError(response);
  32. }else{
  33. merchant.removeRecipe(recipe);
  34. banner.createNotification("RECIPE REMOVED");
  35. controller.openStrand("recipeBook");
  36. }
  37. })
  38. .catch((err)=>{
  39. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  40. })
  41. .finally(()=>{
  42. loader.style.display = "none";
  43. });
  44. },
  45. displayAddIngredient: function(){
  46. let template = document.getElementById("addRecIngredient").content.children[0].cloneNode(true);
  47. template.name = "new";
  48. document.getElementById("recipeIngredientList").appendChild(template);
  49. let categories = merchant.categorizeIngredients();
  50. for(let i = 0; i < categories.length; i++){
  51. let optGroup = document.createElement("optgroup");
  52. optGroup.label = categories[i].name;
  53. template.children[0].appendChild(optGroup);
  54. for(let j = 0; j < categories[i].ingredients.length; j++){
  55. let option = document.createElement("option");
  56. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  57. option.ingredient = categories[i].ingredients[j].ingredient;
  58. optGroup.appendChild(option);
  59. }
  60. }
  61. }
  62. }
  63. module.exports = recipeDetails;