bookings.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. window.addEventListener("load", async function () {
  3. const urlParams = new URLSearchParams(window.location.search);
  4. const appointment = urlParams.get('appointment');
  5. if (!appointment) {
  6. window.location.replace("/html/404.html");
  7. }
  8. await getAppointment(appointment);
  9. await fetchBookings(appointment);
  10. });
  11. async function fetchWithToken(url, options) {
  12. const authToken = localStorage.getItem('token');
  13. return await fetch(url, {...options, headers: {
  14. 'Authorization': `Bearer ${authToken}`,
  15. 'Content-Type': 'application/json',
  16. }});
  17. }
  18. function displayAppointmentDetails(appointmentData) {
  19. // Retrieve the container
  20. const appointmentInfo = document.querySelector('.appointment-info');
  21. // Clear existing content
  22. while (appointmentInfo.firstChild) {
  23. appointmentInfo.removeChild(appointmentInfo.firstChild);
  24. }
  25. // Create and append the updated appointment content
  26. const title = document.createElement('p');
  27. const titleLabel = document.createElement('strong');
  28. titleLabel.textContent = 'Title: ';
  29. title.appendChild(titleLabel);
  30. title.appendChild(document.createTextNode(appointmentData.title));
  31. const description = document.createElement('p');
  32. const descriptionLabel = document.createElement('strong');
  33. descriptionLabel.textContent = 'Description: ';
  34. description.appendChild(descriptionLabel);
  35. description.appendChild(document.createTextNode(appointmentData.description));
  36. const place = document.createElement('p');
  37. const placeLabel = document.createElement('strong');
  38. placeLabel.textContent = 'Place: ';
  39. place.appendChild(placeLabel);
  40. place.appendChild(document.createTextNode(appointmentData.place));
  41. const dueDate = document.createElement('p');
  42. const dueDateLabel = document.createElement('strong');
  43. dueDateLabel.textContent = 'Due Date: ';
  44. dueDate.appendChild(dueDateLabel);
  45. dueDate.appendChild(document.createTextNode(new Date(appointmentData.dueDate).toLocaleDateString()));
  46. const timeSpan = document.createElement('p');
  47. const timeSpanLabel = document.createElement('strong');
  48. timeSpanLabel.textContent = 'Time Span: ';
  49. timeSpan.appendChild(timeSpanLabel);
  50. timeSpan.appendChild(document.createTextNode(getTimeSpan(appointmentData.startDate, appointmentData.endDate)));
  51. // Append all the updated elements
  52. appointmentInfo.appendChild(title);
  53. appointmentInfo.appendChild(description);
  54. appointmentInfo.appendChild(place);
  55. appointmentInfo.appendChild(dueDate);
  56. appointmentInfo.appendChild(timeSpan);
  57. }
  58. // Helper function to format the time span
  59. function getTimeSpan(startDate, endDate) {
  60. const start = new Date(startDate).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  61. const end = new Date(endDate).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  62. return `${start} to ${end}`;
  63. }
  64. async function getAppointment(appointment) {
  65. const options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  66. const response = await fetch(`/api/schedule?isUser=true&appointmentId=${appointment}`, options)
  67. .catch(err => console.error('error:' + err));
  68. if (response.status === 404) {
  69. window.location.replace("/html/404.html");
  70. }
  71. const js = await response.json();
  72. displayAppointmentDetails(js);
  73. }
  74. function renderBookings(data) {
  75. const tableBody = document.getElementById('participants-table-body');
  76. tableBody.innerHTML = ''; // Clear existing rows
  77. data.forEach(participant => {
  78. // Create a new table row
  79. const row = document.createElement('tr');
  80. // Create table cells for first name, last name, and email
  81. const firstNameCell = document.createElement('td');
  82. const lastNameCell = document.createElement('td');
  83. const emailCell = document.createElement('td');
  84. // Populate cell content with participant data
  85. firstNameCell.textContent = participant.firstname;
  86. lastNameCell.textContent = participant.lastname;
  87. emailCell.textContent = participant.email;
  88. // Append cells to the row
  89. row.appendChild(firstNameCell);
  90. row.appendChild(lastNameCell);
  91. row.appendChild(emailCell);
  92. // Append the row to the table body
  93. tableBody.appendChild(row);
  94. });
  95. }
  96. async function fetchBookings(appointmentId) {
  97. const response = await fetchWithToken(`/api/users/bookings?appointmentId=${appointmentId}`);
  98. if (response.status === 401) {
  99. window.location.replace("/html/auth.html");
  100. return;
  101. }
  102. const js = await response.json();
  103. if (!response.ok) {
  104. window.location.replace("/html/404.html");
  105. }
  106. renderBookings(js);
  107. }