recipeDetails.js 2.9 KB

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