Skip to content

Commit 052bcfe

Browse files
committed
Refactor action button handlers to manage processing state during report deletion and new uploads
1 parent 6e94445 commit 052bcfe

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

frontend/src/pages/Reports/ReportDetailPage.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ const ReportDetailPage: React.FC = () => {
8686
};
8787

8888
// Handle action buttons
89-
const handleDiscard = async () => {
89+
const handleDiscard = async (setIsProcessing: (isProcessing: boolean) => void) => {
9090
try {
91+
setIsProcessing(true);
9192
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
93+
setIsProcessing(false);
9294

9395
// Show toast notification
9496
createToast({
@@ -114,8 +116,16 @@ const ReportDetailPage: React.FC = () => {
114116
}
115117
};
116118

117-
const handleNewUpload = () => {
118-
history.push('/tabs/upload');
119+
const handleNewUpload = async (setIsProcessing: (isProcessing: boolean) => void) => {
120+
try {
121+
setIsProcessing(true);
122+
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
123+
setIsProcessing(false);
124+
125+
history.push('/tabs/upload');
126+
} catch (error) {
127+
console.error('Error deleting report before new upload:', error);
128+
}
119129
};
120130

121131
return (

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

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

7-
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
8-
95
interface ActionButtonsProps {
10-
onDiscard: () => void;
11-
onNewUpload: () => void;
6+
onDiscard: (setIsProcessing: (isProcessing: boolean) => void) => Promise<void>;
7+
onNewUpload: (setIsProcessing: (isProcessing: boolean) => void) => Promise<void>;
128
reportTitle?: string;
139
reportId?: string;
1410
}
@@ -17,20 +13,20 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
1713
onDiscard,
1814
onNewUpload,
1915
reportTitle = '',
20-
reportId,
2116
}) => {
2217
const { t } = useTranslation();
2318
const [showConfirmDiscard, setShowConfirmDiscard] = useState(false);
2419
const [showConfirmNewUpload, setShowConfirmNewUpload] = useState(false);
25-
const [isDeleting, setIsDeleting] = useState(false);
20+
const [isProcessing, setIsProcessing] = useState(false);
2621

2722
const handleDiscardClick = () => {
2823
setShowConfirmDiscard(true);
2924
};
3025

31-
const handleConfirmDiscard = () => {
26+
const handleConfirmDiscard = async () => {
3227
setShowConfirmDiscard(false);
33-
onDiscard();
28+
29+
await onDiscard(setIsProcessing);
3430
};
3531

3632
const handleCancelDiscard = () => {
@@ -44,22 +40,7 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
4440
const handleConfirmNewUpload = async () => {
4541
setShowConfirmNewUpload(false);
4642

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-
}
43+
await onNewUpload(setIsProcessing);
6344
};
6445

6546
const handleCancelNewUpload = () => {
@@ -72,14 +53,14 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
7253
<button
7354
className="report-detail-page__action-button report-detail-page__action-button--discard"
7455
onClick={handleDiscardClick}
75-
disabled={isDeleting}
56+
disabled={isProcessing}
7657
>
7758
{t('report.action.discard', { ns: 'reportDetail' })}
7859
</button>
7960
<button
8061
className="report-detail-page__action-button report-detail-page__action-button--upload"
8162
onClick={handleNewUploadClick}
82-
disabled={isDeleting}
63+
disabled={isProcessing}
8364
>
8465
{t('report.action.new-upload', { ns: 'reportDetail' })}
8566
</button>

0 commit comments

Comments
 (0)