Skip to content

Commit 6baa051

Browse files
committed
feat: add confirmation modal for new upload and enhance discard functionality
1 parent c212f0c commit 6baa051

File tree

2 files changed

+79
-16
lines changed

2 files changed

+79
-16
lines changed

frontend/src/pages/Reports/ReportDetailPage.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,24 @@ const ReportDetailPage: React.FC = () => {
8989
const handleDiscard = async () => {
9090
try {
9191
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
92-
92+
9393
// Show toast notification
9494
createToast({
95-
message: t('report.discard.success', {
96-
ns: 'reportDetail',
97-
defaultValue: 'Report deleted successfully'
95+
message: t('report.discard.success', {
96+
ns: 'reportDetail',
97+
defaultValue: 'Report deleted successfully',
9898
}),
9999
duration: 2000,
100100
});
101-
101+
102102
// Navigate back
103103
history.push('/tabs/home');
104104
} catch (error) {
105105
console.error('Error discarding report:', error);
106106
createToast({
107-
message: t('report.discard.error', {
108-
ns: 'reportDetail',
109-
defaultValue: 'Failed to delete report'
107+
message: t('report.discard.error', {
108+
ns: 'reportDetail',
109+
defaultValue: 'Failed to delete report',
110110
}),
111111
duration: 2000,
112112
color: 'danger',
@@ -139,10 +139,11 @@ const ReportDetailPage: React.FC = () => {
139139
<InfoCard />
140140

141141
{/* Action buttons at the bottom */}
142-
<ActionButtons
143-
onDiscard={handleDiscard}
142+
<ActionButtons
143+
onDiscard={handleDiscard}
144144
onNewUpload={handleNewUpload}
145145
reportTitle={reportData.title || reportData.category}
146+
reportId={reportId}
146147
/>
147148
</IonContent>
148149
</IonPage>

frontend/src/pages/Reports/components/ActionButtons.tsx

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import React, { useState } from 'react';
22
import { useTranslation } from 'react-i18next';
3+
import axios from 'axios';
4+
import { getAuthConfig } from 'common/api/reportService';
35
import ConfirmationModal from '../../../common/components/Modal/ConfirmationModal';
46

7+
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
8+
59
interface ActionButtonsProps {
610
onDiscard: () => void;
711
onNewUpload: () => void;
812
reportTitle?: string;
13+
reportId?: string;
914
}
1015

11-
const ActionButtons: React.FC<ActionButtonsProps> = ({ onDiscard, onNewUpload, reportTitle = "" }) => {
16+
const ActionButtons: React.FC<ActionButtonsProps> = ({
17+
onDiscard,
18+
onNewUpload,
19+
reportTitle = '',
20+
reportId,
21+
}) => {
1222
const { t } = useTranslation();
1323
const [showConfirmDiscard, setShowConfirmDiscard] = useState(false);
24+
const [showConfirmNewUpload, setShowConfirmNewUpload] = useState(false);
25+
const [isDeleting, setIsDeleting] = useState(false);
1426

1527
const handleDiscardClick = () => {
1628
setShowConfirmDiscard(true);
@@ -25,29 +37,61 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ onDiscard, onNewUpload, r
2537
setShowConfirmDiscard(false);
2638
};
2739

40+
const handleNewUploadClick = () => {
41+
setShowConfirmNewUpload(true);
42+
};
43+
44+
const handleConfirmNewUpload = async () => {
45+
setShowConfirmNewUpload(false);
46+
47+
// If we have a reportId, delete the current report before going to upload screen
48+
if (reportId) {
49+
try {
50+
setIsDeleting(true);
51+
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
52+
} catch (error) {
53+
console.error('Error deleting report before new upload:', error);
54+
} finally {
55+
setIsDeleting(false);
56+
// Even if delete failed, still go to upload screen
57+
onNewUpload();
58+
}
59+
} else {
60+
// If no reportId, just navigate to upload
61+
onNewUpload();
62+
}
63+
};
64+
65+
const handleCancelNewUpload = () => {
66+
setShowConfirmNewUpload(false);
67+
};
68+
2869
return (
2970
<>
3071
<div className="report-detail-page__actions">
3172
<button
3273
className="report-detail-page__action-button report-detail-page__action-button--discard"
3374
onClick={handleDiscardClick}
75+
disabled={isDeleting}
3476
>
3577
{t('report.action.discard', { ns: 'reportDetail' })}
3678
</button>
3779
<button
3880
className="report-detail-page__action-button report-detail-page__action-button--upload"
39-
onClick={onNewUpload}
81+
onClick={handleNewUploadClick}
82+
disabled={isDeleting}
4083
>
4184
{t('report.action.new-upload', { ns: 'reportDetail' })}
4285
</button>
4386
</div>
4487

45-
<ConfirmationModal
88+
<ConfirmationModal
4689
isOpen={showConfirmDiscard}
4790
title={t('report.discard.title', { ns: 'reportDetail', defaultValue: 'Discard report?' })}
48-
message={t('report.discard.message', {
49-
ns: 'reportDetail',
50-
defaultValue: '{itemName} will be discarded and not be saved to your reports archive. Are you sure?'
91+
message={t('report.discard.message', {
92+
ns: 'reportDetail',
93+
defaultValue:
94+
'{itemName} will be discarded and not be saved to your reports archive. Are you sure?',
5195
})}
5296
confirmText={t('common:yes')}
5397
cancelText={t('common:no')}
@@ -56,6 +100,24 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({ onDiscard, onNewUpload, r
56100
itemName={reportTitle}
57101
testid="discard-report-confirmation"
58102
/>
103+
104+
<ConfirmationModal
105+
isOpen={showConfirmNewUpload}
106+
title={t('report.new-upload.title', {
107+
ns: 'reportDetail',
108+
defaultValue: 'Upload new report?',
109+
})}
110+
message={t('report.new-upload.message', {
111+
ns: 'reportDetail',
112+
defaultValue:
113+
"You've chosen to upload a new report. This will replace your current one. Do you still want to proceed?",
114+
})}
115+
confirmText={t('common:yes')}
116+
cancelText={t('common:no')}
117+
onConfirm={handleConfirmNewUpload}
118+
onCancel={handleCancelNewUpload}
119+
testid="new-upload-confirmation"
120+
/>
59121
</>
60122
);
61123
};

0 commit comments

Comments
 (0)