utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. 'Cache-Control': 'no-cache, no-store',
  8. }});
  9. }
  10. async function fetchRegular(url, options) {
  11. return await fetch(url, {...options, headers: {
  12. 'Content-Type': 'application/json',
  13. 'Cache-Control': 'no-cache, no-store',
  14. }});
  15. }
  16. // Function to format time as HH:MM
  17. function formatTime(date) {
  18. const hours = date.getUTCHours().toString().padStart(2, '0');
  19. const minutes = date.getUTCMinutes().toString().padStart(2, '0');
  20. return `${hours}:${minutes}`;
  21. }
  22. // Function to get the time span (e.g., 09:00 to 10:00)
  23. function getTimeSpan(start, end) {
  24. return `${formatTime(new Date(start))} to ${formatTime(new Date(end))}`;
  25. }
  26. async function parseOrRedirect(response) {
  27. if (response.status === 401) {
  28. window.location.replace("/html/auth.html");
  29. return;
  30. }
  31. if (response.status === 410) {
  32. window.location.replace("/html/410.html");
  33. return;
  34. }
  35. const js = await response.json();
  36. if (!response.ok) {
  37. window.location.replace("/html/404.html");
  38. }
  39. return js;
  40. }
  41. export { fetchWithToken, getTimeSpan, parseOrRedirect, fetchRegular };