Skip to content

Commit c0ac6e5

Browse files
authored
DUOS-762[risk=no] Added Cancel Button to New Chair Console (#887)
* DUOS-762 added cancel button to record rows, refactored create function to create shared view update helper function for both create and cancel flows * DUOS-762 added conditional logic for Final Vote button * DUOS-762 updated AdminManageAccess error handler due to createDARElection adjustment * DUOS-762 codacy fix * DUOS-762 corrected defaultValue key on paginationBar, replaced date check for vote check on filter, added key attribute to button elements
1 parent b06a57b commit c0ac6e5

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

src/components/PaginationBar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function PaginationBar(props) {
3535
onChange: (e) => goToPage(toNumber(currentPage.current.value)),
3636
type: 'text',
3737
ref: currentPage,
38-
defaultvalue: props.currentPage,
38+
defaultValue: props.currentPage,
3939
style: Styles.TABLE.PAGINATION_INPUT
4040
}),
4141
span({}, [` of ${pageCount}`])
@@ -56,7 +56,7 @@ export default function PaginationBar(props) {
5656
onChange: (e) => changeTableSize(tableSize.current.value),
5757
type: 'text',
5858
ref: tableSize,
59-
defaultvalue: props.tableSize,
59+
defaultValue: props.tableSize,
6060
style: Styles.TABLE.PAGINATION_INPUT
6161
})
6262
])

src/libs/ajax.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ export const Election = {
574574
const election = { status: 'Open', finalAccessVote: false };
575575
const url = `${await Config.getApiUrl()}/dataRequest/${requestId}/election`;
576576
const res = await fetchOk(url, fp.mergeAll([Config.jsonBody(election), Config.authOpts(), { method: 'POST' }]));
577-
return res;
577+
return res.json();
578578
},
579579

580580
isDataSetElectionOpen: async () => {

src/pages/AdminManageAccess.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ class AdminManageAccess extends Component {
115115
if (errorResponse.status === 500) {
116116
this.setState({ alertTitle: 'Email Service Error!', alertMessage: 'The election was created but the participants couldnt be notified by Email.', disableCancelBtn: false });
117117
} else {
118-
errorResponse.json().then(error =>
119-
this.setState({ alertTitle: 'Election cannot be created!', alertMessage: error.message, disableCancelBtn: false })
120-
);
118+
this.setState({ alertTitle: 'Election cannot be created!', alertMessage: errorResponse.message, disableCancelBtn: false });
121119
}
122120
});
123121
} else {

src/pages/NewChairConsole.js

+68-36
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,65 @@ const getElectionDate = (election) => {
3939

4040
const Records = (props) => {
4141
//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;
4343
const startIndex = (currentPage - 1) * tableSize;
4444
const endIndex = currentPage * tableSize; //NOTE: endIndex is exclusive, not inclusive
4545
const visibleWindow = props.filteredList.slice(startIndex, endIndex);
4646
const dataIDTextStyle = Styles.TABLE.DATA_REQUEST_TEXT;
4747
const recordTextStyle = Styles.TABLE.RECORD_TEXT;
4848

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) => {
5064
const name = "cell-button hover-color";
5165
const e = electionInfo.election;
5266
const dar = electionInfo.dar;
67+
const currentUserId = Storage.getCurrentUser().dacUserId;
68+
5369
if (!isNil(e)) {
5470
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 ['- -'];
6091
default :
6192
return button({
93+
key: `reopen-button-${e.referenceId}`,
6294
className: name,
6395
onClick: () => openConfirmation(dar, index)
6496
}, ["Re-Open"]);
6597
}
6698
}
6799
return button({
100+
key: `open-button-${e.referenceId}`,
68101
className: name,
69102
onClick: () => openConfirmation(dar, index)
70103
}, ["Open Election"]);
@@ -84,12 +117,12 @@ const Records = (props) => {
84117
div({style: Object.assign({}, Styles.TABLE.SUBMISSION_DATE_CELL, recordTextStyle)}, [getElectionDate(election)]),
85118
div({style: Object.assign({}, Styles.TABLE.DAC_CELL, recordTextStyle)}, [dac ? dac.name : '- -']),
86119
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)]),
88121
]);
89122
});
90123
};
91124

92-
const NewChairConsole = (props) => {
125+
export default function NewChairConsole(props) {
93126
const [showModal, setShowModal] = useState(false);
94127
const [electionList, setElectionList] = useState([]);
95128
const [filteredList, setFilteredList] = useState([]);
@@ -152,6 +185,18 @@ const NewChairConsole = (props) => {
152185
}
153186
};
154187

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+
155200
const handleSearchChange = () => {
156201
setCurrentPage(1);
157202
const searchTermValues = toLower(searchTerms.current.value).split(/\s|,/);
@@ -188,31 +233,20 @@ const NewChairConsole = (props) => {
188233
}
189234
};
190235

191-
const createElection = (darId, index) => {
236+
const createElection = async (darId, index) => {
192237
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+
}
216250
}
217251
};
218252

@@ -261,7 +295,7 @@ const NewChairConsole = (props) => {
261295
div({style: Styles.TABLE.ELECTION_STATUS_CELL}, ["Election status"]),
262296
div({style: Styles.TABLE.ELECTION_ACTIONS_CELL}, ["Election actions"])
263297
]),
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})
265299
]),
266300
h(PaginationBar, {pageCount, currentPage, tableSize, goToPage, changeTableSize, Styles, applyTextHover, removeTextHover}),
267301
h(DarModal, {showModal, closeModal, darDetails, researcher}),
@@ -278,5 +312,3 @@ const NewChairConsole = (props) => {
278312
])
279313
);
280314
};
281-
282-
export default NewChairConsole;

0 commit comments

Comments
 (0)