Browse Source

adapt the time context

Sven Czarnian 2 years ago
parent
commit
cdadef9837
1 changed files with 11 additions and 11 deletions
  1. 11 11
      src/contexts/timecontext.tsx

+ 11 - 11
src/contexts/timecontext.tsx

@@ -1,6 +1,6 @@
 import React, { Dispatch, SetStateAction, useEffect, useState } from 'react';
-import { useNavigate } from 'react-router-dom';
 import { System } from '../services';
+import { BackendReturnStatus } from '../types';
 
 const TimeContext = React.createContext<{
   offset: number;
@@ -9,19 +9,19 @@ const TimeContext = React.createContext<{
 
 export const TimeProvider = ({ children }: { children: any }) => {
   const [offset, setOffset] = useState<number>(0);
-  const navigate = useNavigate();
 
   useEffect(() => {
     const estimateTimeOffset = async () => {
-      System.timestamp().then((timestamp) => {
-        // calculate the time offset (not accurate) between the server and the client to show "correct" times
-        const clientTimeUtc = new Date().getTime()
-        const serverTimeUtc = timestamp;
-        setOffset(serverTimeUtc - clientTimeUtc);
-      }).catch(() => {
-        setOffset(0);
-        navigate('/');
-      });
+      System.timestamp().then((response) => {
+        if (response.status === BackendReturnStatus.Unauthorized) {
+          // TODO relogin
+        } else if (response.status === BackendReturnStatus.Ok) {
+          // calculate the time offset (not accurate) between the server and the client to show "correct" times
+          const clientTimeUtc = new Date().getTime()
+          const serverTimeUtc = response.timestamp;
+          setOffset(serverTimeUtc - clientTimeUtc);
+        }
+      })
     }
 
     estimateTimeOffset();