The tests now update live

This commit is contained in:
Mathias Wagner
2022-05-08 03:10:37 +02:00
parent 60296d3c75
commit 18847de7d6
2 changed files with 16 additions and 6 deletions
+8 -4
View File
@@ -12,7 +12,7 @@ function LatestTestComponent() {
const [setDialog] = useContext(DialogContext);
const config = useContext(ConfigContext);
useEffect(() => {
function updateTest() {
let passwordHeaders = localStorage.getItem("password") ? {password: localStorage.getItem("password")} : {}
fetch("/api/speedtests/latest", {headers: passwordHeaders})
.then(res => res.json())
@@ -20,13 +20,17 @@ function LatestTestComponent() {
setLatest(latest);
setLatestTestTime(generateRelativeTime(latest.created));
});
}
useEffect(() => {
const interval = setInterval(() => updateTest(), 15000);
updateTest();
return () => clearInterval(interval);
}, [setLatest]);
useEffect(() => {
const interval = setInterval(() => setLatestTestTime(generateRelativeTime(latest.created)), 1000);
return () => {
clearInterval(interval);
};
return () => clearInterval(interval);
}, [setLatestTestTime, latest]);
if (Object.entries(config).length === 0) return (<></>)
+8 -2
View File
@@ -7,11 +7,17 @@ function TestArea() {
const config = useContext(ConfigContext);
const [tests, setTests] = useState([]);
useEffect(() => {
function updateTests() {
let passwordHeaders = localStorage.getItem("password") ? {password: localStorage.getItem("password")} : {}
fetch("/api/speedtests", {headers: passwordHeaders})
.then(res => res.json())
.then(tests => setTests(tests));
.then(tests => setTests(tests))
}
useEffect(() => {
const interval = setInterval(() => updateTests(), 15000);
updateTests();
return () => clearInterval(interval);
}, [setTests]);
if (Object.entries(config).length === 0) return (<></>)