diff --git a/src/contexts/timecontext.tsx b/src/contexts/timecontext.tsx index 755d519..1326b03 100644 --- a/src/contexts/timecontext.tsx +++ b/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(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();