@@ -21,6 +21,7 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
2121 const { currentUser } = useAuth ( ) ;
2222 const [ documents , setDocuments ] = useState < Document [ ] > ( [ ] ) ;
2323 const [ isFormatting , setIsFormatting ] = useState < boolean > ( false ) ;
24+ const [ processingComplete , setProcessingComplete ] = useState < boolean > ( false ) ;
2425 const [ error , setError ] = useState < string | null > ( null ) ;
2526 const [ status , setStatus ] = useState < string > ( '' ) ;
2627
@@ -35,44 +36,88 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
3536 const documentsRef = collection ( userDocRef , 'documents' ) ;
3637 const q = query ( documentsRef ) ;
3738
39+ console . log ( 'Setting up document status listener' ) ;
40+
3841 const unsubscribe = onSnapshot ( q , ( snapshot ) => {
3942 const docs : Document [ ] = [ ] ;
43+
44+ console . log ( `Received ${ snapshot . docs . length } documents from Firestore` ) ;
45+
4046 snapshot . forEach ( doc => {
47+ const data = doc . data ( ) ;
48+ console . log ( `Document ${ doc . id } , Type: ${ data . documentType } , Status: ${ data . status } ` ) ;
49+
4150 docs . push ( {
4251 id : doc . id ,
43- ...doc . data ( ) as Omit < Document , 'id' >
52+ ...data as Omit < Document , 'id' >
4453 } ) ;
4554 } ) ;
4655
56+ // Update the documents state
4757 setDocuments ( docs ) ;
4858
49- // Check if all documents are processed
50- const allProcessed = docs . length > 0 && docs . every ( doc => doc . status === 'processed' ) ;
51- if ( allProcessed && onProcessingComplete ) {
52- onProcessingComplete ( ) ;
59+ if ( docs . length > 0 ) {
60+ // Force case-insensitive comparison for document types
61+ const documentTypeCount = {
62+ syllabus : docs . filter ( doc => doc . documentType ?. toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS ) . length ,
63+ transcript : docs . filter ( doc => doc . documentType ?. toLowerCase ( ) === DOCUMENT_TYPES . TRANSCRIPT ) . length ,
64+ grades : docs . filter ( doc => doc . documentType ?. toLowerCase ( ) === DOCUMENT_TYPES . GRADES ) . length
65+ } ;
66+
67+ // Force case-insensitive comparison for document status
68+ const statusCount = {
69+ uploaded : docs . filter ( d => d . status ?. toLowerCase ( ) === 'uploaded' ) . length ,
70+ extracted : docs . filter ( d => d . status ?. toLowerCase ( ) === 'extracted' ) . length ,
71+ processed : docs . filter ( d => d . status ?. toLowerCase ( ) === 'processed' ) . length ,
72+ error : docs . filter ( d => d . status ?. toLowerCase ( ) === 'error' ) . length
73+ } ;
74+
75+ console . log ( 'Document counts by type:' , documentTypeCount ) ;
76+ console . log ( 'Document counts by status:' , statusCount ) ;
77+
78+ // Check for processing completion based on processed syllabus
79+ const hasSyllabus = docs . some ( doc =>
80+ doc . documentType ?. toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS &&
81+ doc . status ?. toLowerCase ( ) === 'processed'
82+ ) ;
83+
84+ console . log ( 'Has processed syllabus:' , hasSyllabus ) ;
85+
86+ // Trigger completion if we have a processed syllabus
87+ if ( hasSyllabus && onProcessingComplete && ! processingComplete ) {
88+ console . log ( 'Processing complete condition met - has processed syllabus' ) ;
89+ onProcessingComplete ( ) ;
90+ setProcessingComplete ( true ) ;
91+ }
5392 }
93+ } , ( error ) => {
94+ console . error ( 'Error in document snapshot listener:' , error ) ;
95+ setError ( 'Error monitoring document status' ) ;
5496 } ) ;
5597
5698 return ( ) => unsubscribe ( ) ;
5799 } , [ currentUser , db , onProcessingComplete ] ) ;
58100
59- // Count documents by status
101+ // Calculate document counts with case-insensitive comparison
60102 const documentCounts = {
61- uploaded : documents . filter ( doc => doc . status === 'uploaded' ) . length ,
62- extracted : documents . filter ( doc => doc . status === 'extracted' ) . length ,
63- processed : documents . filter ( doc => doc . status === 'processed' ) . length ,
64- error : documents . filter ( doc => doc . status === 'error' ) . length
103+ uploaded : documents . filter ( doc => doc . status ?. toLowerCase ( ) === 'uploaded' ) . length ,
104+ extracted : documents . filter ( doc => doc . status ?. toLowerCase ( ) === 'extracted' ) . length ,
105+ processed : documents . filter ( doc => doc . status ?. toLowerCase ( ) === 'processed' ) . length ,
106+ error : documents . filter ( doc => doc . status ?. toLowerCase ( ) === 'error' ) . length
65107 } ;
66108
67- // Count documents by type ( case-insensitive)
109+ // Count documents by type with case-insensitive comparison
68110 const documentTypeCount = {
69- syllabus : documents . filter ( doc => doc . documentType . toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS ) . length ,
70- transcript : documents . filter ( doc => doc . documentType . toLowerCase ( ) === DOCUMENT_TYPES . TRANSCRIPT ) . length ,
71- grades : documents . filter ( doc => doc . documentType . toLowerCase ( ) === DOCUMENT_TYPES . GRADES ) . length
111+ syllabus : documents . filter ( doc => doc . documentType ? .toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS ) . length ,
112+ transcript : documents . filter ( doc => doc . documentType ? .toLowerCase ( ) === DOCUMENT_TYPES . TRANSCRIPT ) . length ,
113+ grades : documents . filter ( doc => doc . documentType ? .toLowerCase ( ) === DOCUMENT_TYPES . GRADES ) . length
72114 } ;
73115
74- // Check if we have the minimum required documents (case-insensitive)
75- const hasSyllabus = documents . some ( doc => doc . documentType . toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS ) ;
116+ // Check if we have the minimum required documents with case-insensitive comparison
117+ const hasSyllabus = documents . some ( doc =>
118+ doc . documentType ?. toLowerCase ( ) === DOCUMENT_TYPES . SYLLABUS &&
119+ doc . status ?. toLowerCase ( ) === 'processed'
120+ ) ;
76121 const hasMinimumDocuments = hasSyllabus ;
77122
78123 // Handle manual formatting
@@ -123,10 +168,12 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
123168 }
124169 } ;
125170
126- // Calculate overall progress
171+ // Calculate overall progress with case-insensitive comparison and logging
127172 const calculateProgress = ( ) => {
128173 if ( documents . length === 0 ) return 0 ;
129174
175+ console . log ( 'Calculating progress with' , documents . length , 'documents' ) ;
176+
130177 const totalSteps = documents . length * 2 ; // Upload + Process for each document
131178 let completedSteps = 0 ;
132179
@@ -135,12 +182,14 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
135182 completedSteps += 1 ;
136183
137184 // Count processing step for extracted or processed documents
138- if ( doc . status === 'extracted' || doc . status === 'processed' ) {
185+ if ( doc . status ?. toLowerCase ( ) === 'extracted' || doc . status ?. toLowerCase ( ) === 'processed' ) {
139186 completedSteps += 1 ;
140187 }
141188 } ) ;
142189
143- return Math . round ( ( completedSteps / totalSteps ) * 100 ) ;
190+ const progress = Math . round ( ( completedSteps / totalSteps ) * 100 ) ;
191+ console . log ( `Progress calculation: ${ completedSteps } /${ totalSteps } = ${ progress } %` ) ;
192+ return progress ;
144193 } ;
145194
146195 const progress = calculateProgress ( ) ;
@@ -245,23 +294,23 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP
245294 < td style = { styles . tableCell } >
246295 < span style = { {
247296 ...styles . statusBadge ,
248- backgroundColor : doc . status === 'processed' ? '#4caf50' :
249- doc . status === 'extracted' ? '#ff9800' :
250- doc . status === 'error' ? '#f44336' : '#2196f3'
297+ backgroundColor : doc . status ?. toLowerCase ( ) === 'processed' ? '#4caf50' :
298+ doc . status ?. toLowerCase ( ) === 'extracted' ? '#ff9800' :
299+ doc . status ?. toLowerCase ( ) === 'error' ? '#f44336' : '#2196f3'
251300 } } >
252301 { doc . status }
253302 </ span >
254303 </ td >
255304 < td style = { styles . tableCell } >
256- { doc . status === 'uploaded' && (
305+ { doc . status ?. toLowerCase ( ) === 'uploaded' && (
257306 < button
258307 onClick = { ( ) => handleRetryProcessing ( doc . id ) }
259308 style = { styles . actionButton }
260309 >
261310 Process Document
262311 </ button >
263312 ) }
264- { doc . status === 'error' && (
313+ { doc . status ?. toLowerCase ( ) === 'error' && (
265314 < button
266315 onClick = { ( ) => handleRetryProcessing ( doc . id ) }
267316 style = { styles . actionButton }
0 commit comments