orders.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const { quantity } = require("../../controllers/validator");
  2. window.ordersStrandObj = {
  3. isFetched: false,
  4. display: async function(){
  5. if(!this.isFetched){
  6. let loader = document.getElementById("loaderContainer");
  7. loader.style.display = "flex";
  8. fetch("/order", {
  9. method: "GET",
  10. headers: {
  11. "Content-Type": "application/json;charset=utf-8"
  12. },
  13. })
  14. .then((response) => response.json())
  15. .then((response)=>{
  16. if(typeof(response) === "string"){
  17. banner.createError(response);
  18. }else{
  19. let newOrders = [];
  20. for(let i = 0; i < response.length; i++){
  21. newOrders.push(new Order(
  22. response[i]._id,
  23. response[i].name,
  24. response[i].date,
  25. response[i].ingredients,
  26. merchant
  27. ));
  28. }
  29. merchant.editOrders(newOrders);
  30. this.isFetched = true;
  31. }
  32. })
  33. .catch((err)=>{
  34. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  35. })
  36. .finally(()=>{
  37. loader.style.display = "none";
  38. });
  39. }
  40. },
  41. populate: function(){
  42. let listDiv = document.getElementById("orderList");
  43. let template = document.getElementById("order").content.children[0];
  44. let dateDropdown = document.getElementById("dateDropdownOrder");
  45. let ingredientDropdown = document.getElementById("ingredientDropdown");
  46. dateDropdown.style.display = "none";
  47. ingredientDropdown.style.display = "none";
  48. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  49. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  50. for(let i = 0; i < merchant.ingredients.length; i++){
  51. let checkbox = document.createElement("input");
  52. checkbox.type = "checkbox";
  53. checkbox.ingredient = merchant.ingredients[i].ingredient;
  54. ingredientDropdown.appendChild(checkbox);
  55. let label = document.createElement("label");
  56. label.innerText = merchant.ingredients[i].ingredient.name;
  57. label.for = checkbox;
  58. ingredientDropdown.appendChild(label);
  59. let brk = document.createElement("br");
  60. ingredientDropdown.appendChild(brk);
  61. }
  62. while(listDiv.children.length > 0){
  63. listDiv.removeChild(listDiv.firstChild);
  64. }
  65. for(let i = 0; i < merchant.orders.length; i++){
  66. let row = template.cloneNode(true);
  67. let totalCost = 0;
  68. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  69. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  70. }
  71. row.children[0].innerText = merchant.orders[i].name;
  72. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  73. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  74. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  75. row.order = merchant.orders[i];
  76. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  77. listDiv.appendChild(row);
  78. }
  79. },
  80. submitFilter: function(){
  81. event.preventDefault();
  82. let data = {
  83. startDate: document.getElementById("orderFilDate1").valueAsDate,
  84. endDate: document.getElementById("orderFilDate2").valueAsDate,
  85. ingredients: []
  86. }
  87. if(data.startDate >= data.endDate){
  88. banner.createError("START DATE CANNOT BE AFTER END DATE");
  89. return;
  90. }
  91. let ingredientChoices = document.getElementById("ingredientDropdown");
  92. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  93. if(ingredientChoices.children[i].checked){
  94. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  95. }
  96. }
  97. let loader = document.getElementById("loarderContainer");
  98. loader.style.display = "flex";
  99. fetch("/order", {
  100. method: "POST",
  101. headers: {
  102. "Content-Type": "application/json;charset=utf-8"
  103. },
  104. body: JSON.stringify(data)
  105. })
  106. .then((response) => response.json())
  107. .then((response)=>{
  108. if(typeof(response) === "string"){
  109. banner.createError(response);
  110. }else{
  111. let orderList = document.getElementById("orderList");
  112. let template = document.getElementById("order").content.children[0];
  113. while(orderList.children.length > 0){
  114. orderList.removeChild(orderList.firstChild);
  115. }
  116. for(let i = 0; i < response.length; i++){
  117. let order = template.cloneNode(true);
  118. let cost = 0;
  119. for(let j = 0; j < response[i].ingredients.length; j++){
  120. cost += (response[i].ingredients[j].price / 100) * quantity;
  121. }
  122. order.children[0].innerText = response[i].name,
  123. order.children[1].innerText = response[i].ingredients.length;
  124. order.children[2].innerText = response[i].date.toLocaleDateString();
  125. order.children[3].innerText = cost;
  126. orderList.appendChild(order);
  127. }
  128. }
  129. })
  130. .catch((err)=>{
  131. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  132. })
  133. .finally(()=>{
  134. loader.style.display = "none";
  135. });
  136. },
  137. toggleDropdown: function(dropdown){
  138. event.preventDefault();
  139. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  140. if(dropdown.style.display === "none"){
  141. dropdown.style.display = "block";
  142. polyline.setAttribute("points", "18 15 12 9 6 15");
  143. }else{
  144. dropdown.style.display = "none";
  145. polyline.setAttribute("points", "6 9 12 15 18 9");
  146. }
  147. }
  148. }