recipes.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. displayOneRecipe: function(recipe){
  24. let recipesDiv = document.querySelector("#recipes");
  25. let ingredientDiv = document.querySelector("#ingredient");
  26. let tbody = document.querySelector("tbody");
  27. recipesDiv.style.display = "none";
  28. ingredientDiv.style.display = "flex";
  29. for(let ingredient of recipe.ingredients){
  30. let row = document.createElement("tr");
  31. tbody.appendChild(row);
  32. let name = document.createElement("td");
  33. name.innerText = ingredient.id.name;
  34. row.appendChild(name);
  35. let quantity = document.createElement("td");
  36. quantity.innerText = `${ingredient.quantity} ${ingredient.id.unitType}`;
  37. row.appendChild(quantity);
  38. let actions = document.createElement("td");
  39. row.appendChild(actions);
  40. let removeButton = document.createElement("button");
  41. removeButton.innerText = "Remove";
  42. removeButton.onclick = ()=>{deleteIngredient(ingredient);};
  43. actions.appendChild(removeButton);
  44. }
  45. },
  46. deleteIngredient: function(ingredient){
  47. }
  48. }
  49. recipesPage.displayRecipes();