From d1cadf77c814571961f8ad83d97d0ccc8d9af930 Mon Sep 17 00:00:00 2001 From: JohanGrims Date: Tue, 10 Dec 2024 23:25:02 +0100 Subject: [PATCH] Refactor Vote component to use useEffect for navigation logic and improve code readability --- src/Vote.jsx | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Vote.jsx b/src/Vote.jsx index 797c948..ae2eac5 100644 --- a/src/Vote.jsx +++ b/src/Vote.jsx @@ -53,28 +53,30 @@ export default function Vote() { const preview = urlParams.get("preview"); - if (localStorage.getItem(id) && !urlParams.get("preview")) { - if (urlParams.get("allowResubmission")) { - navigate(`/x/${id}?allowResubmission=true`); - return; + React.useEffect(() => { + if (localStorage.getItem(id) && !urlParams.get("preview")) { + if (urlParams.get("allowResubmission")) { + navigate(`/x/${id}?allowResubmission=true`); + return; + } + navigate(`/x/${id}`); } - navigate(`/x/${id}`); - } - if ((active === false || Date.now() > endTime.seconds * 1000) && !preview) { - snackbar({ message: "Die Wahl ist bereits beendet." }); - navigate(`/r/${id}`); - } - if (Date.now() < startTime.seconds * 1000 && !preview) { - snackbar({ - message: - "Die Wahl startet erst am " + - moment - .tz(startTime.seconds * 1000, "Europe/Berlin") - .format("dddd, D. MMMM YYYY, HH:mm"), - }); - navigate("/"); - } + if ((active === false || Date.now() > endTime.seconds * 1000) && !preview) { + snackbar({ message: "Die Wahl ist bereits beendet." }); + navigate(`/r/${id}`); + } + if (Date.now() < startTime.seconds * 1000 && !preview) { + snackbar({ + message: + "Die Wahl startet erst am " + + moment + .tz(startTime.seconds * 1000, "Europe/Berlin") + .format("dddd, D. MMMM YYYY, HH:mm"), + }); + navigate("/"); + } + }, [active, endTime, id, navigate, preview, startTime, urlParams]); const submitDisabled = () => {