newRecipe.js 5.8 KB

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