newRecipe.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. let newRecipe = {
  2. display: function(Recipe){
  3. document.getElementById("newRecipeName").value = "";
  4. document.getElementById("newRecipePrice").value = "";
  5. document.getElementById("ingredientCount").value = 1;
  6. let categories = merchant.categorizeIngredients();
  7. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  8. while(ingredientsSelect.children.length > 0){
  9. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  10. }
  11. this.changeIngredientCount(categories);
  12. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  13. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  14. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  15. },
  16. //Updates the number of ingredient inputs displayed for new recipes
  17. changeIngredientCount: function(categories){
  18. let newCount = document.getElementById("ingredientCount").value;
  19. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  20. let template = document.getElementById("recipeInputIngredient").content.children[0];
  21. let oldCount = ingredientsDiv.children.length;
  22. if(newCount > oldCount){
  23. let newDivs = newCount - oldCount;
  24. for(let i = 0; i < newDivs; i++){
  25. let newNode = template.cloneNode(true);
  26. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  27. newNode.children[2].children[0].value = 0;
  28. for(let j = 0; j < categories.length; j++){
  29. let optgroup = document.createElement("optgroup");
  30. optgroup.label = categories[j].name;
  31. for(let k = 0; k < categories[j].ingredients.length; k++){
  32. let option = document.createElement("option");
  33. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  34. option.ingredient = categories[j].ingredients[k];
  35. optgroup.appendChild(option);
  36. }
  37. newNode.children[1].children[0].appendChild(optgroup);
  38. }
  39. ingredientsDiv.appendChild(newNode);
  40. }
  41. for(let i = 0; i < newCount; i++){
  42. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  43. }
  44. }else if(newCount < oldCount){
  45. let newDivs = oldCount - newCount;
  46. for(let i = 0; i < newDivs; i++){
  47. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  48. }
  49. }
  50. },
  51. submit: function(Recipe){
  52. let newRecipe = {
  53. name: document.getElementById("newRecipeName").value,
  54. price: document.getElementById("newRecipePrice").value,
  55. ingredients: []
  56. }
  57. let inputs = document.getElementById("recipeInputIngredients").children;
  58. for(let i = 0; i < inputs.length; i++){
  59. let sel = inputs[i].children[1].children[0];
  60. let ingredient = sel.options[sel.selectedIndex].ingredient;
  61. let newIngredient = {
  62. ingredient: ingredient.ingredient.id,
  63. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  64. };
  65. if(ingredient.ingredient.specialUnit === "bottle"){
  66. newIngredient.quantity = inputs[i].children[2].children[0].value;
  67. }
  68. newRecipe.ingredients.push(newIngredient);
  69. }
  70. let loader = document.getElementById("loaderContainer");
  71. loader.style.display = "flex";
  72. fetch("/recipe/create", {
  73. method: "POST",
  74. headers: {
  75. "Content-Type": "application/json;charset=utf-8"
  76. },
  77. body: JSON.stringify(newRecipe)
  78. })
  79. .then((response) => response.json())
  80. .then((response)=>{
  81. if(typeof(response) === "string"){
  82. controller.createBanner(response, "error");
  83. }else{
  84. let ingredients = [];
  85. for(let i = 0; i < response.ingredients.length; i++){
  86. for(let j = 0; j < merchant.ingredients.length; j++){
  87. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  88. ingredients.push({
  89. ingredient: merchant.ingredients[j].ingredient.id,
  90. quantity: response.ingredients[i].quantity
  91. });
  92. break;
  93. }
  94. }
  95. }
  96. merchant.addRecipe(
  97. response._id,
  98. response.name,
  99. response.price,
  100. ingredients
  101. );
  102. controller.createBanner("RECIPE CREATED", "success");
  103. controller.openStrand("recipeBook");
  104. }
  105. })
  106. .catch((err)=>{
  107. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  108. })
  109. .finally(()=>{
  110. loader.style.display = "none";
  111. });
  112. },
  113. submitSpreadsheet: function(){
  114. event.preventDefault();
  115. controller.closeModal();
  116. const file = document.getElementById("spreadsheetInput").files[0];
  117. let data = new FormData();
  118. data.append("recipes", file);
  119. let loader = document.getElementById("loaderContainer");
  120. loader.style.display = "flex";
  121. fetch("/recipes/create/spreadsheet", {
  122. method: "post",
  123. body: data
  124. })
  125. .then(response => response.json())
  126. .then((response)=>{
  127. if(typeof(response) === "string"){
  128. controller.createBanner(response, "error");
  129. }else{
  130. for(let i = 0; i < response.length; i++){
  131. merchant.addRecipe(
  132. response[i]._id,
  133. response[i].name,
  134. response[i].price,
  135. response[i].ingredients
  136. );
  137. }
  138. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  139. controller.openStrand("recipeBook");
  140. }
  141. })
  142. .catch((err)=>{
  143. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  144. })
  145. .finally(()=>{
  146. loader.style.display = "none";
  147. });
  148. }
  149. }
  150. module.exports = newRecipe;