utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. async function fetchWithToken(url, options) {
  3. const authToken = localStorage.getItem('token');
  4. return await fetch(url, {...options, headers: {
  5. 'Authorization': `Bearer ${authToken}`,
  6. 'Content-Type': 'application/json',
  7. }});
  8. }
  9. // Function to format time as HH:MM
  10. function formatTime(date) {
  11. const hours = date.getUTCHours().toString().padStart(2, '0');
  12. const minutes = date.getUTCMinutes().toString().padStart(2, '0');
  13. return `${hours}:${minutes}`;
  14. }
  15. // Function to get the time span (e.g., 09:00 to 10:00)
  16. function getTimeSpan(start, end) {
  17. return `${formatTime(new Date(start))} to ${formatTime(new Date(end))}`;
  18. }
  19. async function parseOrRedirect(response) {
  20. if (response.status === 401) {
  21. window.location.replace("/html/auth.html");
  22. return;
  23. }
  24. if (response.status === 410) {
  25. window.location.replace("/html/410.html");
  26. return;
  27. }
  28. const js = await response.json();
  29. if (!response.ok) {
  30. window.location.replace("/html/404.html");
  31. }
  32. return js;
  33. }
  34. export { fetchWithToken, getTimeSpan, parseOrRedirect };