From 18847de7d622eb546f25e6cfe7306372286cfd96 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sun, 8 May 2022 03:10:37 +0200 Subject: [PATCH] The tests now update live --- client/src/components/LatestTestComponent.js | 12 ++++++++---- client/src/components/TestAreaComponent.js | 10 ++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/client/src/components/LatestTestComponent.js b/client/src/components/LatestTestComponent.js index c4b948e8..d359a2dd 100644 --- a/client/src/components/LatestTestComponent.js +++ b/client/src/components/LatestTestComponent.js @@ -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 (<>) diff --git a/client/src/components/TestAreaComponent.js b/client/src/components/TestAreaComponent.js index 9ecbb758..a005c8ed 100644 --- a/client/src/components/TestAreaComponent.js +++ b/client/src/components/TestAreaComponent.js @@ -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 (<>)