newOrder.js 5.5 KB

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