@@ -12,6 +12,29 @@ const CreateChatModal = ({ onClose, onChatCreated }) => {
1212 fetchUsers ( ) ;
1313 } , [ ] ) ;
1414
15+ const [ currentUser , setCurrentUser ] = useState ( null ) ;
16+
17+ const fetchCurrentUser = async ( ) => {
18+ const token = sessionStorage . getItem ( "token" ) ;
19+ if ( ! token ) return ;
20+
21+ try {
22+ const response = await fetch ( `${ import . meta. env . VITE_BACKEND_URL } /api/users` , {
23+ method : "POST" ,
24+ headers : {
25+ "Authorization" : `${ token } `
26+ }
27+ } ) ;
28+
29+ if ( response . ok ) {
30+ const userData = await response . json ( ) ;
31+ setCurrentUser ( userData ) ;
32+ }
33+ } catch ( error ) {
34+ console . error ( 'Error fetching current user:' , error ) ;
35+ }
36+ } ;
37+
1538 const fetchUsers = async ( ) => {
1639 const token = sessionStorage . getItem ( "token" ) ;
1740 if ( ! token ) return ;
@@ -25,13 +48,26 @@ const CreateChatModal = ({ onClose, onChatCreated }) => {
2548
2649 if ( response . ok ) {
2750 const usersData = await response . json ( ) ;
28- setUsers ( usersData ) ;
51+ const filteredUsers = chatType === 'group'
52+ ? usersData . filter ( user => user . id !== currentUser ?. id )
53+ : usersData ;
54+ setUsers ( filteredUsers ) ;
2955 }
3056 } catch ( error ) {
3157 console . error ( 'Error fetching users:' , error ) ;
3258 }
3359 } ;
3460
61+ useEffect ( ( ) => {
62+ fetchCurrentUser ( ) ;
63+ } , [ ] ) ;
64+
65+ useEffect ( ( ) => {
66+ if ( currentUser ) {
67+ fetchUsers ( ) ;
68+ }
69+ } , [ currentUser , chatType ] ) ;
70+
3571 const handleUserToggle = ( userId ) => {
3672 if ( chatType === 'direct' ) {
3773 setSelectedUsers ( [ userId ] ) ;
@@ -104,7 +140,10 @@ const CreateChatModal = ({ onClose, onChatCreated }) => {
104140 type = "radio"
105141 value = "direct"
106142 checked = { chatType === 'direct' }
107- onChange = { ( e ) => setChatType ( e . target . value ) }
143+ onChange = { ( e ) => {
144+ setChatType ( e . target . value ) ;
145+ setSelectedUsers ( [ ] ) ;
146+ } }
108147 />
109148 Direct Message
110149 </ label >
@@ -113,7 +152,10 @@ const CreateChatModal = ({ onClose, onChatCreated }) => {
113152 type = "radio"
114153 value = "group"
115154 checked = { chatType === 'group' }
116- onChange = { ( e ) => setChatType ( e . target . value ) }
155+ onChange = { ( e ) => {
156+ setChatType ( e . target . value ) ;
157+ setSelectedUsers ( [ ] ) ;
158+ } }
117159 />
118160 Group Chat
119161 </ label >
0 commit comments