newOrder.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. let newOrder = {
  2. isPopulated: false,
  3. unused: [],
  4. display: function(Order){
  5. if(!this.isPopulated){
  6. let categories = merchant.categorizeIngredients();
  7. let categoriesList = document.getElementById("newOrderCategories");
  8. let template = document.getElementById("addIngredientsCategory").content.children[0];
  9. let ingredientTemplate = document.getElementById("newOrderIngredient").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 = ()=>{this.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].children[0].innerText = categories[i].ingredients[j].ingredient.name;
  20. ingredientDiv.children[0].children[1].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. document.getElementById("submitNewOrder").onclick = ()=>{this.submit(Order)};
  27. this.isPopulated = true;
  28. }
  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. ingredientDiv.children[0].children[1].innerText = "-";
  38. ingredientDiv.children[0].children[1].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  39. ingredientDiv.children[1].style.display = "flex";
  40. container.removeChild(ingredientDiv);
  41. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  42. },
  43. removeOne: function(ingredientDiv, container){
  44. this.unused.push(ingredientDiv.ingredient);
  45. ingredientDiv.children[1].style.display = "none";
  46. ingredientDiv.children[0].children[1].innerText = "+";
  47. ingredientDiv.children[0].children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  48. ingredientDiv.parentElement.removeChild(ingredientDiv);
  49. container.appendChild(ingredientDiv);
  50. },
  51. toggleAddIngredient: function(categoryElement){
  52. let button = categoryElement.children[0].children[1];
  53. let ingredientDisplay = categoryElement.children[1];
  54. if(ingredientDisplay.style.display === "none"){
  55. ingredientDisplay.style.display = "flex";
  56. button.children[0].style.display = "none";
  57. button.children[1].style.display = "block";
  58. }else{
  59. ingredientDisplay.style.display = "none";
  60. button.children[0].style.display = "block";
  61. button.children[1].style.display = "none";
  62. }
  63. },
  64. submit: function(Order){
  65. let categoriesList = document.getElementById("newOrderAdded");
  66. let ingredients = [];
  67. for(let i = 0; i < categoriesList.children.length; i++){
  68. let quantity = categoriesList.children[i].children[1].children[0].value;
  69. let price = categoriesList.children[i].children[1].children[1].value;
  70. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  71. if(quantity !== "" && price !== ""){
  72. ingredients.push({
  73. ingredient: categoriesList.children[i].ingredient.id,
  74. quantity: controller.convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  75. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  76. });
  77. }
  78. }
  79. let time = document.getElementById("orderTime").value;
  80. let date = document.getElementById("orderDate").value;
  81. let dateTime = "";
  82. if(time === "" && date === ""){
  83. dateTime = undefined;
  84. }else if(time === "" && date !== ""){
  85. dateTime = date;
  86. }else if(time !== "" && date === ""){
  87. banner.createError("PLEASE ADD A DATE IF YOU WISH TO HAVE A TIME");
  88. }else{
  89. dateTime = `${date}T${time}:00`
  90. }
  91. let data = {
  92. name: document.getElementById("orderName").value,
  93. date: dateTime,
  94. ingredients: ingredients
  95. };
  96. let loader = document.getElementById("loaderContainer");
  97. loader.style.display = "flex";
  98. fetch("/order/create", {
  99. method: "POST",
  100. headers: {
  101. "Content-Type": "application/json;charset=utf-8"
  102. },
  103. body: JSON.stringify(data)
  104. })
  105. .then(response => response.json())
  106. .then((response)=>{
  107. if(typeof(response) === "string"){
  108. banner.createError(response);
  109. }else{
  110. let order = new Order(
  111. response._id,
  112. response.name,
  113. response.date,
  114. response.ingredients,
  115. merchant
  116. )
  117. merchant.editOrders([order]);
  118. merchant.editIngredients(order.ingredients, false, true);
  119. banner.createNotification("ORDER CREATED");
  120. }
  121. })
  122. .catch((err)=>{
  123. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  124. })
  125. .finally(()=>{
  126. loader.style.display = "none";
  127. });
  128. },
  129. }
  130. module.exports = newOrder;