newRecipe.js 5.6 KB

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