11import React , { useState } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
3+ import axios from 'axios' ;
4+ import { getAuthConfig } from 'common/api/reportService' ;
35import ConfirmationModal from '../../../common/components/Modal/ConfirmationModal' ;
46
7+ const API_URL = import . meta. env . VITE_BASE_URL_API || '' ;
8+
59interface 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