newOrder.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. module.exports = {
  2. isPopulated: false,
  3. unused: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. let categories = merchant.categorizeIngredients();
  7. let categoriesList = document.querySelector("#newOrderCategories");
  8. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  9. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  10. for(let i = 0; i < categories.length; i++){
  11. let category = template.cloneNode(true);
  12. category.children[0].children[0].innerText = categories[i].name;
  13. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  14. category.children[0].children[1].children[1].style.display = "none";
  15. category.children[1].style.display = "none";
  16. categoriesList.appendChild(category);
  17. for(let j = 0; j < categories[i].ingredients.length; j++){
  18. let ingredientDiv = ingredientTemplate.cloneNode(true);
  19. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  20. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  21. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  22. this.unused.push(categories[i].ingredients[j]);
  23. category.children[1].appendChild(ingredientDiv);
  24. }
  25. }
  26. this.isPopulated = true;
  27. }
  28. openSidebar(document.querySelector("#newOrder"));
  29. },
  30. addOne: function(ingredientDiv, container){
  31. for(let i = 0; i < this.unused.length; i++){
  32. if(this.unused[i] === ingredientDiv){
  33. this.unused.splice(i, 1);
  34. break;
  35. }
  36. }
  37. let quantityInput = document.createElement("input");
  38. quantityInput.type = "number";
  39. quantityInput.placeholder = `QUANTITY (${ingredientDiv.ingredient.unit})`;
  40. quantityInput.min = "0";
  41. quantityInput.step = "0.01";
  42. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  43. let priceInput = document.createElement("input");
  44. priceInput.type = "number";
  45. priceInput.placeholder = "Price Per Unit";
  46. priceInput.min = "0";
  47. priceInput.step = "0.01";
  48. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  49. ingredientDiv.children[4].innerText = "-";
  50. ingredientDiv.children[4].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  51. container.removeChild(ingredientDiv);
  52. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  53. },
  54. removeOne: function(ingredientDiv, container){
  55. this.unused.push(ingredientDiv.ingredient);
  56. ingredientDiv.removeChild(ingredientDiv.children[1]);
  57. ingredientDiv.removeChild(ingredientDiv.children[1]);
  58. ingredientDiv.children[1].innerText = "+";
  59. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  60. ingredientDiv.parentElement.removeChild(ingredientDiv);
  61. container.appendChild(ingredientDiv);
  62. },
  63. submit: function(){
  64. let categoriesList = document.getElementById("newOrderAdded");
  65. let ingredients = [];
  66. for(let i = 0; i < categoriesList.children.length; i++){
  67. let quantity = categoriesList.children[i].children[1].value;
  68. let price = categoriesList.children[i].children[2].value;
  69. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  70. if(quantity !== "" && price !== ""){
  71. ingredients.push({
  72. ingredient: categoriesList.children[i].ingredient.id,
  73. quantity: convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  74. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  75. });
  76. }
  77. }
  78. let date = `${document.getElementById("orderDate").value}T${document.getElementById("orderTime").value}:00`
  79. let data = {
  80. name: document.getElementById("orderName").value,
  81. date: date,
  82. ingredients: ingredients
  83. };
  84. let loader = document.getElementById("loaderContainer");
  85. loader.style.display = "flex";
  86. fetch("/order/create", {
  87. method: "POST",
  88. headers: {
  89. "Content-Type": "application/json;charset=utf-8"
  90. },
  91. body: JSON.stringify(data)
  92. })
  93. .then(response => response.json())
  94. .then((response)=>{
  95. if(typeof(response) === "string"){
  96. banner.createError(response);
  97. }else{
  98. let order = new Order(
  99. response._id,
  100. response.name,
  101. response.date,
  102. response.ingredients,
  103. merchant
  104. )
  105. merchant.editOrders([order]);
  106. merchant.editIngredients(order.ingredients, false, true);
  107. banner.createNotification("ORDER CREATED");
  108. }
  109. })
  110. .catch((err)=>{
  111. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  112. })
  113. .finally(()=>{
  114. loader.style.display = "none";
  115. });
  116. },
  117. }