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,38 @@ const TestElection: NextPage = () => {
5662 }
5763 } , [ electionId ] ) ;
5864
65+ useEffect ( ( ) => {
66+ const modesNotAllowedForTest = [ ElectionStatus . archived , ElectionStatus . closed , ElectionStatus . open ] ;
67+ if ( election && modesNotAllowedForTest . includes ( election . electionStatus ) ) {
68+ // setAlertText(`${resp?.electionName} is ${resp.electionStatus} and so cannot be set to test mode.`)
69+ setMaliciousRequest ( true ) ;
70+ }
71+ } , [ election ] ) ;
72+
5973 const testElection = async ( ) => {
6074 if ( electionId ) {
61- await setCurrentElection ( electionId ) ;
62- await openElectionTest ( electionId ) ;
63- router . push ( "/dashboard" ) ;
75+ try {
76+ await openElectionTest ( electionId ) ;
77+ router . push ( "/dashboard" ) ;
78+ } catch ( e : any ) {
79+ console . error ( e ) ;
80+ setAlertText ( e ?. data ?. error_description ) ;
81+ }
82+ // await setCurrentElection(electionId);
83+ }
84+ // loadElection();
85+ } ;
86+
87+ const runSetCurrentElection = async ( ) => {
88+ if ( electionId ) {
89+ try {
90+ await setCurrentElection ( electionId ) ;
91+ reloadCurrentElection ( ) ;
92+ } catch ( e : any ) {
93+ console . error ( e ) ;
94+ setAlertText ( `${ currentElection ?. electionJurisdictionName } ${ currentElection ?. electionName } ${ e ?. data ?. error_description } ` ) ;
95+ }
6496 }
65- loadElection ( ) ;
6697 } ;
6798
6899 const navigate = useNavigate ( ) ;
@@ -71,83 +102,131 @@ const TestElection: NextPage = () => {
71102 navigate ( - 1 ) ;
72103 } ;
73104
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 } >
105+ const InTestMode = ( ) => {
106+ return (
107+ < GC direction = "column" spacing = { 2 } >
108+ < GI >
109+ < Typography variant = "h2" > You are now in Testing Mode!</ Typography >
110+ </ GI >
111+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
112+ You are now in testing mode! Please test your election with your
113+ team, then come back to finish editing and launch your election!
114+ </ Typography >
115+ < GI >
116+ < GC justifyContent = "space-between" >
81117 < GI >
82- < Typography variant = "h2" > You are now in Testing Mode!</ Typography >
118+ < Button
119+ onClick = { ( ) =>
120+ router . push ( `/elections/${ election . electionId } /test` )
121+ }
122+ >
123+ View Test Status
124+ </ Button >
83125 </ 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 >
88126 < 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 >
127+ < Button onClick = { ( ) => router . push ( "/dashboard" ) } >
128+ Go to Dashboard
129+ </ Button >
105130 </ GI >
106131 </ 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 } >
132+ </ GI >
133+ </ GC >
134+ )
135+ }
136+
137+ const NeedCurrentMode = ( ) => {
138+ return (
139+ < GC direction = "column" spacing = { 2 } >
140+ < GI >
141+ < Typography variant = "h2" > Prerequisites not met for test mode!</ Typography >
142+ </ GI >
143+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
144+ Election for { election ?. electionJurisdictionName } { election ?. electionName } is not the current election and so cannot be set to test mode.
145+ </ Typography >
146+ < GI >
147+ < GC justifyContent = "space-between" >
148+ < GI item xs = { 3 } >
119149 < 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 >
150+ </ GI >
151+ { ! alertText ?
152+ < GI item xs = { 3 } >
153+ < LoadingButton onClick = { runSetCurrentElection } >
154+ Set Current
155+ </ LoadingButton >
156+ </ GI >
157+ : false
158+ }
159+ </ GC >
160+ </ GI >
161+ </ GC >
162+ )
163+ }
164+
165+ const SetTestMode = ( ) => {
166+ return (
167+ < >
168+ < Typography variant = "h2" > Please confirm to continue.</ Typography >
169+ < Typography sx = { { fontSize : "3em" , margin : "2em 0" } } >
170+ Please confirm that you would like to enter testing mode for{ " " }
171+ { election ?. electionName } .
172+ </ Typography >
173+ < Grid container spacing = { 4 } >
174+ < Grid item xs = { 3 } >
175+ < Button onClick = { goBack } > Back</ Button >
176+ </ Grid >
177+ < Grid item xs = { 2 } >
178+
148179 </ Grid >
180+ < Grid item xs = { 6 } >
181+ < Slider
182+ onChangeCommitted = { ( _event , newValue ) => {
183+ if ( newValue === 100 ) {
184+ testElection ( ) ;
185+ }
186+ } }
187+ components = { {
188+ Thumb : ThumbComponent ,
189+ } }
190+ step = { null }
191+ marks = { [
192+ {
193+ value : 0 ,
194+ label : "" ,
195+ } ,
196+ {
197+ value : 100 ,
198+ label : "" ,
199+ } ,
200+ ] }
201+ defaultValue = { 0 }
202+ />
203+ </ Grid >
204+ </ Grid >
205+ </ >
206+ )
207+ }
208+
209+ return (
210+ < LoggedInLayout title = "Test Election" >
211+ { ( ! election || loadingCurrentElection ) && < Loading /> }
212+ { maliciousRequest ?
213+ < Alert severity = "error" > { election ?. electionName } is { election ?. electionStatus } and so cannot be set to test mode.</ Alert >
214+ :
215+ < >
216+ { ! loadingCurrentElection && election &&
217+ election . latMode == 1 /*election?.electionStatus === ElectionStatus.test */ &&
218+ < InTestMode />
219+ }
220+ { ! loadingCurrentElection && election && ! election ?. latMode && (
221+ // This should be latMode 0; but some elections don't have that?
222+ //== 0 /*election?.electionStatus !== ElectionStatus.test */ && (
223+ shouldShowSetCurrentBtn ? < NeedCurrentMode /> : < SetTestMode />
224+ ) }
225+ < Grid xs = { 12 } >
226+ { alertText && < Alert severity = "error" > { alertText } </ Alert > }
227+ </ Grid >
149228 </ >
150- ) }
229+ }
151230 </ LoggedInLayout >
152231 ) ;
153232} ;
0 commit comments