From 5900a4d2d2c65d931e2cfc2d990c13ee403fac21 Mon Sep 17 00:00:00 2001 From: Renu Date: Fri, 24 Mar 2023 22:29:36 +0530 Subject: [PATCH] Fix issue 113 --- admin-ui/pages/elections/[id]/open-test.tsx | 231 +++++++++++++------- lib/Election.js | 10 + 2 files changed, 165 insertions(+), 76 deletions(-) diff --git a/admin-ui/pages/elections/[id]/open-test.tsx b/admin-ui/pages/elections/[id]/open-test.tsx index 0488e7e..2da12c1 100644 --- a/admin-ui/pages/elections/[id]/open-test.tsx +++ b/admin-ui/pages/elections/[id]/open-test.tsx @@ -1,4 +1,4 @@ -import { Button, Grid, Slider, SliderThumb, Typography } from "@mui/material"; +import { Alert, Button, Grid, Slider, SliderThumb, Typography } from "@mui/material"; import LoggedInLayout from "layout/LoggedInLayout"; import type { NextPage } from "next"; import { useRouter } from "next/router"; @@ -15,8 +15,9 @@ import { import GC from "component/GC"; import GI from "component/GI"; import Loading from "component/Loading"; -import ElectionCard from "component/ElectionCard"; import { useNavigate } from "react-router-dom"; +import useCurrentElection from "hooks/useCurrentElection"; +import LoadingButton from "component/LoadingButton"; interface ThumbProps { children: ReactNode; @@ -40,13 +41,18 @@ const TestElection: NextPage = () => { const { id } = query; const electionId = Array.isArray(id) ? id[0] : id; - + const [currentElection, reloadCurrentElection, loadingCurrentElection] = useCurrentElection(); const [election, setElection] = useState>(null); + + const shouldShowSetCurrentBtn = !loadingCurrentElection && currentElection && currentElection?.electionId != electionId; + const [alertText, setAlertText] = useState(""); + const [maliciousRequest, setMaliciousRequest] = useState(false); const loadElection = async () => { if (electionId) { const resp = await getElection(electionId); setElection(resp); + } }; @@ -56,13 +62,38 @@ const TestElection: NextPage = () => { } }, [electionId]); + useEffect(() => { + const modesNotAllowedForTest = [ElectionStatus.archived, ElectionStatus.closed, ElectionStatus.open]; + if (election && modesNotAllowedForTest.includes(election.electionStatus)) { + // setAlertText(`${resp?.electionName} is ${resp.electionStatus} and so cannot be set to test mode.`) + setMaliciousRequest(true); + } + }, [election]); + const testElection = async () => { if (electionId) { - await setCurrentElection(electionId); - await openElectionTest(electionId); - router.push("/dashboard"); + try { + await openElectionTest(electionId); + router.push("/dashboard"); + } catch (e: any) { + console.error(e); + setAlertText(e?.data?.error_description); + } + // await setCurrentElection(electionId); + } + // loadElection(); + }; + + const runSetCurrentElection = async () => { + if (electionId) { + try { + await setCurrentElection(electionId); + reloadCurrentElection(); + } catch (e: any) { + console.error(e); + setAlertText(`${currentElection?.electionJurisdictionName} ${currentElection?.electionName} ${e?.data?.error_description}`); + } } - loadElection(); }; const navigate = useNavigate(); @@ -71,83 +102,131 @@ const TestElection: NextPage = () => { navigate(-1); }; - return ( - - {!election && } - {election && - election.latMode == - 1 /*election?.electionStatus === ElectionStatus.test */ && ( - + const InTestMode = () => { + return ( + + + You are now in Testing Mode! + + + You are now in testing mode! Please test your election with your + team, then come back to finish editing and launch your election! + + + - You are now in Testing Mode! + - - You are now in testing mode! Please test your election with your - team, then come back to finish editing and launch your election! - - - - - - - - - + - )} - {election && !election?.latMode && ( - // This should be latMode 0; but some elections don't have that? - //== 0 /*election?.electionStatus !== ElectionStatus.test */ && ( - <> - Please confirm to continue. - - Please confirm that you would like to enter testing mode for{" "} - {election?.electionName}. - - - + + + ) + } + + const NeedCurrentMode = () => { + return ( + + + Prerequisites not met for test mode! + + + Election for {election?.electionJurisdictionName} {election?.electionName} is not the current election and so cannot be set to test mode. + + + + - - -   - - - { - if (newValue === 100) { - testElection(); - } - }} - components={{ - Thumb: ThumbComponent, - }} - step={null} - marks={[ - { - value: 0, - label: "", - }, - { - value: 100, - label: "", - }, - ]} - defaultValue={0} - /> - + + {!alertText ? + + + Set Current + + + : false + } + + + + ) + } + + const SetTestMode = () => { + return ( + <> + Please confirm to continue. + + Please confirm that you would like to enter testing mode for{" "} + {election?.electionName}. + + + + + + +   + + { + if (newValue === 100) { + testElection(); + } + }} + components={{ + Thumb: ThumbComponent, + }} + step={null} + marks={[ + { + value: 0, + label: "", + }, + { + value: 100, + label: "", + }, + ]} + defaultValue={0} + /> + + + + ) + } + + return ( + + {(!election || loadingCurrentElection) && } + {maliciousRequest ? + {election?.electionName} is {election?.electionStatus} and so cannot be set to test mode. + : + <> + {!loadingCurrentElection && election && + election.latMode == 1 /*election?.electionStatus === ElectionStatus.test */ && + + } + {!loadingCurrentElection && election && !election?.latMode && ( + // This should be latMode 0; but some elections don't have that? + //== 0 /*election?.electionStatus !== ElectionStatus.test */ && ( + shouldShowSetCurrentBtn ? : + )} + + {alertText && {alertText}} + - )} + } ); }; diff --git a/lib/Election.js b/lib/Election.js index 5edbb2c..42da46c 100644 --- a/lib/Election.js +++ b/lib/Election.js @@ -1433,6 +1433,16 @@ class Election { case apiEndpoint.setCurrentElection: return Election.testConditions(endpoint, election, [ [liveCurrentElection, "Current election is in a live state"], + [ + electionAttributes?.["electionStatus"] == + Election.electionStatus.archived, + "Election has been archived.", + ], + [ + electionAttributes?.["electionStatus"] == + Election.electionStatus.closed, + "Election has been closed", + ] ]); case apiEndpoint.openElectionTest: