bookings.js 4.1 KB

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