creator-appointments.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. import {fetchRegular, 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. console.log(window.location.host);
  41. const searchParams = new URLSearchParams({
  42. appointmentId: appointment,
  43. isUser: true
  44. });
  45. const response = await fetchRegular("/api/schedule?" + searchParams.toString(), options)
  46. .catch(err => console.error('error:' + err));
  47. const js = await parseOrRedirect(response);
  48. await displayAppointment(js);
  49. }
  50. function getParameters() {
  51. const title = titleInput.value;
  52. const description = descriptionInput.value;
  53. const dueDate = dueDateInput.value;
  54. const startDate = startDateInput.value;
  55. const endDate = endDateInput.value;
  56. const place = placeInput.value;
  57. return {title, description, dueDate, startDate, endDate, place};
  58. }
  59. async function editAppointment(appointment) {
  60. await setAppointment("modify", appointment);
  61. }
  62. async function createAppointment() {
  63. await setAppointment("create", "");
  64. }
  65. async function setAppointment(endpoint, appointment) {
  66. const {title, description, dueDate, startDate, endDate, place} = getParameters();
  67. if (!title || !description || !dueDate || !startDate || !endDate) {
  68. appointmentSuccessParagraph.innerText = "";
  69. appointmentErrorParagraph.innerText = "Not fields filled";
  70. return;
  71. }
  72. const response = await fetchWithToken(`/api/users/${endpoint}`, {
  73. method: 'POST',
  74. body: JSON.stringify({
  75. title: title,
  76. description: description,
  77. dueDate: dueDate,
  78. startDate: startDate,
  79. endDate: endDate,
  80. place: place,
  81. appointmentId: appointment
  82. })
  83. });
  84. const js = await response.json();
  85. if (!response.ok) {
  86. appointmentSuccessParagraph.innerText = "";
  87. appointmentErrorParagraph.innerText = js["message"];
  88. } else {
  89. const searchParams = new URLSearchParams({
  90. appointment: js["id"],
  91. });
  92. const path = '/html/schedule.html?' + searchParams.toString();
  93. appointmentErrorParagraph.innerText = "";
  94. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  95. "was copied to your clipboard";
  96. if (endpoint === "modify") {
  97. appointmentSuccessParagraph.innerText = "Appointment edited successfully. The share link " +
  98. "was copied to your clipboard";
  99. }
  100. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  101. }
  102. }