newRecipe.js 5.9 KB

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