creator-appointments.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. import {fetchWithToken, parseOrRedirect} from "./utils.js";
  3. const appointmentErrorParagraph = document.getElementById("appointment-error");
  4. const appointmentSuccessParagraph = document.getElementById("appointment-success");
  5. const titleInput = document.getElementById('title');
  6. const descriptionInput = document.getElementById('description');
  7. const dueDateInput = document.getElementById('dueDate');
  8. const startDateInput = document.getElementById('startDate');
  9. const endDateInput = document.getElementById('endDate');
  10. const placeInput = document.getElementById('place');
  11. window.addEventListener("load", async function() {
  12. const urlParams = new URLSearchParams(window.location.search);
  13. const appointment = urlParams.get('appointment');
  14. if (appointment) {
  15. // load appointment data
  16. await loadAppointment(appointment);
  17. document.getElementById("appointment-btn").innerText = "Edit Appointment";
  18. document.getElementById("form-title").innerText = "Edit Appointment";
  19. }
  20. document.getElementById("appointment-btn").addEventListener('click', async () => {
  21. if (!appointment) {
  22. await createAppointment();
  23. } else {
  24. // modify appointment
  25. // TODO Add a modify appointment function
  26. await editAppointment(appointment);
  27. }
  28. })
  29. });
  30. async function displayAppointment(data) {
  31. titleInput.value = data.title;
  32. descriptionInput.value = data.description;
  33. dueDateInput.value = new Date(data.dueDate).toISOString().split('T')[0];
  34. startDateInput.value = new Date(data.startDate).toISOString().slice(0, 16);
  35. endDateInput.value = new Date(data.endDate).toISOString().slice(0, 16);
  36. placeInput.value = data.place;
  37. }
  38. async function loadAppointment(appointment) {
  39. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  40. const response = await fetch(`/api/schedule?appointmentId=${appointment}&isUser=true`, options)
  41. .catch(err => console.error('error:' + err));
  42. const js = await parseOrRedirect(response);
  43. await displayAppointment(js);
  44. }
  45. function getParameters() {
  46. const title = titleInput.value;
  47. const description = descriptionInput.value;
  48. const dueDate = dueDateInput.value;
  49. const startDate = startDateInput.value;
  50. const endDate = endDateInput.value;
  51. const place = placeInput.value;
  52. return {title, description, dueDate, startDate, endDate, place};
  53. }
  54. async function editAppointment(appointment) {
  55. await setAppointment("modify", appointment);
  56. }
  57. async function createAppointment() {
  58. await setAppointment("create", "");
  59. }
  60. async function setAppointment(endpoint, appointment) {
  61. const { title, description, dueDate, startDate, endDate, place } = getParameters();
  62. if (!title || !description || !dueDate || !startDate || !endDate) {
  63. appointmentSuccessParagraph.innerText = "";
  64. appointmentErrorParagraph.innerText = "Not fields filled";
  65. return;
  66. }
  67. const response = await fetchWithToken(`/api/users/${endpoint}`, {
  68. method: 'POST',
  69. body: JSON.stringify({
  70. title: title,
  71. description: description,
  72. dueDate: dueDate,
  73. startDate: startDate,
  74. endDate: endDate,
  75. place: place,
  76. appointmentId: appointment
  77. })
  78. });
  79. const js = await response.json();
  80. if (!response.ok) {
  81. appointmentSuccessParagraph.innerText = "";
  82. appointmentErrorParagraph.innerText = js["message"];
  83. } else {
  84. const path = `/html/schedule.html?appointment=${js["id"]}`;
  85. appointmentErrorParagraph.innerText = "";
  86. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  87. "was copied to your clipboard";
  88. if (endpoint === "modify") {
  89. appointmentSuccessParagraph.innerText = "Appointment edited successfully. The share link " +
  90. "was copied to your clipboard";
  91. }
  92. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  93. }
  94. }