recipes.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. console.log(recipes);
  2. let recipesPage = {
  3. //Display all recipes on a card
  4. displayRecipes: function(){
  5. let body = document.querySelector(".container");
  6. for(let recipe of recipes){
  7. let recipeDiv = document.createElement("div");
  8. recipeDiv.classList = "recipe-card";
  9. recipeDiv.onclick = ()=>{this.displayOneRecipe(recipe)};
  10. body.appendChild(recipeDiv);
  11. let title = document.createElement("h2");
  12. title.innerText = recipe.name;
  13. recipeDiv.appendChild(title);
  14. let ingredientList = document.createElement("ul");
  15. recipeDiv.appendChild(ingredientList);
  16. for(let ingredient of recipe.ingredients){
  17. let ul = document.createElement("li");
  18. ul.innerText = ingredient.id.name;
  19. ingredientList.appendChild(ul);
  20. }
  21. }
  22. },
  23. //Display a single recipe with all of its ingredients
  24. displayOneRecipe: function(recipe){
  25. let recipesDiv = document.querySelector("#recipes");
  26. let ingredientDiv = document.querySelector("#ingredient");
  27. let tbody = document.querySelector("tbody");
  28. let title = document.querySelector("#title");
  29. title.innerText = recipe.name;
  30. recipesDiv.style.display = "none";
  31. ingredientDiv.style.display = "flex";
  32. for(let ingredient of recipe.ingredients){
  33. let row = document.createElement("tr");
  34. tbody.appendChild(row);
  35. let name = document.createElement("td");
  36. name.innerText = ingredient.id.name;
  37. row.appendChild(name);
  38. let quantity = document.createElement("td");
  39. quantity.innerText = `${ingredient.quantity} ${ingredient.id.unitType}`;
  40. row.appendChild(quantity);
  41. let actions = document.createElement("td");
  42. row.appendChild(actions);
  43. let removeButton = document.createElement("button");
  44. removeButton.innerText = "Remove";
  45. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  46. actions.appendChild(removeButton);
  47. }
  48. },
  49. deleteIngredient: function(recipeId, ingredientId, row){
  50. row.parentNode.removeChild(row);
  51. axios.post("/recipes/ingredients/remove", {recipeId: recipeId, ingredientId:ingredientId})
  52. .then((result)=>{
  53. banner.createNotification("Ingredient has been removed from recipe");
  54. })
  55. .catch((err)=>{
  56. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  57. console.log(err);
  58. });
  59. }
  60. }
  61. recipesPage.displayRecipes();