recipeDetails.js 2.6 KB

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