newRecipe.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. let newRecipe = {
  2. unchosen: [],
  3. display: function(){
  4. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  5. document.getElementById("newRecipeName").value = "";
  6. document.getElementById("newRecipeCategory").value = "";
  7. document.getElementById("newRecipePrice").value = "";
  8. let chosen = document.getElementById("newRecipeChosenList");
  9. while(chosen.children.length > 0){
  10. chosen.removeChild(chosen.firstChild);
  11. }
  12. document.getElementById("submitNewRecipe").onclick = ()=>{this.gatherData()};
  13. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  14. document.getElementById("newRecipeSearch").onkeyup = ()=>{this.populateChoices()};
  15. this.unchosen = [];
  16. for(let i = 0; i < merchant.inventory.length; i++){
  17. this.unchosen.push(merchant.inventory[i].ingredient);
  18. }
  19. this.populateChoices();
  20. },
  21. populateChoices: function(){
  22. this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
  23. let searchStr = document.getElementById("newRecipeSearch").value;
  24. let list = document.getElementById("recipeChoices");
  25. while(list.children.length > 0){
  26. list.removeChild(list.firstChild);
  27. }
  28. for(let i = 0; i < this.unchosen.length; i++){
  29. if(searchStr === "" || this.unchosen[i].name.toLowerCase().includes(searchStr)){
  30. let ingredient = document.createElement("button");
  31. ingredient.innerText = this.unchosen[i].name;
  32. ingredient.classList.add("choosable");
  33. ingredient.classList.add("selection");
  34. ingredient.onclick = ()=>{
  35. this.add(this.unchosen[i]);
  36. this.unchosen.splice(i, 1);
  37. this.populateChoices();
  38. };
  39. list.appendChild(ingredient);
  40. }
  41. }
  42. },
  43. add: function(ingredient){
  44. let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
  45. element.children[0].children[0].innerText = ingredient.name;
  46. element.children[1].children[0].placeholder = "QUANTITY";
  47. element.children[0].children[1].onclick = ()=>{
  48. this.unchosen.push(ingredient);
  49. element.parentElement.removeChild(element);
  50. this.populateChoices();
  51. };
  52. element.ingredient = ingredient;
  53. document.getElementById("newRecipeChosenList").appendChild(element);
  54. },
  55. gatherData: function(){
  56. let data = {
  57. name: document.getElementById("newRecipeName").value,
  58. category: document.getElementById("newRecipeCategory").value,
  59. price: parseInt(document.getElementById("newRecipePrice").value * 100),
  60. ingredients: []
  61. };
  62. let mismatchUnits = [];
  63. let ingredients = document.getElementById("newRecipeChosenList").children;
  64. for(let i = 0; i < ingredients.length; i++){
  65. let ingredient = ingredients[i].ingredient;
  66. let newIngredient = {
  67. ingredient: ingredient.id,
  68. quantity: ingredients[i].children[1].children[0].value,
  69. unit: ingredients[i].children[1].children[1].value
  70. }
  71. // if(ingredient.getPotentialUnits().includes(newIngredient.unit) === false){
  72. // mismatchUnits.push({ingredient: ingredient, newIngredient: newIngredient});
  73. // }else{
  74. // newIngredient.baseUnitMultiplier = 1 / controller.baseUnit(1, newIngredient.unit);
  75. // }
  76. if(["g", "kg", "oz", "lb"].includes(newIngredient.unit) && ingredient.toMass === undefined){
  77. mismatchUnits.push({ingredient: ingredient, newIngredient: newIngredient});
  78. }else if(["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"].includes(newIngredient.unit) && ingredient.toVolume === undefined){
  79. mismatchUnits.push({ingredient: ingredient, newIngredient: newIngredient});
  80. }else if(["mm", "cm", "m", "in", "ft"].includes(newIngredient.unit) && ingredient.toLength === undefined){
  81. mismatchUnits.push({ingredient: ingredient, newIngredient: newIngredient});
  82. }
  83. data.ingredients.push(newIngredient);
  84. }
  85. if(mismatchUnits.length === 0){
  86. this.submit(data);
  87. return;
  88. }
  89. controller.openModal("alternateUnitConversion", {mismatchUnits: mismatchUnits, recipe: data, submit: this.submit});
  90. },
  91. submit: function(data){
  92. let loader = document.getElementById("loaderContainer");
  93. loader.style.display = "flex";
  94. fetch("/recipe/create", {
  95. method: "post",
  96. headers: {
  97. "Content-Type": "application/json"
  98. },
  99. body: JSON.stringify(data)
  100. })
  101. .then(response => response.json())
  102. .then((response)=>{
  103. if(typeof(response) === "string"){
  104. controller.createBanner(response, "error");
  105. }else{
  106. merchant.addRecipes([response]);
  107. state.updateRecipes();
  108. controller.createBanner("RECIPE CREATED", "success");
  109. controller.openStrand("recipeBook");
  110. }
  111. })
  112. .catch((err)=>{
  113. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  114. })
  115. .finally(()=>{
  116. loader.style.display = "none";
  117. });
  118. },
  119. submitSpreadsheet: function(){
  120. event.preventDefault();
  121. controller.closeModal();
  122. const file = document.getElementById("spreadsheetInput").files[0];
  123. let data = new FormData();
  124. data.append("recipes", file);
  125. let loader = document.getElementById("loaderContainer");
  126. loader.style.display = "flex";
  127. fetch("/recipes/create/spreadsheet", {
  128. method: "post",
  129. body: data
  130. })
  131. .then(response => response.json())
  132. .then((response)=>{
  133. if(typeof(response) === "string"){
  134. controller.createBanner(response, "error");
  135. }else{
  136. merchant.addRecipes(response);
  137. state.updateRecipes();
  138. controller.createBanner("ALL RECIPES 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;