editRecipe.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. module.exports = {
  2. unused: [],
  3. display: function(recipe){
  4. let title = document.getElementById("editRecipeTitle");
  5. let name = document.getElementById("editRecipeName");
  6. let category = document.getElementById("editRecipeCategory");
  7. let price = document.getElementById("editRecipePrice");
  8. title.innerText = recipe.name;
  9. name.children[0].value = recipe.name;
  10. category.children[0].value = recipe.category;
  11. price.children[0].value = recipe.price;
  12. if(merchant.pos === "none"){
  13. name.style.display = "flex";
  14. category.style.display = "flex";
  15. price.style.display = "flex";
  16. title.style.display = "none";
  17. }
  18. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  19. document.getElementById("editRecipeSearch").oninput = ()=>{this.search()};
  20. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe.id)};
  21. let used = document.getElementById("editRecipeUsed");
  22. let template = document.getElementById("editRecipeInputItem").content.children[0];
  23. let tempList = [];
  24. while(used.children.length > 0){
  25. used.removeChild(used.firstChild);
  26. }
  27. for(let i = 0; i < recipe.ingredients.length; i++){
  28. tempList.push(recipe.ingredients[i].ingredient.id);
  29. let ingredient = template.cloneNode(true);
  30. ingredient.ingredient = recipe.ingredients[i].ingredient;
  31. ingredient.children[0].children[0].innerText = recipe.ingredients[i].ingredient.name;
  32. ingredient.children[0].children[1].onclick = ()=>{this.removeIngredient(ingredient)};
  33. ingredient.children[1].children[0].value = recipe.ingredients[i].quantity;
  34. ingredient.children[1].children[1].value = recipe.ingredients[i].unit;
  35. used.appendChild(ingredient);
  36. }
  37. this.unused = [];
  38. for(let i = 0; i < merchant.inventory.length; i++){
  39. if(tempList.includes(merchant.inventory[i].ingredient.id)) continue;
  40. this.unused.push(merchant.inventory[i].ingredient);
  41. }
  42. this.displayUnused(this.unused);
  43. },
  44. displayUnused: function(items){
  45. let container = document.getElementById("editRecipeUnused");
  46. items.sort((a, b) => (a.name > b.name) ? 1 : -1);
  47. while(container.children.length > 0){
  48. container.removeChild(container.firstChild);
  49. }
  50. for(let i = 0; i < items.length; i++){
  51. let button = document.createElement("button");
  52. button.innerText = items[i].name;
  53. button.classList.add("choosable");
  54. button.classList.add("selection");
  55. button.ingredient = items[i];
  56. button.onclick = ()=>{this.addIngredient(button)};
  57. container.appendChild(button);
  58. }
  59. },
  60. addIngredient: function(ingredient){
  61. for(let i = 0; i < this.unused.length; i++){
  62. if(this.unused[i] === ingredient.ingredient){
  63. this.unused.splice(i, 1);
  64. break;
  65. }
  66. }
  67. let unused = document.getElementById("editRecipeUnused");
  68. unused.removeChild(ingredient);
  69. let used = document.getElementById("editRecipeUsed");
  70. let newItem = document.getElementById("editRecipeInputItem").content.children[0].cloneNode(true);
  71. newItem.ingredient = ingredient.ingredient;
  72. newItem.children[0].children[0].innerText = ingredient.ingredient.name;
  73. newItem.children[0].children[1].onclick = ()=>{this.removeIngredient(newItem)};
  74. let select = newItem.children[1].children[1];
  75. if(ingredient.ingredient.convert.toMass !== undefined) select.children[0].style.display = "block";
  76. if(ingredient.ingredient.convert.toVolume !== undefined) select.children[1].style.display = "block";
  77. if(ingredient.ingredient.convert.toLength !== undefined) select.children[2].style.display = "block";
  78. switch(ingredient.ingredient.unitType){
  79. case "mass": select.value = "g"; break;
  80. case "volume": select.value = "ml"; break;
  81. case "length": select.value = "mm"; break;
  82. case "other":
  83. select.value = "each";
  84. select.children[3].style.display = "block";
  85. break;
  86. }
  87. used.appendChild(newItem);
  88. },
  89. removeIngredient: function(ingredient){
  90. let used = document.getElementById("editRecipeUsed");
  91. this.unused.push(ingredient.ingredient);
  92. used.removeChild(ingredient);
  93. this.displayUnused(this.unused);
  94. },
  95. search: function(){
  96. let text = document.getElementById("editRecipeSearch").value;
  97. let newList = [];
  98. for(let i = 0; i < this.unused.length; i++){
  99. let name = this.unused[i].name.toLowerCase();
  100. if(name.includes(text) === true){
  101. newList.push(this.unused[i]);
  102. }
  103. }
  104. this.displayUnused(newList);
  105. },
  106. submit: function(id){
  107. let data = {
  108. id: id,
  109. name: document.getElementById("editRecipeName").children[0].value,
  110. price: parseInt(document.getElementById("editRecipePrice").children[0].value * 100),
  111. category: document.getElementById("editRecipeCategory").children[0].value,
  112. ingredients: []
  113. };
  114. let divs = document.getElementById("editRecipeUsed").children;
  115. for(let i = 0; i < divs.length; i++){
  116. data.ingredients.push({
  117. ingredient: divs[i].ingredient.id,
  118. quantity: divs[i].children[1].children[0].value,
  119. unit: divs[i].children[1].children[1].value
  120. });
  121. }
  122. let loader = document.getElementById("loaderContainer");
  123. loader.style.display = "flex";
  124. fetch("/recipe/update", {
  125. method: "put",
  126. headers: {
  127. "Content-Type": "application/json"
  128. },
  129. body: JSON.stringify(data)
  130. })
  131. .then(response => response.json())
  132. .then((response)=>{
  133. if(typeof(response) === "string"){
  134. controller.createBanner(response, "error");
  135. }else{
  136. merchant.updateRecipe(merchant.getRecipe(response._id), response);
  137. state.updateRecipes();
  138. controller.closeSidebar();
  139. controller.createBanner("RECIPE UPDATED", "success");
  140. }
  141. })
  142. .catch((err)=>{
  143. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  144. })
  145. .finally(()=>{
  146. loader.style.display = "none";
  147. });
  148. }
  149. }