recipeDetails.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. let recipeDetails = {
  2. display: function(recipe){
  3. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  4. let showIcon = document.getElementById("showRecipeBtn");
  5. let hideIcon = document.getElementById("hideRecipeBtn");
  6. if(recipe.hidden === true){
  7. showIcon.style.display = "block";
  8. hideIcon.style.display = "none";
  9. showIcon.onclick = ()=>{this.hide(recipe)};
  10. }else{
  11. showIcon.style.display = "none";
  12. hideIcon.style.display = "block";
  13. showIcon.onclick = ()=>{this.hide(recipe)};
  14. }
  15. document.getElementById("hideRecipeBtn").onclick = ()=>{this.hide(recipe)};
  16. document.getElementById("recipeName").innerText = recipe.name;
  17. document.getElementById("recipeCategory").innerText = recipe.category;
  18. let button = document.getElementById("removeRecipeBtn");
  19. switch(merchant.pos){
  20. case "square":
  21. button.style.display = "none";
  22. break;
  23. case "none":
  24. button.style.display = "block";
  25. button.onclick = ()=>{controller.openModal("confirmDeleteRecipe", recipe)};
  26. break;
  27. }
  28. //ingredient list
  29. let ingredientsDiv = document.getElementById("recipeIngredientList");
  30. while(ingredientsDiv.children.length > 0){
  31. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  32. }
  33. let template = document.getElementById("recipeIngredient").content.children[0];
  34. for(let i = 0; i < recipe.ingredients.length; i++){
  35. let recipeDiv = template.cloneNode(true);
  36. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  37. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  38. recipeDiv.onclick = ()=>{
  39. controller.openStrand("ingredients");
  40. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  41. }
  42. ingredientsDiv.appendChild(recipeDiv);
  43. }
  44. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  45. },
  46. hide: function(recipe){
  47. recipe.hidden = (recipe.hidden === true) ? false : true;
  48. state.updateRecipes();
  49. controller.openStrand("recipeBook");
  50. controller.openSidebar("recipeDetails", recipe);
  51. fetch(`/recipes/hide/${recipe.id}`)
  52. .then(response => response.json())
  53. .then((response)=>{
  54. if(typeof(response) === "string"){
  55. recipe.hidden = (recipe.hidden === true) ? false : true;
  56. controller.openStrand("recipes");
  57. controller.createBanner(response, "error");
  58. }
  59. })
  60. .catch((err)=>{});
  61. },
  62. remove: function(recipe){
  63. let loader = document.getElementById("loaderContainer");
  64. loader.style.display = "flex";
  65. fetch(`/recipe/remove/${recipe.id}`, {
  66. method: "delete"
  67. })
  68. .then((response) => response.json())
  69. .then((response)=>{
  70. if(typeof(response) === "string"){
  71. controller.createBanner(response, "error");
  72. }else{
  73. merchant.removeRecipe(recipe);
  74. state.updateRecipes();
  75. controller.createBanner("RECIPE REMOVED", "success");
  76. controller.closeModal();
  77. controller.openStrand("recipeBook");
  78. }
  79. })
  80. .catch((err)=>{
  81. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  82. })
  83. .finally(()=>{
  84. loader.style.display = "none";
  85. });
  86. }
  87. }
  88. module.exports = recipeDetails;