Skip to content

Commit

Permalink
Merge pull request #13 from episphere/participant-reset
Browse files Browse the repository at this point in the history
Participant reset
  • Loading branch information
amber-emmes authored Sep 9, 2024
2 parents 9919898 + 842a024 commit 0542295
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/participantDetails.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { dashboardNavBarLinks, removeActiveClass } from './navigationBar.js';
import { attachUpdateLoginMethodListeners, allStates, closeModal, getFieldValues, getImportantRows, getModalLabel, hideUneditableButtons, renderReturnSearchResults, resetChanges, saveResponses, showSaveNoteInModal, submitClickHandler, suffixList, languageList, viewParticipantSummary, } from './participantDetailsHelpers.js';
import { attachUpdateLoginMethodListeners, allStates, closeModal, getFieldValues, getImportantRows, getModalLabel, hideUneditableButtons, renderReturnSearchResults, resetChanges, saveResponses, showSaveNoteInModal, submitClickHandler, resetClickHandlers, suffixList, languageList, viewParticipantSummary, } from './participantDetailsHelpers.js';
import fieldMapping from './fieldToConceptIdMapping.js';
import { renderParticipantHeader } from './participantHeader.js';
import { getDataAttributes } from './utils.js';
import { getDataAttributes, urls } from './utils.js';
import { appState } from './stateManager.js';

appState.setState({unsavedChangesTrack:{saveFlag: false, counter: 0}});
Expand Down Expand Up @@ -38,6 +38,7 @@ export const renderParticipantDetails = (participant, changedOption) => {
hideUneditableButtons(participant, changedOption);
localStorage.setItem("participant", JSON.stringify(participant));
changeParticipantDetail(participant, changedOption, originalHTML);
resetParticipantConfirm();
editAltContact(participant);
viewParticipantSummary(participant);
renderReturnSearchResults();
Expand All @@ -63,6 +64,7 @@ export const render = (participant, changedOption) => {
${renderParticipantHeader(participant)}
${renderBackToSearchDivAndButton()}
${renderCancelChangesAndSaveChangesButtons()}
${renderResetUserButton(participant?.state?.uid)}
${renderDetailsTableHeader()}
`;

Expand Down Expand Up @@ -107,6 +109,30 @@ export const render = (participant, changedOption) => {
return template;
}

const resetParticipantConfirm = () => {
const openResetDialogBtn = document.getElementById('openResetDialog');
if(openResetDialogBtn) {
let data = getDataAttributes(openResetDialogBtn);
openResetDialogBtn.addEventListener('click', () => {
const header = document.getElementById('modalHeader');
const body = document.getElementById('modalBody');
const uid = data.participantuid;
header.innerHTML = `
<h5>Confirm Participant Reset</h5>
<button type="button" class="modal-close-btn" id="closeModal" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>`
body.innerHTML = `<div>
Are you sure you want to reset this participant to a just-verified state? This cannot be undone.
<div style="display:inline-block;">
<button type="submit" class="btn btn-danger" data-dismiss="modal" target="_blank">Cancel</button>
&nbsp;
<button type="button" class="btn btn-primary" id="resetUserBtn">Confirm</button>
</div>
</div>`
resetClickHandlers(uid);
});
}
}

const changeParticipantDetail = (participant, changedOption, originalHTML) => {
const detailedRow = Array.from(document.getElementsByClassName('detailedRow'));
if (detailedRow) {
Expand Down Expand Up @@ -282,6 +308,24 @@ const renderBackToSearchDivAndButton = () => {
`;
};

const renderResetUserButton = (participantUid) => {
if(location.hostname === 'localhost' || location.hostname === '127.0.0.1' || location.host === urls.dev) {
return `
<a
data-toggle="modal"
data-target="#modalShowMoreData"
name="modalResetParticipant"
id="openResetDialog"
data-participantuid="${participantUid}"
>
<button type="button" class="btn btn-danger">Reset User</button>
</a>
`
} else {
return '';
}
}

const renderDetailsTableHeader = () => {
return `
<table class="table detailsTable"> <h4 style="text-align: center;"> Participant Details </h4>
Expand Down
78 changes: 78 additions & 0 deletions src/participantDetailsHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,37 @@ const showAuthUpdateAPIError = (bodyId, message) => {
return false;
}

export const refreshParticipantAfterReset = async (participant) => {
showAnimation();
localStorage.setItem('participant', JSON.stringify(participant));
renderParticipantDetails(participant, {});
appState.setState({unsavedChangesTrack:{saveFlag: false, counter: 0}})
let alertList = document.getElementById('alert_placeholder');
let template = '';
template += `<div class="alert alert-warning alert-dismissible fade show" role="alert">
Success! Participant Reset.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>`
hideAnimation();
alertList.innerHTML = template;
}

export const participantRefreshError = async (errorMsg) => {
showAnimation();
let alertList = document.getElementById('alert_placeholder');
let template = '';
template += `<div class="alert alert-danger alert-dismissible fade show" role="alert">
Error resetting participant: ${errorMsg}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>`
hideAnimation();
alertList.innerHTML = template;
}

export const refreshParticipantAfterUpdate = async (participant) => {
showAnimation();
localStorage.setItem('participant', JSON.stringify(participant));
Expand Down Expand Up @@ -1206,6 +1237,29 @@ export const submitClickHandler = async (participant, changedOption) => {
}
};

export const resetClickHandlers = async (participantUid) => {
const resetButton = document.getElementById('resetUserBtn');
if(!resetButton) {
return;
}
resetButton.addEventListener('click', async () => {
try {
const json = await postResetUserData(participantUid);
closeModal();
if(json.code === 200) {
refreshParticipantAfterReset(json.data.data);
} else if (json.code === 404) {
participantRefreshError('Unable to find participant.');
} else {
participantRefreshError(json.data);
}
} catch(error) {
console.error('error', error);
participantRefreshError('Unknown error.');
}
});
}

/**
* Handle the query.frstName and query.lastName fields.
* Check changedUserDataForProfile the participant profile for all name types. If a name is in changedUserDataForProfile, Add it to the queryNameArray.
Expand Down Expand Up @@ -1490,3 +1544,27 @@ export const postUserDataUpdate = async (changedUserData) => {
throw error;
}
}

export const postResetUserData = async (uid) => {
try {
const idToken = await getIdToken();
const response = await fetch(`${baseAPI}/dashboard?api=resetUser`, {
method: "POST",
headers:{
Authorization: "Bearer " + idToken,
"Content-Type": "application/json"
},
body: JSON.stringify({uid, saveToDb: 'true'})
});

if (!response.ok) {
const error = (response.status + ": " + (await response.json()).message);
throw new Error(error);
}

return await response.json();
} catch (error) {
console.error('Error in postResetUserData:', error);
throw error;
}
}

0 comments on commit 0542295

Please sign in to comment.