recipeDetails.js 3.8 KB

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