components.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. let recipeDetailsComp = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. openSidebar(document.querySelector("#recipeDetails"));
  6. document.querySelector("#recipeName").style.display = "block";
  7. document.querySelector("#recipeNameIn").style.display = "none";
  8. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  9. let ingredientList = document.querySelector("#recipeIngredientList");
  10. while(ingredientList.children.length > 0){
  11. ingredientList.removeChild(ingredientList.firstChild);
  12. }
  13. let template = document.querySelector("#recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. ingredientDiv = template.cloneNode(true);
  16. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  18. ingredientDiv._id = recipe.ingredients[i].ingredient._id;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. let name = document.querySelector("#recipeName");
  32. let nameIn = document.querySelector("#recipeNameIn");
  33. name.style.display = "none";
  34. nameIn.style.display = "block";
  35. nameIn.placeholder = name.innerText;
  36. for(let i = 0; i < ingredientDivs.children.length; i++){
  37. let div = ingredientDivs.children[i];
  38. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  39. div.children[1].style.display = "block";
  40. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  41. div.children[3].style.display = "block";
  42. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  43. }
  44. document.querySelector("#addRecIng").style.display = "flex";
  45. let price = document.querySelector("#recipePrice");
  46. price.children[1].style.display = "none";
  47. price.children[2].style.display = "block";
  48. price.children[2].placeholder = price.children[1].innerText;
  49. document.querySelector("#recipeUpdate").style.display = "flex";
  50. },
  51. update: function(){
  52. let updatedRecipe = {
  53. _id: this.recipe._id,
  54. name: document.querySelector("#recipeNameIn").value || this.recipe.name,
  55. price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
  56. ingredients: []
  57. }
  58. let divs = document.querySelector("#recipeIngredientList").children;
  59. for(let i = 0; i < divs.length; i++){
  60. if(divs[i].name === "new"){
  61. updatedRecipe.ingredients.push({
  62. ingredient: divs[i].children[0].value,
  63. quantity: divs[i].children[1].value
  64. })
  65. }else{
  66. updatedRecipe.ingredients.push({
  67. ingredient: divs[i]._id,
  68. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  69. });
  70. }
  71. }
  72. if(validator.recipe(updatedRecipe)){
  73. fetch("/recipe/update", {
  74. method: "PUT",
  75. headers: {
  76. "Content-Type": "application/json;charset=utf-8"
  77. },
  78. body: JSON.stringify(updatedRecipe)
  79. })
  80. .then((response) => response.json())
  81. .then((response)=>{
  82. if(typeof(response) === "string"){
  83. banner.createError(response);
  84. }else{
  85. updateRecipes(updatedRecipe);
  86. banner.createNotification("Recipe successfully updated");
  87. }
  88. })
  89. .catch((err)=>{
  90. banner.createError("Something went wrong. Please refresh the page");
  91. })
  92. }
  93. },
  94. remove: function(){
  95. fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
  96. method: "DELETE"
  97. })
  98. .then((response) => response.json())
  99. .then((response)=>{
  100. if(typeof(response) === "string"){
  101. banner.createError(response);
  102. }else{
  103. updateRecipes(this.recipe, true);
  104. banner.createNotification("Recipe removed");
  105. }
  106. })
  107. .catch((err)=>{
  108. banner.createError("Something went wrong. Try refreshing the page");
  109. });
  110. },
  111. displayAddIngredient: function(){
  112. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  113. template.name = "new";
  114. document.querySelector("#recipeIngredientList").appendChild(template);
  115. let categories = categorizeIngredients(merchant.inventory);
  116. for(let i = 0; i < categories.length; i++){
  117. let optGroup = document.createElement("optgroup");
  118. optGroup.label = categories[i].name;
  119. template.children[0].appendChild(optGroup);
  120. for(let j = 0; j < categories[i].ingredients.length; j++){
  121. let option = document.createElement("option");
  122. option.innerText = `${categories[i].ingredients[j].name} (${categories[i].ingredients[j].unit})`;
  123. option.value = categories[i].ingredients[j].id;
  124. optGroup.appendChild(option);
  125. }
  126. }
  127. }
  128. }
  129. let newOrderComp = {
  130. display: function(){
  131. openSidebar(document.querySelector("#newOrder"));
  132. console.log(merchant.inventory);
  133. let categories = categorizeIngredients(merchant.inventory);
  134. let categoriesList = document.querySelector("#newOrderCategories");
  135. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  136. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  137. for(let i = 0; i < categories.length; i++){
  138. let category = template.cloneNode(true);
  139. category.children[0].children[0].innerText = categories[i].name;
  140. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  141. category.children[0].children[1].children[1].style.display = "none";
  142. category.children[1].style.display = "none";
  143. categoriesList.appendChild(category);
  144. for(let j = 0; j < categories[i].ingredients.length; j++){
  145. let ingredientDiv = ingredientTemplate.cloneNode(true);
  146. ingredientDiv.children[1].innerText = categories[i].ingredients[j].name;
  147. ingredientDiv._id = categories[i].ingredients[j].id;
  148. ingredientDiv._name = categories[i].ingredients[j].name;
  149. ingredientDiv._unit = categories[i].ingredients[j].unit;
  150. ingredientDiv._category = categories[i].name;
  151. category.children[1].appendChild(ingredientDiv);
  152. }
  153. }
  154. }
  155. }