newRecipe.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. data.ingredients.push(newIngredient);
  77. }
  78. if(mismatchUnits.length === 0){
  79. this.submit(data);
  80. return;
  81. }
  82. controller.openModal("alternateUnitConversion", {mismatchUnits: mismatchUnits, recipe: data, submit: this.submit});
  83. },
  84. submit: function(data){
  85. let loader = document.getElementById("loaderContainer");
  86. loader.style.display = "flex";
  87. fetch("/recipe/create", {
  88. method: "post",
  89. headers: {
  90. "Content-Type": "application/json"
  91. },
  92. body: JSON.stringify(data)
  93. })
  94. .then(response => response.json())
  95. .then((response)=>{
  96. if(typeof(response) === "string"){
  97. controller.createBanner(response, "error");
  98. }else{
  99. merchant.addRecipes([response]);
  100. state.updateRecipes();
  101. controller.createBanner("RECIPE CREATED", "success");
  102. controller.openStrand("recipeBook");
  103. }
  104. })
  105. .catch((err)=>{
  106. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  107. })
  108. .finally(()=>{
  109. loader.style.display = "none";
  110. });
  111. },
  112. submitSpreadsheet: function(){
  113. event.preventDefault();
  114. controller.closeModal();
  115. const file = document.getElementById("spreadsheetInput").files[0];
  116. let data = new FormData();
  117. data.append("recipes", file);
  118. let loader = document.getElementById("loaderContainer");
  119. loader.style.display = "flex";
  120. fetch("/recipes/create/spreadsheet", {
  121. method: "post",
  122. body: data
  123. })
  124. .then(response => response.json())
  125. .then((response)=>{
  126. if(typeof(response) === "string"){
  127. controller.createBanner(response, "error");
  128. }else{
  129. merchant.addRecipes(response);
  130. state.updateRecipes();
  131. controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
  132. controller.openStrand("recipeBook");
  133. }
  134. })
  135. .catch((err)=>{
  136. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  137. })
  138. .finally(()=>{
  139. loader.style.display = "none";
  140. });
  141. }
  142. };
  143. module.exports = newRecipe;