newRecipe.js 5.7 KB

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