recipeDetails.js 3.2 KB

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