newRecipe.js 5.9 KB

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