orders.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 tbody = document.querySelector("#orderList tbody");
  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]._id;
  25. row.children[1].innerText = response[i].ingredients.length;
  26. row.children[2].innerText = response[i].date;
  27. row.children[3].innerText = totalCost;
  28. }
  29. }
  30. })
  31. .catch((err)=>{
  32. banner.createError("Unable to retrieve your orders at the moment");
  33. });
  34. }
  35. }
  36. }