orders.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. window.ordersStrandObj = {
  2. isPopulated: false,
  3. display: async function(){
  4. if(!this.isPopulated){
  5. window.orders = [];
  6. fetch("/order", {
  7. method: "GET",
  8. headers: {
  9. "Content-Type": "application/json;charset=utf-8"
  10. },
  11. })
  12. .then((response) => response.json())
  13. .then((response)=>{
  14. if(typeof(response) === "string"){
  15. banner.createError(response);
  16. }else{
  17. let newOrders = [];
  18. for(let i = 0; i < response.length; i++){
  19. newOrders.push(new Order(
  20. response[i].name,
  21. new Date(response.date),
  22. response.ingredients,
  23. merchant
  24. ));
  25. }
  26. let listDiv = document.querySelector("#orderList");
  27. let template = document.querySelector("#order").content.children[0];
  28. while(listDiv.children.length > 0){
  29. listDiv.removeChild(listDiv.firstChild);
  30. }
  31. for(let i = 0; i < merchant.orders.length; i++){
  32. let row = template.cloneNode(true);
  33. let totalCost = 0;
  34. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  35. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  36. }
  37. row.children[0].innerText = merchant.orders[i].name;
  38. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  39. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  40. row.children[3].innerText = (totalCost / 100).toFixed(2);
  41. row.order = merchant.orders[i];
  42. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  43. window.orders.push(row);
  44. listDiv.appendChild(row);
  45. }
  46. }
  47. })
  48. .catch((err)=>{
  49. banner.createError("Unable to retrieve your orders at the moment");
  50. });
  51. this.isPopulated = true;
  52. }
  53. }
  54. }