creator-appointments.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Appointment management script.
  3. * Handles loading, creating, and editing appointments.
  4. */
  5. "use strict";
  6. import {fetchRegular, fetchWithToken, parseOrRedirect} from "./utils.js";
  7. /**
  8. * DOM elements for appointment form and error/success messages.
  9. */
  10. const appointmentErrorParagraph = document.getElementById("appointment-error");
  11. const appointmentSuccessParagraph = document.getElementById("appointment-success");
  12. const titleInput = document.getElementById('title');
  13. const descriptionInput = document.getElementById('description');
  14. const dueDateInput = document.getElementById('dueDate');
  15. const startDateInput = document.getElementById('startDate');
  16. const endDateInput = document.getElementById('endDate');
  17. const placeInput = document.getElementById('place');
  18. /**
  19. * Event listener for window load event.
  20. * Checks if an appointment ID is provided in the URL query string.
  21. * If an appointment ID is found, loads the appointment data and updates the form.
  22. */
  23. window.addEventListener("load", async function () {
  24. const urlParams = new URLSearchParams(window.location.search);
  25. const appointment = urlParams.get('appointment');
  26. if (appointment) {
  27. // Load appointment data
  28. await loadAppointment(appointment);
  29. document.getElementById("appointment-btn").innerText = "Edit Appointment";
  30. document.getElementById("form-title").innerText = "Edit Appointment";
  31. }
  32. /**
  33. * Event listener for appointment button click event.
  34. * Creates or edits an appointment based on the presence of an appointment ID.
  35. */
  36. document.getElementById("appointment-btn").addEventListener('click', async () => {
  37. if (!appointment) {
  38. await createAppointment();
  39. } else {
  40. // Modify appointment
  41. await editAppointment(appointment);
  42. }
  43. })
  44. });
  45. /**
  46. * Displays appointment data in the form.
  47. * @param {Object} data - Appointment data object.
  48. */
  49. async function displayAppointment(data) {
  50. titleInput.value = data.title;
  51. descriptionInput.value = data.description;
  52. dueDateInput.value = new Date(data.dueDate).toISOString().split('T')[0];
  53. startDateInput.value = new Date(data.startDate).toISOString().slice(0, 16);
  54. endDateInput.value = new Date(data.endDate).toISOString().slice(0, 16);
  55. placeInput.value = data.place;
  56. }
  57. /**
  58. * Loads appointment data from the server.
  59. * @param {string} appointment - Appointment ID.
  60. */
  61. async function loadAppointment(appointment) {
  62. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  63. console.log(window.location.host);
  64. const searchParams = new URLSearchParams({
  65. appointmentId: appointment,
  66. isUser: true
  67. });
  68. const response = await fetchRegular("/api/schedule?" + searchParams.toString(), options)
  69. .catch(err => console.error('error:' + err));
  70. const js = await parseOrRedirect(response);
  71. await displayAppointment(js);
  72. }
  73. /**
  74. * Retrieves form data as an object.
  75. * @returns {Object} Form data object.
  76. */
  77. function getParameters() {
  78. const title = titleInput.value;
  79. const description = descriptionInput.value;
  80. const dueDate = dueDateInput.value;
  81. const startDate = startDateInput.value;
  82. const endDate = endDateInput.value;
  83. const place = placeInput.value;
  84. return {title, description, dueDate, startDate, endDate, place};
  85. }
  86. /**
  87. * Edits an existing appointment.
  88. * @param {string} appointment - Appointment ID.
  89. */
  90. async function editAppointment(appointment) {
  91. await setAppointment("modify", appointment);
  92. }
  93. /**
  94. * Creates a new appointment.
  95. */
  96. async function createAppointment() {
  97. await setAppointment("create", "");
  98. }
  99. /**
  100. * Sets an appointment (create or modify).
  101. * @param {string} endpoint - API endpoint (create or modify).
  102. * @param {string} appointment - Appointment ID (optional).
  103. */
  104. async function setAppointment(endpoint, appointment) {
  105. const {title, description, dueDate, startDate, endDate, place} = getParameters();
  106. if (!title || !description || !dueDate || !startDate || !endDate) {
  107. appointmentSuccessParagraph.innerText = "";
  108. appointmentErrorParagraph.innerText = "Not all fields filled";
  109. return;
  110. }
  111. const response = await fetchWithToken(`/api/users/${endpoint}`, {
  112. method: 'POST',
  113. body: JSON.stringify({
  114. title: title,
  115. description: description,
  116. dueDate: dueDate,
  117. startDate: startDate,
  118. endDate: endDate,
  119. place: place,
  120. appointmentId: appointment
  121. })
  122. });
  123. const js = await response.json();
  124. if (!response.ok) {
  125. appointmentSuccessParagraph.innerText = "";
  126. appointmentErrorParagraph.innerText = js["message"];
  127. } else {
  128. const searchParams = new URLSearchParams({
  129. appointment: js["id"],
  130. });
  131. const path = '/html/schedule.html?' + searchParams.toString();
  132. appointmentErrorParagraph.innerText = "";
  133. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  134. "was copied to your clipboard";
  135. if (endpoint === "modify") {
  136. appointmentSuccessParagraph.innerText = "Appointment edited successfully. The share link " +
  137. "was copied to your clipboard";
  138. }
  139. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  140. }
  141. }