schedule.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. import {fetchRegular, getTimeSpan, parseOrRedirect} from "./utils.js";
  3. let appointment;
  4. const errorParagraph = document.getElementById('appointment-error');
  5. const successParagraph = document.getElementById('appointment-success');
  6. window.addEventListener("load", async function () {
  7. document.getElementById("btn-attend").addEventListener("click", async () => {
  8. await attendEvent();
  9. })
  10. const urlParams = new URLSearchParams(window.location.search);
  11. appointment = urlParams.get('appointment');
  12. if (!appointment) {
  13. window.location.replace("/html/404.html");
  14. }
  15. await getAppointment();
  16. });
  17. async function getAppointment() {
  18. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  19. const searchParams = new URLSearchParams({
  20. appointmentId: appointment,
  21. });
  22. const response = await fetchRegular('/api/schedule?' + searchParams.toString(), options)
  23. .catch(err => console.error('error:' + err));
  24. const js = await parseOrRedirect(response);
  25. displayAppointmentDetails(js);
  26. }
  27. function displayAppointmentDetails(appointment) {
  28. // Update the innerHTML of the elements to display the appointment details
  29. document.getElementById('appointment-date').innerText = appointment._id;
  30. document.getElementById('title').innerText = appointment.title;
  31. document.getElementById('description').innerText = appointment.description;
  32. // Format the dates to a more user-friendly format
  33. const dueDate = new Date(appointment.dueDate).toLocaleDateString();
  34. const startDate = new Date(appointment.startDate);
  35. const endDate = new Date(appointment.endDate);
  36. const startDateFormatted = startDate.toLocaleString();
  37. const endDateFormatted = endDate.toLocaleString();
  38. console.log(dueDate);
  39. console.log(startDate);
  40. console.log(endDate);
  41. document.getElementById('due-date').value = dueDate;
  42. document.getElementById('appointment-date').value = startDateFormatted;
  43. document.getElementById('time-span').value = getTimeSpan(startDate, endDate);
  44. // Set place
  45. document.getElementById('place').innerText = appointment.place;
  46. }
  47. async function attendEvent() {
  48. const email = document.getElementById('email').value;
  49. const firstname = document.getElementById('firstname').value;
  50. const lastname = document.getElementById('lastname').value;
  51. if (!email || !firstname || !lastname) {
  52. errorParagraph.innerText = "Fill in all fields.";
  53. return;
  54. }
  55. const response = await fetchRegular("/api/schedule/create",
  56. {
  57. method: 'POST',
  58. headers: {'Content-Type': 'application/json'},
  59. body: JSON.stringify({
  60. appointment: appointment,
  61. email: email,
  62. firstname: firstname,
  63. lastname: lastname
  64. })
  65. });
  66. const js = await response.json();
  67. if (response.status !== 200) {
  68. errorParagraph.innerText = js["message"];
  69. successParagraph.innerText = "";
  70. } else {
  71. errorParagraph.innerText = "";
  72. successParagraph.innerText = "Registered successfully.";
  73. }
  74. }