editRecipe.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = "other";
  84. select.children[3].style.display = "block";
  85. }
  86. used.appendChild(newItem);
  87. },
  88. removeIngredient: function(ingredient){
  89. let used = document.getElementById("editRecipeUsed");
  90. this.unused.push(ingredient.ingredient);
  91. used.removeChild(ingredient);
  92. this.displayUnused(this.unused);
  93. },
  94. search: function(){
  95. let text = document.getElementById("editRecipeSearch").value;
  96. let newList = [];
  97. for(let i = 0; i < this.unused.length; i++){
  98. let name = this.unused[i].name.toLowerCase();
  99. if(name.includes(text) === true){
  100. newList.push(this.unused[i]);
  101. }
  102. }
  103. this.displayUnused(newList);
  104. },
  105. submit: function(id){
  106. let data = {
  107. id: id,
  108. name: document.getElementById("editRecipeName").children[0].value,
  109. price: parseInt(document.getElementById("editRecipePrice").children[0].value * 100),
  110. category: document.getElementById("editRecipeCategory").children[0].value,
  111. ingredients: []
  112. };
  113. let divs = document.getElementById("editRecipeUsed").children;
  114. for(let i = 0; i < divs.length; i++){
  115. data.ingredients.push({
  116. ingredient: divs[i].ingredient.id,
  117. quantity: divs[i].children[1].children[0].value,
  118. unit: divs[i].children[1].children[1].value
  119. });
  120. }
  121. let loader = document.getElementById("loaderContainer");
  122. loader.style.display = "flex";
  123. fetch("/recipe/update", {
  124. method: "put",
  125. headers: {
  126. "Content-Type": "application/json"
  127. },
  128. body: JSON.stringify(data)
  129. })
  130. .then(response => response.json())
  131. .then((response)=>{
  132. if(typeof(response) === "string"){
  133. controller.createBanner(response, "error");
  134. }else{
  135. merchant.updateRecipe(merchant.getRecipe(response._id), response);
  136. state.updateRecipes();
  137. controller.closeSidebar();
  138. controller.createBanner("RECIPE UPDATED", "success");
  139. }
  140. })
  141. .catch((err)=>{
  142. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  143. })
  144. .finally(()=>{
  145. loader.style.display = "none";
  146. });
  147. }
  148. }