Skip to content

Commit 154e3ee

Browse files
authored
Merge pull request #131 from ModusCreateOrg/ADE-67
[ADE-67] Refactor action button handlers to manage processing state during report deletion and new uploads
2 parents 6e94445 + d2eab3c commit 154e3ee

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

frontend/src/pages/Reports/ReportDetailPage.tsx

Lines changed: 17 additions & 5 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({
@@ -102,6 +104,8 @@ const ReportDetailPage: React.FC = () => {
102104
// Navigate back
103105
history.push('/tabs/home');
104106
} catch (error) {
107+
setIsProcessing(false);
108+
105109
console.error('Error discarding report:', error);
106110
createToast({
107111
message: t('report.discard.error', {
@@ -114,8 +118,17 @@ const ReportDetailPage: React.FC = () => {
114118
}
115119
};
116120

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

121134
return (
@@ -142,8 +155,7 @@ const ReportDetailPage: React.FC = () => {
142155
<ActionButtons
143156
onDiscard={handleDiscard}
144157
onNewUpload={handleNewUpload}
145-
reportTitle={reportData.title || reportData.category}
146-
reportId={reportId}
158+
reportTitle={reportData.title}
147159
/>
148160
</IonContent>
149161
</IonPage>

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
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
}
1511

1612
const ActionButtons: React.FC<ActionButtonsProps> = ({
1713
onDiscard,
1814
onNewUpload,
19-
reportTitle = '',
20-
reportId,
15+
reportTitle,
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)