newRecipe.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. state.updateRecipes();
  83. controller.createBanner("RECIPE CREATED", "success");
  84. controller.openStrand("recipeBook");
  85. }
  86. })
  87. .catch((err)=>{
  88. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  89. })
  90. .finally(()=>{
  91. loader.style.display = "none";
  92. });
  93. },
  94. submitSpreadsheet: function(){
  95. event.preventDefault();
  96. controller.closeModal();
  97. const file = document.getElementById("spreadsheetInput").files[0];
  98. let data = new FormData();
  99. data.append("recipes", file);
  100. let loader = document.getElementById("loaderContainer");
  101. loader.style.display = "flex";
  102. fetch("/recipes/create/spreadsheet", {
  103. method: "post",
  104. body: data
  105. })
  106. .then(response => response.json())
  107. .then((response)=>{
  108. if(typeof(response) === "string"){
  109. controller.createBanner(response, "error");
  110. }else{
  111. merchant.addRecipes(response);
  112. state.updateRecipes();
  113. controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
  114. controller.openStrand("recipeBook");
  115. }
  116. })
  117. .catch((err)=>{
  118. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  119. })
  120. .finally(()=>{
  121. loader.style.display = "none";
  122. });
  123. }
  124. }
  125. module.exports = newRecipe;