adapt the time context

This commit is contained in:
Sven Czarnian
2022-11-05 00:31:16 +01:00
parent e1ced4e9f8
commit cdadef9837

View File

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