1- import { Button , Grid , Slider , SliderThumb , Typography } from "@mui/material" ;
1+ import { Alert , Button , Grid , Slider , SliderThumb , Typography } from "@mui/material" ;
22import LoggedInLayout from "layout/LoggedInLayout" ;
33import type { NextPage } from "next" ;
44import { useRouter } from "next/router" ;
@@ -15,8 +15,9 @@ import {
1515import GC from "component/GC" ;
1616import GI from "component/GI" ;
1717import Loading from "component/Loading" ;
18- import ElectionCard from "component/ElectionCard" ;
1918import { useNavigate } from "react-router-dom" ;
19+ import useCurrentElection from "hooks/useCurrentElection" ;
20+ import LoadingButton from "component/LoadingButton" ;
2021
2122interface ThumbProps {
2223 children : ReactNode ;
@@ -40,13 +41,18 @@ const TestElection: NextPage = () => {
4041 const { id } = query ;
4142
4243 const electionId = Array . isArray ( id ) ? id [ 0 ] : id ;
43-
44+ const [ currentElection , reloadCurrentElection , loadingCurrentElection ] = useCurrentElection ( ) ;
4445 const [ election , setElection ] = useState < Maybe < Election > > ( null ) ;
46+
47+ const shouldShowSetCurrentBtn = ! loadingCurrentElection && currentElection && currentElection ?. electionId != electionId ;
48+ const [ alertText , setAlertText ] = useState < string > ( "" ) ;
49+ const [ maliciousRequest , setMaliciousRequest ] = useState < boolean > ( false ) ;
4550
4651 const loadElection = async ( ) => {
4752 if ( electionId ) {
4853 const resp = await getElection ( electionId ) ;
4954 setElection ( resp ) ;
55+
5056 }
5157 } ;
5258
@@ -56,13 +62,37 @@ const TestElection: NextPage = () => {
5662 }
5763 } , [ electionId ] ) ;
5864
65+ useEffect ( ( ) => {
66+ if ( election && [ ElectionStatus . archived , ElectionStatus . closed , ElectionStatus . open ] . includes ( election . electionStatus ) ) {
67+ // setAlertText(`${resp?.electionName} is ${resp.electionStatus} and so cannot be set to test mode.`)
68+ setMaliciousRequest ( true ) ;
69+ }
70+ } , [ election ] ) ;
71+
5972 const testElection = async ( ) => {
6073 if ( electionId ) {
61- await setCurrentElection ( electionId ) ;
62- await openElectionTest ( electionId ) ;
63- router . push ( "/dashboard" ) ;
74+ try {
75+ await openElectionTest ( electionId ) ;
76+ router . push ( "/dashboard" ) ;
77+ } catch ( e : any ) {
78+ console . error ( e ) ;
79+ setAlertText ( e ?. data ?. error_description ) ;
80+ }
81+ // await setCurrentElection(electionId);
82+ }
83+ // loadElection();
84+ } ;
85+
86+ const runSetCurrentElection = async ( ) => {
87+ if ( electionId ) {
88+ try {
89+ await setCurrentElection ( electionId ) ;
90+ reloadCurrentElection ( ) ;
91+ } catch ( e : any ) {
92+ console . error ( e ) ;
93+ setAlertText ( `${ currentElection ?. electionJurisdictionName } ${ currentElection ?. electionName } ${ e ?. data ?. error_description } ` ) ;
94+ }
6495 }
65- loadElection ( ) ;
6696 } ;
6797
6898 const navigate = useNavigate ( ) ;
@@ -71,83 +101,131 @@ const TestElection: NextPage = () => {
71101 navigate ( - 1 ) ;
72102 } ;
73103
74- return (
75- < LoggedInLayout title = "Test Election" >
76- { ! election && < Loading /> }
77- { election &&
78- election . latMode ==
79- 1 /*election?.electionStatus === ElectionStatus.test */ && (
80- < GC direction = "column" spacing = { 2 } >
104+ const InTestMode = ( ) => {
105+ return (
106+ < GC direction = "column" spacing = { 2 } >
107+ < GI >
108+ < Typography variant = "h2" > You are now in Testing Mode!</ Typography >
109+ </ GI >
110+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
111+ You are now in testing mode! Please test your election with your
112+ team, then come back to finish editing and launch your election!
113+ </ Typography >
114+ < GI >
115+ < GC justifyContent = "space-between" >
81116 < GI >
82- < Typography variant = "h2" > You are now in Testing Mode!</ Typography >
117+ < Button
118+ onClick = { ( ) =>
119+ router . push ( `/elections/${ election . electionId } /test` )
120+ }
121+ >
122+ View Test Status
123+ </ Button >
83124 </ GI >
84- < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
85- You are now in testing mode! Please test your election with your
86- team, then come back to finish editing and launch your election!
87- </ Typography >
88125 < GI >
89- < GC justifyContent = "space-between" >
90- < GI >
91- < Button
92- onClick = { ( ) =>
93- router . push ( `/elections/${ election . electionId } /test` )
94- }
95- >
96- View Test Status
97- </ Button >
98- </ GI >
99- < GI >
100- < Button onClick = { ( ) => router . push ( "/dashboard" ) } >
101- Go to Dashboard
102- </ Button >
103- </ GI >
104- </ GC >
126+ < Button onClick = { ( ) => router . push ( "/dashboard" ) } >
127+ Go to Dashboard
128+ </ Button >
105129 </ GI >
106130 </ GC >
107- ) }
108- { election && ! election ?. latMode && (
109- // This should be latMode 0; but some elections don't have that?
110- //== 0 /*election?.electionStatus !== ElectionStatus.test */ && (
111- < >
112- < Typography variant = "h2" > Please confirm to continue.</ Typography >
113- < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
114- Please confirm that you would like to enter testing mode for{ " " }
115- { election ?. electionName } .
116- </ Typography >
117- < Grid container spacing = { 4 } >
118- < Grid item xs = { 3 } >
131+ </ GI >
132+ </ GC >
133+ )
134+ }
135+
136+ const NeedCurrentMode = ( ) => {
137+ return (
138+ < GC direction = "column" spacing = { 2 } >
139+ < GI >
140+ < Typography variant = "h2" > Prerequisites not met for test mode!</ Typography >
141+ </ GI >
142+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
143+ Election for { election ?. electionJurisdictionName } { election ?. electionName } is not the current election and so cannot be set to test mode.
144+ </ Typography >
145+ < GI >
146+ < GC justifyContent = "space-between" >
147+ < GI item xs = { 3 } >
119148 < Button onClick = { goBack } > Back</ Button >
120- </ Grid >
121- < Grid item xs = { 2 } >
122-
123- </ Grid >
124- < Grid item xs = { 6 } >
125- < Slider
126- onChangeCommitted = { ( _event , newValue ) => {
127- if ( newValue === 100 ) {
128- testElection ( ) ;
129- }
130- } }
131- components = { {
132- Thumb : ThumbComponent ,
133- } }
134- step = { null }
135- marks = { [
136- {
137- value : 0 ,
138- label : "" ,
139- } ,
140- {
141- value : 100 ,
142- label : "" ,
143- } ,
144- ] }
145- defaultValue = { 0 }
146- />
147- </ Grid >
149+ </ GI >
150+ { ! alertText ?
151+ < GI item xs = { 3 } >
152+ < LoadingButton onClick = { runSetCurrentElection } >
153+ Set Current
154+ </ LoadingButton >
155+ </ GI >
156+ : false
157+ }
158+ </ GC >
159+ </ GI >
160+ </ GC >
161+ )
162+ }
163+
164+ const SetTestMode = ( ) => {
165+ return (
166+ < >
167+ < Typography variant = "h2" > Please confirm to continue.</ Typography >
168+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
169+ Please confirm that you would like to enter testing mode for{ " " }
170+ { election ?. electionName } .
171+ </ Typography >
172+ < Grid container spacing = { 4 } >
173+ < Grid item xs = { 3 } >
174+ < Button onClick = { goBack } > Back</ Button >
175+ </ Grid >
176+ < Grid item xs = { 2 } >
177+
148178 </ Grid >
179+ < Grid item xs = { 6 } >
180+ < Slider
181+ onChangeCommitted = { ( _event , newValue ) => {
182+ if ( newValue === 100 ) {
183+ testElection ( ) ;
184+ }
185+ } }
186+ components = { {
187+ Thumb : ThumbComponent ,
188+ } }
189+ step = { null }
190+ marks = { [
191+ {
192+ value : 0 ,
193+ label : "" ,
194+ } ,
195+ {
196+ value : 100 ,
197+ label : "" ,
198+ } ,
199+ ] }
200+ defaultValue = { 0 }
201+ />
202+ </ Grid >
203+ </ Grid >
204+ </ >
205+ )
206+ }
207+
208+ return (
209+ < LoggedInLayout title = "Test Election" >
210+ { ( ! election || loadingCurrentElection ) && < Loading /> }
211+ { maliciousRequest ?
212+ < Alert severity = "error" > { election ?. electionName } is { election ?. electionStatus } and so cannot be set to test mode.</ Alert >
213+ :
214+ < >
215+ { ! loadingCurrentElection && election &&
216+ election . latMode == 1 /*election?.electionStatus === ElectionStatus.test */ &&
217+ < InTestMode />
218+ }
219+ { ! loadingCurrentElection && election && ! election ?. latMode && (
220+ // This should be latMode 0; but some elections don't have that?
221+ //== 0 /*election?.electionStatus !== ElectionStatus.test */ && (
222+ shouldShowSetCurrentBtn ? < NeedCurrentMode /> : < SetTestMode />
223+ ) }
224+ < Grid xs = { 12 } >
225+ { alertText && < Alert severity = "error" > { alertText } </ Alert > }
226+ </ Grid >
149227 </ >
150- ) }
228+ }
151229 </ LoggedInLayout >
152230 ) ;
153231} ;
0 commit comments