creator-appointments.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. const appointmentErrorParagraph = document.getElementById("appointment-error");
  3. const appointmentSuccessParagraph = document.getElementById("appointment-success");
  4. const titleInput = document.getElementById('title');
  5. const descriptionInput = document.getElementById('description');
  6. const dueDateInput = document.getElementById('dueDate');
  7. const startDateInput = document.getElementById('startDate');
  8. const endDateInput = document.getElementById('endDate');
  9. const placeInput = document.getElementById('place');
  10. window.addEventListener("load", async function() {
  11. const urlParams = new URLSearchParams(window.location.search);
  12. appointment = urlParams.get('appointment');
  13. document.getElementById("appointment-btn").addEventListener('click', async () => {
  14. if (!appointment) {
  15. await createAppointment();
  16. } else {
  17. // modify appointment
  18. }
  19. })
  20. console.log(titleInput.value);
  21. });
  22. async function fetchWithToken(url, options) {
  23. const authToken = localStorage.getItem('token');
  24. return await fetch(url, {...options, headers: {
  25. 'Authorization': `Bearer ${authToken}`,
  26. 'Content-Type': 'application/json',
  27. }});
  28. }
  29. async function createAppointment() {
  30. const title = titleInput.value;
  31. const description = descriptionInput.value;
  32. const dueDate = dueDateInput.value;
  33. const startDate = startDateInput.value;
  34. const endDate = endDateInput.value;
  35. const place = placeInput.value;
  36. if (!title || !description || !dueDate || !startDate || !endDate) {
  37. appointmentSuccessParagraph.innerText = "";
  38. appointmentErrorParagraph.innerText = "Not fields filled";
  39. return;
  40. }
  41. const response = await fetchWithToken('/api/users/create', {
  42. method: 'POST',
  43. body: JSON.stringify({
  44. title: title,
  45. description: description,
  46. dueDate: dueDate,
  47. startDate: startDate,
  48. endDate: endDate,
  49. place: place
  50. })
  51. });
  52. const js = await response.json();
  53. if (!response.ok) {
  54. appointmentSuccessParagraph.innerText = "";
  55. appointmentErrorParagraph.innerText = js["message"];
  56. } else {
  57. const path = `/html/schedule.html?appointment=${js["id"]}`;
  58. appointmentErrorParagraph.innerText = "";
  59. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  60. "was copied to your clipboard";
  61. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  62. }
  63. }