newRecipe.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.submit()};
  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. let select = element.children[1].children[1];
  54. if(ingredient.convert.toMass !== undefined) select.children[0].style.display = "block";
  55. if(ingredient.convert.toVolume !== undefined) select.children[1].style.display = "block";
  56. if(ingredient.convert.toLength !== undefined) select.children[2].style.display = "block";
  57. switch(ingredient.unitType){
  58. case "mass": select.value = "g"; break;
  59. case "volume": select.value = "ml"; break;
  60. case "length": select.value = "mm"; break;
  61. case "other":
  62. select.children[3].style.display = "block";
  63. select.value = "each";
  64. break;
  65. }
  66. document.getElementById("newRecipeChosenList").appendChild(element);
  67. },
  68. submit: function(){
  69. let data = {
  70. name: document.getElementById("newRecipeName").value,
  71. price: parseInt(document.getElementById("newRecipePrice").value * 100),
  72. category: document.getElementById("newRecipeCategory").value,
  73. ingredients: []
  74. }
  75. let ingredients = document.getElementById("newRecipeChosenList").children;
  76. for(let i = 0; i < ingredients.length; i++){
  77. let ingredient = ingredients[i].ingredient;
  78. data.ingredients.push({
  79. ingredient: ingredient.id,
  80. quantity: ingredients[i].children[1].children[0].value,
  81. unit: ingredients[i].children[1].children[1].value
  82. });
  83. }
  84. let loader = document.getElementById("loaderContainer");
  85. loader.style.display = "flex";
  86. fetch("/recipe/create", {
  87. method: "post",
  88. headers: {
  89. "Content-Type": "application/json"
  90. },
  91. body: JSON.stringify(data)
  92. })
  93. .then(response => response.json())
  94. .then((response)=>{
  95. if(typeof(response) === "string"){
  96. controller.createBanner(response, "error");
  97. }else{
  98. merchant.addRecipes([response]);
  99. state.updateRecipes();
  100. controller.createBanner("RECIPE CREATED", "success");
  101. controller.openStrand("recipeBook");
  102. }
  103. })
  104. .catch((err)=>{
  105. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  106. })
  107. .finally(()=>{
  108. loader.style.display = "none";
  109. });
  110. },
  111. submitSpreadsheet: function(){
  112. event.preventDefault();
  113. controller.closeModal();
  114. const file = document.getElementById("spreadsheetInput").files[0];
  115. let data = new FormData();
  116. data.append("recipes", file);
  117. let loader = document.getElementById("loaderContainer");
  118. loader.style.display = "flex";
  119. fetch("/recipes/create/spreadsheet", {
  120. method: "post",
  121. body: data
  122. })
  123. .then(response => response.json())
  124. .then((response)=>{
  125. if(typeof(response) === "string"){
  126. controller.createBanner(response, "error");
  127. }else{
  128. merchant.addRecipes(response);
  129. state.updateRecipes();
  130. controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
  131. controller.openStrand("recipeBook");
  132. }
  133. })
  134. .catch((err)=>{
  135. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  136. })
  137. .finally(()=>{
  138. loader.style.display = "none";
  139. });
  140. }
  141. };
  142. module.exports = newRecipe;