newRecipe.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. newRecipe.ingredients.push({
  62. ingredient: ingredient.ingredient.id,
  63. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  64. });
  65. }
  66. let loader = document.getElementById("loaderContainer");
  67. loader.style.display = "flex";
  68. fetch("/recipe/create", {
  69. method: "POST",
  70. headers: {
  71. "Content-Type": "application/json;charset=utf-8"
  72. },
  73. body: JSON.stringify(newRecipe)
  74. })
  75. .then((response) => response.json())
  76. .then((response)=>{
  77. if(typeof(response) === "string"){
  78. controller.createBanner(response, "error");
  79. }else{
  80. let ingredients = [];
  81. for(let i = 0; i < response.ingredients.length; i++){
  82. for(let j = 0; j < merchant.ingredients.length; j++){
  83. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  84. ingredients.push({
  85. ingredient: merchant.ingredients[j].ingredient,
  86. quantity: response.ingredients[i].quantity
  87. });
  88. break;
  89. }
  90. }
  91. }
  92. merchant.addRecipe(
  93. response._id,
  94. response.name,
  95. response.price,
  96. ingredients
  97. );
  98. controller.createBanner("RECIPE CREATED", "success");
  99. controller.openStrand("recipeBook");
  100. }
  101. })
  102. .catch((err)=>{
  103. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  104. })
  105. .finally(()=>{
  106. loader.style.display = "none";
  107. });
  108. },
  109. submitSpreadsheet: function(){
  110. event.preventDefault();
  111. controller.closeModal();
  112. const file = document.getElementById("spreadsheetInput").files[0];
  113. let data = new FormData();
  114. data.append("recipes", file);
  115. let loader = document.getElementById("loaderContainer");
  116. loader.style.display = "flex";
  117. fetch("/recipes/create/spreadsheet", {
  118. method: "post",
  119. body: data
  120. })
  121. .then(response => response.json())
  122. .then((response)=>{
  123. if(typeof(response) === "String"){
  124. controller.createBanner(response, "error");
  125. }else{
  126. for(let i = 0; i < response.length; i++){
  127. merchant.addRecipe(
  128. response[i]._id,
  129. response[i].name,
  130. response[i].price,
  131. response[i].ingredients
  132. );
  133. }
  134. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  135. controller.openStrand("recipeBook");
  136. }
  137. })
  138. .catch((err)=>{
  139. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  140. })
  141. .finally(()=>{
  142. loader.style.display = "none";
  143. });
  144. }
  145. }
  146. module.exports = newRecipe;