@@ -6,6 +6,9 @@ import { useEffect, useState, useRef } from 'react';
66import { useAxios } from '../../common/hooks/useAxios' ;
77import './ProcessingPage.scss' ;
88import { getAuthConfig } from 'common/api/reportService' ;
9+ import ProcessingError from './components/ProcessingError' ;
10+ import ProcessingAnimation from './components/ProcessingAnimation' ;
11+
912const API_URL = import . meta. env . VITE_BASE_URL_API || '' ;
1013
1114/**
@@ -28,6 +31,13 @@ const ProcessingPage: React.FC = () => {
2831 const location = useLocation < { reportId : string } > ( ) ;
2932 const reportId = location . state ?. reportId ;
3033
34+ const clearStatusCheckInterval = ( ) => {
35+ if ( statusCheckIntervalRef . current ) {
36+ window . clearInterval ( statusCheckIntervalRef . current ) ;
37+ statusCheckIntervalRef . current = null ;
38+ }
39+ } ;
40+
3141 // Check the status of the report processing
3242 const checkReportStatus = async ( ) => {
3343 if ( ! reportId ) return ;
@@ -38,86 +48,79 @@ const ProcessingPage: React.FC = () => {
3848 await getAuthConfig ( ) ,
3949 ) ;
4050
41- console . log ( 'Report status:' , response . data ) ;
51+ const data = response . data ;
4252
4353 // If processing is complete, clear the interval and redirect to the report page
44- if ( response . data . isComplete ) {
54+ if ( data . isComplete ) {
4555 setIsProcessing ( false ) ;
4656
4757 // Clear the interval
48- if ( statusCheckIntervalRef . current ) {
49- window . clearInterval ( statusCheckIntervalRef . current ) ;
50- statusCheckIntervalRef . current = null ;
51- }
58+ clearStatusCheckInterval ( ) ;
5259
5360 console . log ( 'Processing complete' ) ;
5461
5562 // Redirect to report detail page
5663 history . push ( `/tabs/reports/${ reportId } ` ) ;
64+ } else if ( data . status === 'failed' ) {
65+ throw new Error ( 'Processing failed' ) ;
5766 }
5867 } catch ( error ) {
68+ // Clear the interval on error
69+ clearStatusCheckInterval ( ) ;
70+
5971 console . error ( 'Error checking report status:' , error ) ;
60- setProcessingError ( 'Failed to check the status of the report. Please try again.' ) ;
72+ setProcessingError ( 'An error occurred while processing the report. Please try again.' ) ;
6173 setIsProcessing ( false ) ;
74+ }
75+ } ;
6276
63- // Clear the interval on error
64- if ( statusCheckIntervalRef . current ) {
65- window . clearInterval ( statusCheckIntervalRef . current ) ;
66- statusCheckIntervalRef . current = null ;
67- }
77+ // Process file function - moved outside useEffect to make it callable from buttons
78+ const processFile = async ( ) => {
79+ if ( ! reportId ) {
80+ return ;
81+ }
82+
83+ try {
84+ // Send POST request to backend API
85+ await axios . post (
86+ `${ API_URL } /api/document-processor/process-file` ,
87+ { reportId } ,
88+ await getAuthConfig ( ) ,
89+ ) ;
90+
91+ // Start checking the status every 2 seconds
92+ statusCheckIntervalRef . current = window . setInterval ( checkReportStatus , 2000 ) ;
93+ } catch ( error ) {
94+ console . error ( 'Error processing file:' , error ) ;
95+ setProcessingError ( 'Failed to process the file. Please try again.' ) ;
96+ setIsProcessing ( false ) ;
6897 }
6998 } ;
7099
100+ // Handle retry attempt
101+ const handleRetry = ( ) => {
102+ // Reset error state and try processing the same file again
103+ setProcessingError ( null ) ;
104+ setIsProcessing ( true ) ;
105+ hasInitiatedProcessing . current = false ;
106+ processFile ( ) ;
107+ } ;
108+
71109 // Send the API request when component mounts
72110 useEffect ( ( ) => {
73111 // Use ref to ensure this effect runs only once for the core logic
74112 if ( hasInitiatedProcessing . current ) {
75113 return ;
76114 }
77115
78- if ( ! reportId ) {
79- setProcessingError ( 'No report ID provided' ) ;
80- setIsProcessing ( false ) ;
81- hasInitiatedProcessing . current = true ; // Mark as initiated even on error
82- return ;
83- }
84-
85116 hasInitiatedProcessing . current = true ; // Mark as initiated before fetching
86117
87- const processFile = async ( ) => {
88- try {
89- // Send POST request to backend API
90- const response = await axios . post (
91- `${ API_URL } /api/document-processor/process-file` ,
92- { reportId } ,
93- await getAuthConfig ( ) ,
94- ) ;
95-
96- console . log ( 'File processing started:' , response . data ) ;
97-
98- // Start checking the status every 2 seconds
99- statusCheckIntervalRef . current = window . setInterval ( checkReportStatus , 2000 ) ;
100-
101- // Run the first status check immediately
102- checkReportStatus ( ) ;
103- } catch ( error ) {
104- console . error ( 'Error processing file:' , error ) ;
105- setProcessingError ( 'Failed to process the file. Please try again.' ) ;
106- setIsProcessing ( false ) ;
107- }
108- } ;
109-
110118 processFile ( ) ;
111119
112120 // Clean up the interval when the component unmounts
113- return ( ) => {
114- if ( statusCheckIntervalRef . current ) {
115- window . clearInterval ( statusCheckIntervalRef . current ) ;
116- }
117- } ;
121+ return clearStatusCheckInterval ;
118122 } , [ reportId , location , history ] ) ;
119123
120-
121124 return (
122125 < IonPage className = "processing-page" >
123126 < IonContent fullscreen >
@@ -132,41 +135,19 @@ const ProcessingPage: React.FC = () => {
132135 testid = "processing-user-avatar"
133136 />
134137 </ div >
135-
136- { /* Title section */ }
137- < div className = "processing-page__title" >
138- < p className = "processing-page__subtitle" >
139- Just a few seconds{ firstName && ', ' + firstName } !
140- </ p >
141- < h1 className = "processing-page__heading" >
142- { processingError ? 'Processing Error' : 'Processing Data...' }
143- </ h1 >
144- { processingError && < p className = "processing-page__error" > { processingError } </ p > }
145- </ div >
146138 </ div >
147139
148- { /* Animation circle */ }
149- { isProcessing && (
150- < div className = "processing-page__animation" >
151- < div className = "processing-page__animation-circle" > </ div >
152- </ div >
153- ) }
140+ { /* Processing animation */ }
141+ { isProcessing && < ProcessingAnimation firstName = { firstName } /> }
154142
155- { /* Error state - show retry button */ }
143+ { /* Error state - shows when processing fails */ }
156144 { processingError && (
157- < div className = "processing-page__error-actions" >
158- < button
159- className = "processing-page__retry-btn"
160- onClick = { ( ) => history . push ( '/tabs/upload' ) }
161- >
162- Try Again
163- </ button >
164- </ div >
145+ < ProcessingError errorMessage = { processingError } onRetry = { handleRetry } />
165146 ) }
166147 </ div >
167148 </ IonContent >
168149 </ IonPage >
169150 ) ;
170151} ;
171152
172- export default ProcessingPage ;
153+ export default ProcessingPage ;
0 commit comments