@@ -3,61 +3,67 @@ import axios from "axios";
33import "../styles/SupervisorDashboard.css" ;
44import ViewFormModal from "./ViewFormModal" ;
55
6-
76const SupervisorDashboard = ( ) => {
87 const [ requests , setRequests ] = useState ( [ ] ) ;
98 const [ selectedForm , setSelectedForm ] = useState ( null ) ;
109 const [ loading , setLoading ] = useState ( true ) ;
1110 const [ message , setMessage ] = useState ( "" ) ;
1211
13- const handleFormActionComplete = ( ) => {
14- fetchRequests ( ) ; // ✅ Refresh from DB
15- setSelectedForm ( null ) ;
16- } ;
17-
12+ const token = localStorage . getItem ( "token" ) || "" ;
1813
1914 const fetchRequests = async ( ) => {
2015 try {
21- const res = await axios . get ( `${ process . env . REACT_APP_API_URL } /api/form/a1forms` ) ;
22- setRequests ( res . data ) ;
16+ const res = await axios . get ( `${ process . env . REACT_APP_API_URL } /api/supervisor/forms` , {
17+ headers : {
18+ Authorization : `Bearer ${ token } ` ,
19+ } ,
20+ } ) ;
21+
22+ const formatted = res . data . map ( ( item ) => ( {
23+ _id : item . _id ,
24+ name : item . student ?. userName || item . student ?. name || "N/A" ,
25+ student_id : item . student ?. _id || item . _id ,
26+ form_type : item . form_type || "A.1" ,
27+ createdAt : item . createdAt ,
28+ supervisor_status : item . supervisor_status || "pending" ,
29+ fullForm : item ,
30+ } ) ) ;
31+
32+ setRequests ( formatted ) ;
2333 setLoading ( false ) ;
2434 } catch ( err ) {
25- console . error ( "Error fetching requests :" , err ) ;
26- setMessage ( "Error fetching requests ." ) ;
35+ console . error ( "Error fetching Internship A1 forms :" , err ) ;
36+ setMessage ( "Error fetching Internship A1 forms ." ) ;
2737 setLoading ( false ) ;
2838 }
2939 } ;
30-
3140
3241 useEffect ( ( ) => {
33- const fetchRequests = async ( ) => {
34- try {
35- const res = await axios . get ( `${ process . env . REACT_APP_API_URL } /api/form/a1forms` ) ;
36-
37-
38- setRequests ( res . data ) ;
39- setLoading ( false ) ;
40- } catch ( err ) {
41- console . error ( "Error fetching requests:" , err ) ;
42- setMessage ( "Error fetching requests." ) ;
43- setLoading ( false ) ;
44- }
45- } ;
4642 fetchRequests ( ) ;
47-
4843 } , [ ] ) ;
4944
45+ const handleFormActionComplete = ( ) => {
46+ fetchRequests ( ) ; // Refresh table after Approve/Reject
47+ setSelectedForm ( null ) ;
48+ } ;
5049
51-
52- const handleAction = async ( id , action , comment ) => {
50+ const handleAction = async ( id , form_type , action , comment ) => {
5351 const confirmed = window . confirm ( `Are you sure you want to ${ action } this request?` ) ;
5452 if ( ! confirmed ) return ;
5553
5654 try {
57- const res = await axios . post ( `${ process . env . REACT_APP_API_URL } /api/submissions/${ id } /${ action } ` , { comment } ) ;
55+ const res = await axios . post (
56+ `${ process . env . REACT_APP_API_URL } /api/supervisor/form/${ form_type } /${ id } /${ action } ` ,
57+ { comment } ,
58+ {
59+ headers : {
60+ Authorization : `Bearer ${ token } ` ,
61+ } ,
62+ }
63+ ) ;
5864
5965 setMessage ( res . data . message || `${ action } successful` ) ;
60- setRequests ( prev => prev . filter ( req => req . _id !== id ) ) ;
66+ setRequests ( ( prev ) => prev . filter ( ( req ) => req . _id !== id ) ) ;
6167 setSelectedForm ( null ) ;
6268 } catch ( err ) {
6369 console . error ( `Failed to ${ action } request:` , err ) ;
@@ -68,26 +74,19 @@ const SupervisorDashboard = () => {
6874 const openFormView = ( form ) => setSelectedForm ( form ) ;
6975 const closeFormView = ( ) => setSelectedForm ( null ) ;
7076
71- const formatDate = ( dateStr ) => new Date ( dateStr ) . toLocaleDateString ( ) ;
72-
73- const sortedRequests = [ ...requests ]
74- . filter ( ( req ) => req . status . toLowerCase ( ) === "submitted" )
75- . sort ( ( a , b ) => new Date ( a . createdAt ) - new Date ( b . createdAt ) ) ;
76-
77+ const formatDate = ( date ) => new Date ( date ) . toLocaleDateString ( ) ;
7778
7879 let content ;
7980
8081 if ( loading ) {
8182 content = < p > Loading...</ p > ;
82- }
83- else if ( sortedRequests . length === 0 ) {
83+ } else if ( requests . length === 0 ) {
8484 content = (
8585 < div className = "empty-message-container" >
8686 < div className = "empty-message" > No pending approvals.</ div >
8787 </ div >
8888 ) ;
89- }
90- else {
89+ } else {
9190 content = (
9291 < table className = "dashboard-table" >
9392 < thead >
@@ -100,21 +99,19 @@ const SupervisorDashboard = () => {
10099 </ tr >
101100 </ thead >
102101 < tbody >
103- { sortedRequests . map ( ( req ) => (
104- < tr key = { req . _id } className = "clickable-row" onClick = { ( ) => openFormView ( req ) } >
105- < td > { req . studentName } </ td >
106- < td > { req . soonerId } </ td >
107- < td > A.1</ td >
108- < td > { formatDate ( req . createdAt ) } </ td >
109- < td >
110- < span className = { `status-badge ${ req . status } ` } >
111- { req . status }
112- </ span >
113- </ td >
114- </ tr >
115-
116- ) ) }
117-
102+ { requests . map ( ( req ) => (
103+ < tr key = { req . _id } className = "clickable-row" onClick = { ( ) => openFormView ( req . fullForm ) } >
104+ < td > { req . name } </ td >
105+ < td > { req . student_id } </ td >
106+ < td > { req . form_type } </ td >
107+ < td > { formatDate ( req . createdAt ) } </ td >
108+ < td >
109+ < span className = { `status-badge ${ req . supervisor_status } ` } >
110+ { req . supervisor_status }
111+ </ span >
112+ </ td >
113+ </ tr >
114+ ) ) }
118115 </ tbody >
119116 </ table >
120117 ) ;
@@ -137,4 +134,4 @@ const SupervisorDashboard = () => {
137134 ) ;
138135} ;
139136
140- export default SupervisorDashboard ;
137+ export default SupervisorDashboard ;
0 commit comments