app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function() {
  2. let orders = null;
  3. const appLoadingEle = document.getElementById("app-loading-display");
  4. const orderWrapperEle = document.getElementById("order-display");
  5. const orderEmptyTextEle = document.getElementById("order-empty-text");
  6. const orderTableEle = document.getElementById("order-table");
  7. const orderTableBodyEle = document.querySelector("#order-table tbody");
  8. const addOrderEle = document.getElementById("add-order-wrapper");
  9. const addOrderForm = document.getElementById("add-order-form");
  10. const orderIdField = document.getElementById("order-id");
  11. const productIdField = document.getElementById("product-id");
  12. const quantityField = document.getElementById("quantity");
  13. const amountField = document.getElementById("amount");
  14. const taxField = document.getElementById("tax");
  15. const shippingField = document.getElementById("shippingAmount");
  16. const shippingAddressField = document.getElementById("shippingAddress");
  17. function fetchOrders() {
  18. fetch("http://localhost:8080/orders")
  19. .then(r => r.json())
  20. .then(r => orders = r)
  21. .then(renderOrders)
  22. .catch((e) => {
  23. init();
  24. });
  25. }
  26. function init() {
  27. fetch("http://localhost:8080/init")
  28. .then(() => fetchOrders())
  29. .catch((e) => displayError(e));
  30. }
  31. function renderOrders() {
  32. appLoadingEle.classList.add("d-none");
  33. orderWrapperEle.classList.remove("d-none");
  34. addOrderEle.classList.remove("d-none");
  35. if (orders.length === 0) {
  36. orderEmptyTextEle.classList.remove("d-none");
  37. orderTableEle.classList.add("d-none");
  38. return;
  39. }
  40. orderEmptyTextEle.classList.add("d-none");
  41. orderTableEle.classList.remove("d-none");
  42. while (orderTableBodyEle.firstChild) {
  43. orderTableBodyEle.removeChild(orderTableBodyEle.firstChild);
  44. }
  45. orders.forEach((order) => {
  46. const orderId = order.order_id;
  47. const row = document.createElement("tr");
  48. row.appendChild(createCell(order.order_id));
  49. row.appendChild(createCell(order.product_id));
  50. row.appendChild(createCell(order.quantity));
  51. row.appendChild(createCell(order.amount));
  52. row.appendChild(createCell(order.shipping));
  53. row.appendChild(createCell(order.tax));
  54. row.appendChild(createCell(order.shipping_address));
  55. const actionCell = document.createElement("td");
  56. const deleteButton = document.createElement("button");
  57. deleteButton.classList.add(...["btn","btn-sm","btn-danger"]);
  58. deleteButton.innerText = "Delete";
  59. deleteButton.addEventListener("click", (e) => {
  60. e.preventDefault();
  61. deleteOrder(orderId);
  62. });
  63. actionCell.appendChild(deleteButton);
  64. row.appendChild(actionCell);
  65. orderTableBodyEle.appendChild(row);
  66. });
  67. }
  68. function createCell(contents) {
  69. const cell = document.createElement("td");
  70. cell.innerText = contents;
  71. return cell;
  72. }
  73. function deleteOrder(orderId) {
  74. fetch(`http://localhost:8080/delete_order?id=${orderId}`)
  75. .then(() => fetchOrders());
  76. }
  77. function displayError(err) {
  78. alert("Error:" + err);
  79. }
  80. function onAddFormSubmit(e) {
  81. e.preventDefault();
  82. const data = {
  83. order_id : parseFloat(orderIdField.value),
  84. product_id : parseFloat(productIdField.value),
  85. quantity : parseFloat(quantityField.value),
  86. amount : parseFloat(amountField.value),
  87. shipping : parseFloat(shippingField.value),
  88. tax : parseFloat(taxField.value),
  89. shipping_address : shippingAddressField.value,
  90. };
  91. fetch("http://localhost:8080/create_order", {
  92. method: "POST",
  93. body: JSON.stringify(data),
  94. headers: { "Content-type": "application/json" },
  95. }).then(() => fetchOrders())
  96. .then(() => resetAddOrderForm());
  97. alert("Order added");
  98. }
  99. function resetAddOrderForm() {
  100. orderIdField.value = "";
  101. productIdField.value = "";
  102. quantityField.value = "";
  103. amountField.value = "";
  104. shippingField.value = "";
  105. taxField.value = "";
  106. shippingAddressField.value = "";
  107. }
  108. fetchOrders();
  109. addOrderForm.addEventListener("submit", onAddFormSubmit);
  110. })();