orders.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. window.ordersStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. fetch("/orders", {
  6. method: "GET",
  7. headers: {
  8. "Content-Type": "application/json;charset=utf-8"
  9. },
  10. })
  11. .then((response) => response.json())
  12. .then((response)=>{
  13. if(typeof(response) === "string"){
  14. banner.createError(response);
  15. }else{
  16. let listDiv = document.querySelector("#orderList");
  17. let template = document.querySelector("#order").content.children[0];
  18. for(let i = 0; i < response.length; i++){
  19. let row = template.cloneNode(true);
  20. let totalCost = 0;
  21. for(let j = 0; j < response[i].ingredients; j++){
  22. totalCost += response[i].ingredients[j].quantity * response[i].ingredients[j].price;
  23. }
  24. row.children[0].innerText = response[i].orderId;
  25. row.children[1].innerText = `${response[i].ingredients.length} items`;
  26. row.children[2].innerText = new Date(response[i].date).toLocaleDateString("en-US");
  27. row.children[3].innerText = totalCost;
  28. listDiv.appendChild(row);
  29. }
  30. }
  31. })
  32. .catch((err)=>{
  33. banner.createError("Unable to retrieve your orders at the moment");
  34. });
  35. this.isPopulated = true;
  36. }
  37. }
  38. }