From 14100714d55b9e6b5f636295540614e122ce6af5 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Fri, 1 Nov 2019 08:29:57 +0200 Subject: Cleanup fetchImages() and URL param parsing --- src/Geshem.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Geshem.js b/src/Geshem.js index 1163ea8..3c816e6 100644 --- a/src/Geshem.js +++ b/src/Geshem.js @@ -22,16 +22,18 @@ function App() { function Geshem(props) { const [images, setImages] = useState([]); const [playback] = useState( - props.match.params.date || window.location.search.slice(-8) || false + props.match.params.date || + new URL(window.location).searchParams.get("history") || + false ); const [slider, setSlider] = useState(playback ? 143 : 9); useEffect(() => { - const fetchImages = async () => { - const res = await fetch(`${IMAGES_BASE_URL}/imgs.json`); - const imgs = await res.json(); - setImages(imgs["280"]); - }; + const fetchImages = async () => + fetch(`${IMAGES_BASE_URL}/imgs.json`) + .then(res => res.json()) + .then(imgs => imgs["280"]) + .then(setImages); const buildPlayback = async () => { const date = playback; @@ -54,11 +56,11 @@ function Geshem(props) { else { fetchImages(); timer = setInterval(fetchImages, 60 * 1000); - }; + } return () => { if (timer !== undefined) clearInterval(timer); - } + }; }, [playback]); return ( -- cgit v1.3.1