@@ -39,32 +39,65 @@ const getElectionDate = (election) => {
39
39
40
40
const Records = ( props ) => {
41
41
//NOTE: currentPage is not zero-indexed
42
- const { openModal, currentPage, tableSize, applyTextHover, removeTextHover, openConfirmation} = props ;
42
+ const { openModal, currentPage, tableSize, applyTextHover, removeTextHover, openConfirmation, updateLists } = props ;
43
43
const startIndex = ( currentPage - 1 ) * tableSize ;
44
44
const endIndex = currentPage * tableSize ; //NOTE: endIndex is exclusive, not inclusive
45
45
const visibleWindow = props . filteredList . slice ( startIndex , endIndex ) ;
46
46
const dataIDTextStyle = Styles . TABLE . DATA_REQUEST_TEXT ;
47
47
const recordTextStyle = Styles . TABLE . RECORD_TEXT ;
48
48
49
- const createActionButton = ( electionInfo , index ) => {
49
+ const cancelElectionHandler = async ( electionData , darId , index ) => {
50
+ const targetElection = electionData . election ;
51
+ const electionId = targetElection . electionId ;
52
+ const electionPayload = Object . assign ( { } , electionData . election , { status : 'Canceled' , finalAccessVote : 'false' } ) ;
53
+
54
+ try {
55
+ const updatedElection = await Election . updateElection ( electionId , electionPayload ) ;
56
+ const notification = 'Election has been cancelled' ;
57
+ updateLists ( updatedElection , darId , index , notification ) ;
58
+ } catch ( error ) {
59
+ Notifications . showError ( { text : 'Error: Failed to cancel selected Election' } ) ;
60
+ }
61
+ } ;
62
+
63
+ const createActionButtons = ( electionInfo , index ) => {
50
64
const name = "cell-button hover-color" ;
51
65
const e = electionInfo . election ;
52
66
const dar = electionInfo . dar ;
67
+ const currentUserId = Storage . getCurrentUser ( ) . dacUserId ;
68
+
53
69
if ( ! isNil ( e ) ) {
54
70
switch ( e . status ) {
55
- case "Open" :
56
- return button ( {
57
- className : name ,
58
- onClick : ( ) => props . history . push ( `access_review/${ dar . referenceId } ` )
59
- } , [ "Vote" ] ) ;
71
+ case 'Open' :
72
+ const votes = filter ( { type : 'DAC' , dacUserId : currentUserId } ) ( electionInfo . votes ) ;
73
+ const isFinal = ! isEmpty ( votes . find ( ( voteData ) => ! isNil ( voteData . vote ) ) ) ;
74
+ const vote = head ( votes ) ;
75
+ return [
76
+ button ( {
77
+ key : `vote-button-${ e . referenceId } ` ,
78
+ className : `${ name } vote-button` ,
79
+ style : { flex : 1 } ,
80
+ onClick : ( ) => props . history . push ( `access_review/${ dar . referenceId } /${ vote . voteId } ` )
81
+ } , [ `${ isFinal ? 'Final' : 'Vote' } ` ] ) ,
82
+ button ( {
83
+ key : `cancel-button-${ e . referenceId } ` ,
84
+ className : `${ name } cancel-button` ,
85
+ style : { flex : 1 } ,
86
+ onClick : ( e ) => cancelElectionHandler ( electionInfo , dar . referenceId , index )
87
+ } , [ 'Cancel' ] )
88
+ ] ;
89
+ case 'Final' :
90
+ return [ '- -' ] ;
60
91
default :
61
92
return button ( {
93
+ key : `reopen-button-${ e . referenceId } ` ,
62
94
className : name ,
63
95
onClick : ( ) => openConfirmation ( dar , index )
64
96
} , [ "Re-Open" ] ) ;
65
97
}
66
98
}
67
99
return button ( {
100
+ key : `open-button-${ e . referenceId } ` ,
68
101
className : name ,
69
102
onClick : ( ) => openConfirmation ( dar , index )
70
103
} , [ "Open Election" ] ) ;
@@ -84,12 +117,12 @@ const Records = (props) => {
84
117
div ( { style : Object . assign ( { } , Styles . TABLE . SUBMISSION_DATE_CELL , recordTextStyle ) } , [ getElectionDate ( election ) ] ) ,
85
118
div ( { style : Object . assign ( { } , Styles . TABLE . DAC_CELL , recordTextStyle ) } , [ dac ? dac . name : '- -' ] ) ,
86
119
div ( { style : Object . assign ( { } , Styles . TABLE . ELECTION_STATUS_CELL , recordTextStyle ) } , [ election ? election . status : '- -' ] ) ,
87
- div ( { style : Object . assign ( { } , Styles . TABLE . ELECTION_ACTIONS_CELL , recordTextStyle ) } , [ createActionButton ( electionInfo , index ) ] ) ,
120
+ div ( { style : Object . assign ( { } , Styles . TABLE . ELECTION_ACTIONS_CELL , recordTextStyle ) } , [ createActionButtons ( electionInfo , index ) ] ) ,
88
121
] ) ;
89
122
} ) ;
90
123
} ;
91
124
92
- const NewChairConsole = ( props ) => {
125
+ export default function NewChairConsole ( props ) {
93
126
const [ showModal , setShowModal ] = useState ( false ) ;
94
127
const [ electionList , setElectionList ] = useState ( [ ] ) ;
95
128
const [ filteredList , setFilteredList ] = useState ( [ ] ) ;
@@ -152,6 +185,18 @@ const NewChairConsole = (props) => {
152
185
}
153
186
} ;
154
187
188
+ const updateLists = ( updatedElection , darId , index , successText ) => {
189
+ const i = index + ( ( currentPage - 1 ) * tableSize ) ;
190
+ let filteredListCopy = cloneDeep ( filteredList ) ;
191
+ let electionListCopy = cloneDeep ( electionList ) ;
192
+ filteredListCopy [ parseInt ( i , 10 ) ] . election = updatedElection ;
193
+ setFilteredList ( filteredListCopy ) ;
194
+ const row = electionListCopy . find ( ( element ) => element . dar . referenceId === darId ) ;
195
+ row . election = Object . assign ( { } , row . election , updatedElection ) ;
196
+ setElectionList ( electionListCopy ) ;
197
+ Notifications . showSuccess ( { text : successText } ) ;
198
+ } ;
199
+
155
200
const handleSearchChange = ( ) => {
156
201
setCurrentPage ( 1 ) ;
157
202
const searchTermValues = toLower ( searchTerms . current . value ) . split ( / \s | , / ) ;
@@ -188,31 +233,20 @@ const NewChairConsole = (props) => {
188
233
}
189
234
} ;
190
235
191
- const createElection = ( darId , index ) => {
236
+ const createElection = async ( darId , index ) => {
192
237
if ( ! isNil ( darId ) ) {
193
- let copy ;
194
- const i = index + ( ( currentPage - 1 ) * tableSize ) ;
195
- Election . createDARElection ( darId )
196
- . then ( ( electionResponse ) => electionResponse . json ( ) )
197
- . then ( ( newElection ) => {
198
- Notifications . showSuccess ( { text : "Election successfully opened" } ) ;
199
- copy = cloneDeep ( filteredList ) ;
200
- copy [ parseInt ( i , 10 ) ] . election = newElection ;
201
- setFilteredList ( copy ) ;
202
- const row = electionList . find ( ( element ) => element . dar . referenceId === darId ) ;
203
- row . election = Object . assign ( { } , row . election , { status : "Open" , finalAccessVote : false } ) ;
204
- } )
205
- . catch ( ( errorResponse ) => {
206
- if ( errorResponse . status === 500 ) {
207
- Notifications . showError ( { text : "Email Service Error! The election was created but the participants couldnt be notified by Email." } ) ;
208
- } else {
209
- errorResponse . json ( ) . then ( ( error ) =>
210
- Notifications . showError ( { text : "Election cannot be created! " + error . message } ) ) ;
211
- }
212
- } ) ;
213
- setShowConfirmation ( false ) ;
214
- } else {
215
- Notifications . showError ( { text : "Could not open election. Contact us for support." } ) ;
238
+ try {
239
+ const updatedElection = await Election . createDARElection ( darId ) ;
240
+ const successMsg = 'Election successfully opened' ;
241
+ updateLists ( updatedElection , darId , index , successMsg ) ;
242
+ setShowConfirmation ( false ) ;
243
+ } catch ( error ) {
244
+ const errorReturn = { text : 'Error: Failed to create election!' } ;
245
+ if ( error . status === 500 ) {
246
+ errorReturn . text = "Email Service Error! The election was created but the participants couldnt be notified by Email." ;
247
+ }
248
+ Notifications . showError ( errorReturn ) ;
249
+ }
216
250
}
217
251
} ;
218
252
@@ -261,7 +295,7 @@ const NewChairConsole = (props) => {
261
295
div ( { style : Styles . TABLE . ELECTION_STATUS_CELL } , [ "Election status" ] ) ,
262
296
div ( { style : Styles . TABLE . ELECTION_ACTIONS_CELL } , [ "Election actions" ] )
263
297
] ) ,
264
- h ( Records , { isRendered : ! isEmpty ( filteredList ) , filteredList, openModal, currentPage, tableSize, applyTextHover, removeTextHover, history : props . history , openConfirmation} )
298
+ h ( Records , { isRendered : ! isEmpty ( filteredList ) , filteredList, openModal, currentPage, tableSize, applyTextHover, removeTextHover, history : props . history , openConfirmation, updateLists } )
265
299
] ) ,
266
300
h ( PaginationBar , { pageCount, currentPage, tableSize, goToPage, changeTableSize, Styles, applyTextHover, removeTextHover} ) ,
267
301
h ( DarModal , { showModal, closeModal, darDetails, researcher} ) ,
@@ -278,5 +312,3 @@ const NewChairConsole = (props) => {
278
312
] )
279
313
) ;
280
314
} ;
281
-
282
- export default NewChairConsole ;
0 commit comments