recipeBook.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. window.recipeBookStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let recipeList = document.querySelector("#recipeList");
  6. for(let recipe of merchant.recipes){
  7. let recipeDiv = document.createElement("div");
  8. recipeDiv.classList = "recipeItem";
  9. recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
  10. recipeList.appendChild(recipeDiv);
  11. let recipeName = document.createElement("p");
  12. recipeName.innerText = recipe.name;
  13. recipeDiv.appendChild(recipeName);
  14. let recipePrice = document.createElement("p");
  15. recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
  16. recipeDiv.appendChild(recipePrice);
  17. this.isPopulated = true;
  18. }
  19. }
  20. },
  21. displayRecipe: function(recipe){
  22. closeSidebar();
  23. document.querySelector("#recipeDetails").classList = "sidebar";
  24. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  25. let ingredientList = document.querySelector("#recipeIngredients");
  26. while(ingredientList.children.length > 0){
  27. ingredientList.removeChild(ingredientList.firstChild);
  28. }
  29. for(let ingredient of recipe.ingredients){
  30. let ingredientDiv = document.createElement("div");
  31. ingredientDiv.classList = "recipeIngredient";
  32. ingredientList.appendChild(ingredientDiv);
  33. let ingredientName = document.createElement("p");
  34. ingredientName.innerText = ingredient.ingredient.name;
  35. ingredientDiv.appendChild(ingredientName);
  36. let ingredientQuantity = document.createElement("p");
  37. ingredientQuantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  38. ingredientDiv.appendChild(ingredientQuantity);
  39. }
  40. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  41. }
  42. }