recipeBook.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. window.recipeBookStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. this.populateRecipes();
  6. this.isPopulated = true;
  7. }
  8. },
  9. populateRecipes: function(){
  10. let recipeList = document.querySelector("#recipeList");
  11. while(recipeList.children.length > 0){
  12. recipeList.removeChild(recipeList.firstChild);
  13. }
  14. for(let recipe of merchant.recipes){
  15. let recipeDiv = document.createElement("div");
  16. recipeDiv.classList = "recipeItem";
  17. recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
  18. recipeList.appendChild(recipeDiv);
  19. let recipeName = document.createElement("p");
  20. recipeName.innerText = recipe.name;
  21. recipeDiv.appendChild(recipeName);
  22. let recipePrice = document.createElement("p");
  23. recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
  24. recipeDiv.appendChild(recipePrice);
  25. }
  26. },
  27. displayRecipe: function(recipe){
  28. openSidebar(document.querySelector("#recipeDetails"));
  29. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  30. let ingredientList = document.querySelector("#recipeIngredients");
  31. while(ingredientList.children.length > 0){
  32. ingredientList.removeChild(ingredientList.firstChild);
  33. }
  34. for(let ingredient of recipe.ingredients){
  35. let ingredientDiv = document.createElement("div");
  36. ingredientDiv.classList = "recipeIngredient";
  37. ingredientList.appendChild(ingredientDiv);
  38. let ingredientName = document.createElement("p");
  39. ingredientName.innerText = ingredient.ingredient.name;
  40. ingredientDiv.appendChild(ingredientName);
  41. let ingredientQuantity = document.createElement("p");
  42. ingredientQuantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  43. ingredientDiv.appendChild(ingredientQuantity);
  44. }
  45. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  46. },
  47. displayAddRecipe: function(){
  48. openSidebar(document.querySelector("#addRecipe"));
  49. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  50. let categories = categorizeIngredients(merchant.inventory);
  51. for(let category of categories){
  52. let optgroup = document.createElement("optgroup");
  53. optgroup.label = category.name;
  54. ingredientsSelect.appendChild(optgroup);
  55. for(let ingredient of category.ingredients){
  56. let option = document.createElement("option");
  57. option.value = ingredient.id;
  58. option.innerText = ingredient.name;
  59. optgroup.appendChild(option);
  60. }
  61. }
  62. },
  63. //Updates the number of ingredient inputs displayed for new recipes
  64. changeRecipeCount: function(){
  65. let newCount = document.querySelector("#ingredientCount").value;
  66. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  67. let oldCount = ingredientsDiv.children.length;
  68. if(newCount > oldCount){
  69. let newDivs = newCount - oldCount;
  70. for(let i = 0; i < newDivs; i++){
  71. let newNode = ingredientsDiv.children[0].cloneNode(true);
  72. newNode.children[2].children[0].value = "";
  73. ingredientsDiv.appendChild(newNode);
  74. }
  75. for(let i = 0; i < newCount; i++){
  76. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  77. }
  78. }else if(newCount < oldCount){
  79. let newDivs = oldCount - newCount;
  80. for(let i = 0; i < newDivs; i++){
  81. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  82. }
  83. }
  84. },
  85. submitNewRecipe: function(){
  86. let newRecipe = {
  87. name: document.querySelector("#newRecipeName").value,
  88. price: document.querySelector("#newRecipePrice").value,
  89. ingredients: []
  90. }
  91. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  92. for(let input of inputs){
  93. newRecipe.ingredients.push({
  94. ingredient: input.children[1].children[0].value,
  95. quantity: input.children[2].children[0].value
  96. });
  97. }
  98. if(!validator.recipe(newRecipe)){
  99. return;
  100. }
  101. fetch("/recipe/create", {
  102. method: "POST",
  103. headers: {
  104. "Content-Type": "application/json;charset=utf-8"
  105. },
  106. body: JSON.stringify(newRecipe)
  107. })
  108. .then((response) => response.json())
  109. .then((response)=>{
  110. if(typeof(response) === "string"){
  111. banner.createError(response);
  112. }else{
  113. newRecipe._id = response._id;
  114. newRecipe.price = Math.round(newRecipe.price * 100);
  115. for(let i = 0; i < newRecipe.ingredients.length; i++){
  116. newRecipe.ingredients[i].quantity = parseFloat(newRecipe.ingredients[i].quantity);
  117. }
  118. updateRecipes(newRecipe);
  119. banner.createNotification("New recipe successfully created");
  120. }
  121. })
  122. .catch((err)=>{
  123. banner.createError("Refresh page to update data");
  124. });
  125. }
  126. }