@@ -4,14 +4,16 @@ import axios from 'axios';
44function AnnouncementsPlacement ( ) {
55 const [ announcements , setAnnouncements ] = useState ( [ ] ) ;
66 const [ loggedInUserRole , setLoggedInUserRole ] = useState ( '' ) ;
7+ const [ modalIsOpen , setModalIsOpen ] = useState ( false ) ;
8+ const [ formData , setFormData ] = useState ( { title : '' , desc : '' , docs : '' } ) ;
79 const host = process . env . REACT_APP_BACKEND_URL ;
810
911 useEffect ( ( ) => {
1012 const fetchData = async ( ) => {
1113 try {
1214 const accessToken = localStorage . getItem ( 'accessToken' ) ;
1315 const headers = { Authorization : `Bearer ${ accessToken } ` } ;
14-
16+
1517 const userRoleResponse = await axios . get ( `${ host } /user-details/` , { headers } ) ;
1618 setLoggedInUserRole ( userRoleResponse . data . role ) ;
1719
@@ -35,11 +37,49 @@ function AnnouncementsPlacement() {
3537 }
3638 } ;
3739
40+ const handleAddAnnouncement = async ( ) => {
41+ try {
42+ const accessToken = localStorage . getItem ( 'accessToken' ) ;
43+ const headers = { Authorization : `Bearer ${ accessToken } ` } ;
44+ await axios . post ( `${ host } /placements/announcement/` , formData , { headers } ) ;
45+ setModalIsOpen ( false ) ;
46+ setFormData ( { title : '' , desc : '' } ) ;
47+ const updatedAnnouncements = await axios . get ( `${ host } /placements/announcement/` , { headers } ) ;
48+ setAnnouncements ( updatedAnnouncements . data ) ;
49+ } catch ( error ) {
50+ console . error ( 'Error adding announcement:' , error ) ;
51+ }
52+ } ;
53+
54+ const handleInputChange = ( e ) => {
55+ const { name, value } = e . target ;
56+ setFormData ( ( prevFormData ) => ( { ...prevFormData , [ name ] : value } ) ) ;
57+ } ;
58+
59+ const handleSubmit = ( e ) => {
60+ e . preventDefault ( ) ;
61+ handleAddAnnouncement ( ) ;
62+ } ;
63+
64+ const getFileNameFromPath = ( path ) => {
65+ if ( path ) {
66+ const parts = path . split ( '/' ) ;
67+ return parts [ parts . length - 1 ] ;
68+ }
69+ return '' ;
70+ } ;
71+
3872 return (
3973 < div className = "container mx-auto px-4 py-8" >
4074 < h2 className = "text-4xl font-semibold mb-4" > Announcements</ h2 >
75+ { loggedInUserRole === 'PLACEMENTOFFICER' && (
76+ < button onClick = { ( ) => setModalIsOpen ( true ) } className = "bg-blue-500 text-white px-2 py-1 rounded-md mb-4" >
77+ Add New Announcement
78+ </ button >
79+ ) }
4180 < div className = "my-4" >
4281 < table className = "table-auto w-full border-collapse border border-gray-800" >
82+ { /* Table Header */ }
4383 < thead >
4484 < tr className = "bg-gray-200" >
4585 < th className = "border border-gray-800 px-4 py-2" > Title</ th >
@@ -49,14 +89,22 @@ function AnnouncementsPlacement() {
4989 < th className = "border border-gray-800 px-4 py-2" > Actions</ th >
5090 </ tr >
5191 </ thead >
92+ { /* Table Body */ }
5293 < tbody >
5394 { announcements . map ( ( announcement ) => (
5495 < tr key = { announcement . id } >
5596 < td className = "border border-gray-800 px-4 py-2" > { announcement . title } </ td >
5697 < td className = "border border-gray-800 px-4 py-2" > { announcement . desc } </ td >
5798 < td className = "border border-gray-800 px-4 py-2" > { new Date ( announcement . date ) . toLocaleString ( ) } </ td >
5899 < td className = "border border-gray-800 px-4 py-2" >
59- < a href = { announcement . docs } target = "_blank" rel = "noopener noreferrer" > View Documents</ a >
100+ < a href = { announcement . docs } target = "_blank" rel = "noopener noreferrer" >
101+ {
102+ ( announcement . docs )
103+ ? < p className = 'text-blue-600 hover:underline' > { getFileNameFromPath ( announcement . docs ) } </ p >
104+ :
105+ "no doc"
106+ }
107+ </ a >
60108 </ td >
61109 < td className = "border border-gray-800 px-4 py-2" >
62110 < button
@@ -71,6 +119,42 @@ function AnnouncementsPlacement() {
71119 </ tbody >
72120 </ table >
73121 </ div >
122+ { modalIsOpen && (
123+ < div className = "fixed inset-0 flex items-center justify-center z-50" >
124+ < div className = "absolute inset-0 bg-gray-900 opacity-75" > </ div >
125+ < div className = "bg-white p-8 rounded-md z-50 relative" >
126+ < h2 className = "text-2xl font-bold mb-4" > Add New Announcement</ h2 >
127+ < form onSubmit = { handleSubmit } >
128+ < input
129+ type = "text"
130+ name = "title"
131+ value = { formData . title }
132+ onChange = { handleInputChange }
133+ placeholder = "Title"
134+ className = "block w-full px-4 py-2 border border-gray-300 rounded-md mb-4"
135+ />
136+ < textarea
137+ name = "desc"
138+ value = { formData . desc }
139+ onChange = { handleInputChange }
140+ placeholder = "Description"
141+ className = "block w-full px-4 py-2 border border-gray-300 rounded-md mb-4"
142+ > </ textarea >
143+ < div className = "flex justify-end" >
144+ < button type = "submit" className = "bg-blue-500 text-white px-4 py-2 rounded-md mr-2" >
145+ Add Announcement
146+ </ button >
147+ < button
148+ onClick = { ( ) => setModalIsOpen ( false ) }
149+ className = "bg-gray-500 text-white px-4 py-2 rounded-md"
150+ >
151+ Cancel
152+ </ button >
153+ </ div >
154+ </ form >
155+ </ div >
156+ </ div >
157+ ) }
74158 </ div >
75159 ) ;
76160}
0 commit comments