@@ -3,7 +3,7 @@ import { useParams } from 'react-router-dom';
33import axios from 'axios' ;
44
55const Apply = ( { assistantId } ) => {
6- const { id : postId } = useParams ( ) ; // URL param for the post
6+ const { id : postId } = useParams ( ) ; // Extract postId from URL
77 const [ message , setMessage ] = useState ( '' ) ;
88 const [ status , setStatus ] = useState ( null ) ;
99 const [ loading , setLoading ] = useState ( false ) ;
@@ -14,16 +14,28 @@ const Apply = ({ assistantId }) => {
1414 setStatus ( null ) ;
1515
1616 try {
17+ const trimmedMessage = message . trim ( ) ;
18+ if ( ! trimmedMessage ) {
19+ setStatus ( "❗ Message cannot be empty." ) ;
20+ setLoading ( false ) ;
21+ return ;
22+ }
23+
1724 const res = await axios . post ( 'https://research-finder-server.vercel.app/applications' , {
1825 assistantId,
1926 postId,
20- message,
27+ message : trimmedMessage ,
2128 } ) ;
2229
23- setStatus ( '✅ Application submitted successfully!' ) ;
30+ if ( res . status === 201 ) {
31+ setStatus ( '✅ Application submitted successfully!' ) ;
32+ setMessage ( '' ) ; // Clear message after success
33+ } else {
34+ setStatus ( '⚠️ Unexpected response. Try again later.' ) ;
35+ }
2436 } catch ( err ) {
2537 console . error ( '❌ Application error:' , err . response ?. data || err . message ) ;
26- setStatus ( ' ❌ Failed to submit application.' ) ;
38+ setStatus ( ` ❌ Failed to submit application: ${ err . response ?. data ?. error || err . message } ` ) ;
2739 } finally {
2840 setLoading ( false ) ;
2941 }
@@ -33,23 +45,40 @@ const Apply = ({ assistantId }) => {
3345 < div style = { { padding : '2rem' , maxWidth : '600px' , margin : '0 auto' } } >
3446 < h2 > Apply to Research Opportunity</ h2 >
3547 < form onSubmit = { handleSubmit } >
36- < label >
48+ < label htmlFor = "application-message" >
3749 Why are you interested?
38- < textarea
39- rows = "5"
40- value = { message }
41- onChange = { ( e ) => setMessage ( e . target . value ) }
42- required
43- style = { { width : '100%' , marginTop : '0.5rem' } }
44- />
4550 </ label >
46- < br />
47- < button type = "submit" disabled = { loading } style = { { marginTop : '1rem' } } >
51+ < textarea
52+ id = "application-message"
53+ rows = "5"
54+ value = { message }
55+ onChange = { ( e ) => setMessage ( e . target . value ) }
56+ required
57+ placeholder = "Write your message here..."
58+ style = { { width : '100%' , marginTop : '0.5rem' , padding : '0.5rem' } }
59+ />
60+ < button
61+ type = "submit"
62+ disabled = { loading }
63+ style = { {
64+ marginTop : '1rem' ,
65+ padding : '0.5rem 1rem' ,
66+ backgroundColor : '#007bff' ,
67+ color : 'white' ,
68+ border : 'none' ,
69+ borderRadius : '5px' ,
70+ cursor : 'pointer'
71+ } }
72+ >
4873 { loading ? 'Submitting...' : 'Submit Application' }
4974 </ button >
5075 </ form >
5176
52- { status && < p style = { { marginTop : '1rem' , fontWeight : 'bold' } } > { status } </ p > }
77+ { status && (
78+ < p style = { { marginTop : '1rem' , fontWeight : 'bold' , color : status . startsWith ( '✅' ) ? 'green' : 'red' } } >
79+ { status }
80+ </ p >
81+ ) }
5382 </ div >
5483 ) ;
5584} ;
0 commit comments