recipeDetails.js 3.1 KB

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