schedule.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. /**
  3. * Import required functions from utils.js
  4. */
  5. import { fetchRegular, getTimeSpan, parseOrRedirect } from "./utils.js";
  6. let appointment;
  7. const errorParagraph = document.getElementById('appointment-error');
  8. const successParagraph = document.getElementById('appointment-success');
  9. /**
  10. * Event listener for window load
  11. */
  12. window.addEventListener("load", async function () {
  13. document.getElementById("btn-attend").addEventListener("click", async () => {
  14. await attendEvent();
  15. })
  16. const urlParams = new URLSearchParams(window.location.search);
  17. appointment = urlParams.get('appointment');
  18. if (!appointment) {
  19. window.location.replace("/html/404.html");
  20. }
  21. await getAppointment();
  22. });
  23. /**
  24. * Get appointment details from API
  25. *
  26. * @async
  27. * @returns {Promise<void>}
  28. */
  29. async function getAppointment() {
  30. let options = { method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'} };
  31. const searchParams = new URLSearchParams({
  32. appointmentId: appointment,
  33. });
  34. const response = await fetchRegular('/api/schedule?' + searchParams.toString(), options)
  35. .catch(err => console.error('error:' + err));
  36. const js = await parseOrRedirect(response);
  37. displayAppointmentDetails(js);
  38. }
  39. /**
  40. * Display appointment details on page
  41. *
  42. * @param {object} appointment - Appointment data from API
  43. */
  44. function displayAppointmentDetails(appointment) {
  45. /**
  46. * Update element values with appointment details
  47. */
  48. document.getElementById('appointment-date').innerText = appointment._id;
  49. document.getElementById('title').innerText = appointment.title;
  50. document.getElementById('description').innerText = appointment.description;
  51. const dueDate = new Date(appointment.dueDate).toLocaleDateString();
  52. const startDate = new Date(appointment.startDate);
  53. const endDate = new Date(appointment.endDate);
  54. const startDateFormatted = startDate.toLocaleString();
  55. document.getElementById('due-date').value = dueDate;
  56. document.getElementById('appointment-date').value = startDateFormatted;
  57. document.getElementById('time-span').value = getTimeSpan(startDate, endDate);
  58. document.getElementById('place').innerText = appointment.place;
  59. }
  60. /**
  61. * Attend event by creating a new schedule entry in API
  62. *
  63. * @async
  64. * @returns {Promise<void>}
  65. */
  66. async function attendEvent() {
  67. const email = document.getElementById('email').value;
  68. const firstname = document.getElementById('firstname').value;
  69. const lastname = document.getElementById('lastname').value;
  70. if (!email || !firstname || !lastname) {
  71. errorParagraph.innerText = "Fill in all fields.";
  72. return;
  73. }
  74. const response = await fetchRegular("/api/schedule/create",
  75. {
  76. method: 'POST',
  77. headers: {'Content-Type': 'application/json'},
  78. body: JSON.stringify({
  79. appointment: appointment,
  80. email: email,
  81. firstname: firstname,
  82. lastname: lastname
  83. })
  84. });
  85. const js = await response.json();
  86. if (response.status !== 200) {
  87. errorParagraph.innerText = js["message"];
  88. successParagraph.innerText = "";
  89. } else {
  90. errorParagraph.innerText = "";
  91. successParagraph.innerText = "Registered successfully.";
  92. }
  93. }