components.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. isPopulated: false,
  131. display: function(){
  132. openSidebar(document.querySelector("#newOrder"));
  133. if(!this.isPopulated){
  134. let categories = categorizeIngredients(merchant.inventory);
  135. let categoriesList = document.querySelector("#newOrderCategories");
  136. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  137. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  138. for(let i = 0; i < categories.length; i++){
  139. let category = template.cloneNode(true);
  140. category.children[0].children[0].innerText = categories[i].name;
  141. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  142. category.children[0].children[1].children[1].style.display = "none";
  143. category.children[1].style.display = "none";
  144. categoriesList.appendChild(category);
  145. for(let j = 0; j < categories[i].ingredients.length; j++){
  146. let ingredientDiv = ingredientTemplate.cloneNode(true);
  147. ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
  148. ingredientDiv.children[1].placeholder = categories[i].ingredients[j].unit;
  149. ingredientDiv._id = categories[i].ingredients[j].id;
  150. ingredientDiv._name = categories[i].ingredients[j].name;
  151. ingredientDiv._unit = categories[i].ingredients[j].unit;
  152. ingredientDiv._category = categories[i].name;
  153. let priceInput = document.createElement("input");
  154. priceInput.type = "number";
  155. priceInput.min = "0";
  156. priceInput.step = "0.01";
  157. priceInput.placeholder = "Price Per Unit";
  158. ingredientDiv.appendChild(priceInput);
  159. category.children[1].appendChild(ingredientDiv);
  160. }
  161. }
  162. this.isPopulated = true;
  163. }
  164. },
  165. submit: function(){
  166. let categoriesList = document.querySelector("#newOrderCategories");
  167. let newOrder = {
  168. orderId: document.querySelector("#orderName").value,
  169. date: new Date(document.querySelector("#orderDate").value),
  170. ingredients: []
  171. }
  172. for(let i = 0; i < categoriesList.children.length; i++){
  173. for(let j = 0; j < categoriesList.children[i].children[1].children.length; j++){
  174. let ingredientDiv = categoriesList.children[i].children[1].children[j];
  175. let quantity = ingredientDiv.children[1].value;
  176. let price = ingredientDiv.children[2].value;
  177. if(quantity !== "" || price !== ""){
  178. let newIngredient = {
  179. ingredient: ingredientDiv._id,
  180. quantity: quantity,
  181. price: parseInt(price * 100)
  182. }
  183. newOrder.ingredients.push(newIngredient);
  184. }
  185. }
  186. }
  187. if(validator.order(newOrder)){
  188. fetch("/order", {
  189. method: "POST",
  190. headers: {
  191. "Content-Type": "application/json;charset=utf-8"
  192. },
  193. body: JSON.stringify(newOrder)
  194. })
  195. .then(response => response.json())
  196. .then((response)=>{
  197. if(typeof(response) === "string"){
  198. banner.createError(response);
  199. }else{
  200. banner.createNotification("New order created");
  201. updateOrders(newOrder);
  202. }
  203. })
  204. .catch((err)=>{
  205. console.log(err);
  206. banner.createError("Something went wrong. Try refreshing the page");
  207. });
  208. }
  209. }
  210. }