Skip to content

Commit 73c2c63

Browse files
authored
Prevent accidental data discarding (#534)
* Add onstepchange alerts * Add translations and clean up code * Clean up stepContent * Modularize functions * Edit translations
1 parent ee60c28 commit 73c2c63

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

frontend/src/components/StepContent/StepContent.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ const StepContent = ({
2626
loading,
2727
stepData,
2828
onDataSaved,
29+
edit,
30+
setEdit,
2931
}) => {
30-
const [edit, setEdit] = useState(false);
3132
const [updatedData, setUpdatedData] = useState(_.cloneDeep(stepData));
3233
const [currentQuestion, setCurrentQuestion] = useState(0);
3334
const [singleQuestionFormat, setSingleQuestionFormat] = useState(false);
@@ -38,6 +39,19 @@ const StepContent = ({
3839
setUpdatedData(_.cloneDeep(stepData));
3940
}, [stepData]);
4041

42+
useEffect(() => {
43+
const determinePreventDefault = (e) => {
44+
// Check if any of the step is being edited
45+
if (edit) {
46+
e.preventDefault();
47+
e.returnValue = '';
48+
}
49+
};
50+
window.addEventListener('beforeunload', determinePreventDefault);
51+
return () =>
52+
window.removeEventListener('beforeunload', determinePreventDefault);
53+
}, [edit]);
54+
4155
const handleSimpleUpdate = (fieldKey, value) => {
4256
setUpdatedData((data) => {
4357
const dataCopy = _.cloneDeep(data);
@@ -289,6 +303,8 @@ StepContent.propTypes = {
289303
loading: PropTypes.bool.isRequired,
290304
stepData: PropTypes.object.isRequired,
291305
onDataSaved: PropTypes.func.isRequired,
306+
edit: PropTypes.bool.isRequired,
307+
setEdit: PropTypes.func.isRequired,
292308
};
293309

294310
export default StepContent;

frontend/src/pages/PatientDetail/PatientDetail.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const PatientDetail = () => {
3838
const [isManagePatientModalOpen, setManagePatientModalOpen] =
3939
useState(false);
4040
const stepKeyParam = useQueryParam('stepKey', StringParam)[0];
41+
const [edit, setEdit] = useState(false);
4142

4243
/**
4344
* Fetch metadata for all steps and the patient's data.
@@ -116,12 +117,36 @@ const PatientDetail = () => {
116117
};
117118
};
118119

119-
const onStepChange = (newStep) => {
120+
const displayUnsavedDataAlert = (newStep) => {
121+
swal({
122+
title: translations.components.swal.dataDiscarding
123+
.confirmationQuestion,
124+
buttons: [
125+
translations.components.swal.dataDiscarding.stay,
126+
translations.components.swal.dataDiscarding.leave,
127+
],
128+
}).then((isLeaveConfirmed) => {
129+
if (isLeaveConfirmed) {
130+
switchStep(newStep);
131+
}
132+
});
133+
};
134+
135+
const switchStep = (newStep) => {
120136
if (newStep === null) return;
121137
setSelectedStep(newStep);
138+
setEdit(false);
122139
window.history.pushState({}, '', `${patientId}?stepKey=${newStep}`);
123140
};
124141

142+
const onStepChange = (newStep) => {
143+
if (edit) {
144+
displayUnsavedDataAlert(newStep);
145+
} else {
146+
switchStep(newStep);
147+
}
148+
};
149+
125150
/**
126151
* This generates the main content of this screen.
127152
* Renders all of the fields in the selected step
@@ -148,6 +173,8 @@ const PatientDetail = () => {
148173
)}
149174
stepData={patientData[step.key] ?? {}}
150175
loading={loading}
176+
edit={edit}
177+
setEdit={setEdit}
151178
/>
152179
);
153180
})}

frontend/src/translations.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@
145145
"discard": "Discard"
146146
},
147147
"swal": {
148+
"dataDiscarding": {
149+
"stay": "Stay",
150+
"leave": "Leave",
151+
"confirmationQuestion": "You have unsaved changes. Do you want to leave this page?"
152+
},
148153
"createPatient": {
149154
"title": "New Patient",
150155
"firstName": "Name",
@@ -382,6 +387,11 @@
382387
"discard": "تجاهل"
383388
},
384389
"swal": {
390+
"dataDiscarding": {
391+
"stay": "البقاء",
392+
"leave": "يترك",
393+
"confirmationQuestion": "لم تحفظ التغييرات. هل تريد مغادرة هذه الصفحة؟"
394+
},
385395
"createPatient": {
386396
"title": "\u0645\u0631\u064a\u0636 \u062c\u062f\u064a\u062f",
387397
"firstName": "\u0627\u0633\u0645",

0 commit comments

Comments
 (0)