orders.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. window.ordersStrandObj = {
  2. isFetched: false,
  3. display: async function(){
  4. if(!this.isFetched){
  5. window.orders = [];
  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("Unable to retrieve your orders at the moment");
  35. })
  36. .finally(()=>{
  37. loader.style.display = "none";
  38. });
  39. }
  40. },
  41. populate: function(){
  42. let listDiv = document.querySelector("#orderList");
  43. let template = document.querySelector("#order").content.children[0];
  44. while(listDiv.children.length > 0){
  45. listDiv.removeChild(listDiv.firstChild);
  46. }
  47. for(let i = 0; i < merchant.orders.length; i++){
  48. let row = template.cloneNode(true);
  49. let totalCost = 0;
  50. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  51. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  52. }
  53. row.children[0].innerText = merchant.orders[i].name;
  54. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  55. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  56. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  57. row.order = merchant.orders[i];
  58. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  59. window.orders.push(row);
  60. listDiv.appendChild(row);
  61. }
  62. }
  63. }