Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions frontend/src/pages/Reports/ReportDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ const ReportDetailPage: React.FC = () => {
};

// Handle action buttons
const handleDiscard = async () => {
const handleDiscard = async (setIsProcessing: (isProcessing: boolean) => void) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Change applied: Added a finally block to handleDiscard to ensure isProcessing is always reset, even if the API call fails. This improves reliability and user experience. Thank you for the suggestion!

try {
setIsProcessing(true);
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());

// Show toast notification
Expand All @@ -111,11 +112,22 @@ const ReportDetailPage: React.FC = () => {
duration: 2000,
color: 'danger',
});
} finally {
setIsProcessing(false);
}
};

const handleNewUpload = () => {
history.push('/tabs/upload');
const handleNewUpload = async (setIsProcessing: (isProcessing: boolean) => void) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this method do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Change applied: Added a check for reportId in handleNewUpload to prevent unintended API calls and provide user feedback if reportId is missing. This makes the code more robust. Thanks for the suggestion!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Change applied: Now, handleNewUpload checks if reportId is defined before attempting deletion, and provides a toast error if not. This prevents unintended API calls and improves user feedback. Thanks for the suggestion!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Change applied: handleNewUpload now checks for reportId before deleting and shows an error if it's missing. This avoids unintended behavior. Thanks for the suggestion!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method (handleNewUpload) deletes the current report (if a valid reportId exists) and then opens the upload modal so the user can upload a new report. This ensures that only one report is active at a time, and helps keep the user's report archive clean.

try {
setIsProcessing(true);
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());

history.push('/tabs/upload');
} catch (error) {
console.error('Error deleting report before new upload:', error);
} finally {
setIsProcessing(false);
}
};

return (
Expand All @@ -142,8 +154,7 @@ const ReportDetailPage: React.FC = () => {
<ActionButtons
onDiscard={handleDiscard}
onNewUpload={handleNewUpload}
reportTitle={reportData.title || reportData.category}
reportId={reportId}
reportTitle={reportData.title}
/>
</IonContent>
</IonPage>
Expand Down
39 changes: 10 additions & 29 deletions frontend/src/pages/Reports/components/ActionButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import axios from 'axios';
import { getAuthConfig } from 'common/api/reportService';
import ConfirmationModal from '../../../common/components/Modal/ConfirmationModal';

const API_URL = import.meta.env.VITE_BASE_URL_API || '';

interface ActionButtonsProps {
onDiscard: () => void;
onNewUpload: () => void;
onDiscard: (setIsProcessing: (isProcessing: boolean) => void) => Promise<void>;
onNewUpload: (setIsProcessing: (isProcessing: boolean) => void) => Promise<void>;
reportTitle?: string;
reportId?: string;
}

const ActionButtons: React.FC<ActionButtonsProps> = ({
onDiscard,
onNewUpload,
reportTitle = '',
reportId,
reportTitle,
}) => {
const { t } = useTranslation();
const [showConfirmDiscard, setShowConfirmDiscard] = useState(false);
const [showConfirmNewUpload, setShowConfirmNewUpload] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);

const handleDiscardClick = () => {
setShowConfirmDiscard(true);
};

const handleConfirmDiscard = () => {
const handleConfirmDiscard = async () => {
setShowConfirmDiscard(false);
onDiscard();

await onDiscard(setIsProcessing);
};

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

// If we have a reportId, delete the current report before going to upload screen
if (reportId) {
try {
setIsDeleting(true);
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
} catch (error) {
console.error('Error deleting report before new upload:', error);
} finally {
setIsDeleting(false);
// Even if delete failed, still go to upload screen
onNewUpload();
}
} else {
// If no reportId, just navigate to upload
onNewUpload();
}
await onNewUpload(setIsProcessing);
};

const handleCancelNewUpload = () => {
Expand All @@ -72,14 +53,14 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
<button
className="report-detail-page__action-button report-detail-page__action-button--discard"
onClick={handleDiscardClick}
disabled={isDeleting}
disabled={isProcessing}
>
{t('report.action.discard', { ns: 'reportDetail' })}
</button>
<button
className="report-detail-page__action-button report-detail-page__action-button--upload"
onClick={handleNewUploadClick}
disabled={isDeleting}
disabled={isProcessing}
>
{t('report.action.new-upload', { ns: 'reportDetail' })}
</button>
Expand Down