adapt the time context
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user