@@ -28,6 +28,8 @@ import { createClassesFromArray, isExplicitDisconnection } from 'src/lib/utils';
2828
2929import useKeyPress , { ShortcutFlags } from 'src/hooks/useKeyPress' ;
3030import useCheckTimePassed from 'src/hooks/useCheckTimePassed' ;
31+ import { useAuth } from 'src/context/AuthContext' ;
32+ import { api } from 'src/lib/axios' ;
3133
3234const centerItems = `flex items-center justify-center` ;
3335
@@ -36,6 +38,7 @@ const Anonymous = ({
3638
3739} ) => {
3840 const { app, endSearch } = useApp ( ) ;
41+ const { authState } = useAuth ( )
3942 const { currentChatId, onlineStatus } = app ;
4043 const { clearTimer } = useCheckTimePassed ( ) ;
4144
@@ -54,7 +57,7 @@ const Anonymous = ({
5457 const typingStatusTimeoutRef = useRef ( null ) ;
5558
5659 const navigate = useNavigate ( ) ;
57- const { closeChat } = useChat ( ) ;
60+ const { messages : state , closeChat } = useChat ( ) ;
5861 const { setDialog } = useDialog ( ) ;
5962
6063 const onDisplay = useCallback ( ( { isTyping, chatId } ) => {
@@ -170,6 +173,59 @@ const Anonymous = ({
170173 } ) ;
171174 } ;
172175
176+ const blockUser = async ( ) => {
177+ // Get the other user id
178+ const chattingPartnersId = state [ currentChatId ] ?. userIds . find (
179+ id => id !== authState . loginId && id !== authState . email
180+ ) ;
181+
182+ if ( ! chattingPartnersId ) {
183+ return { success : false , message : "could not find user to block" } ;
184+ }
185+
186+ try {
187+ const res = await api . post ( '/blockUser' , {
188+ userIdToBlock : chattingPartnersId ,
189+ currentUserId : authState . loginId
190+ } ) ;
191+
192+ if ( res . status === 200 ) {
193+ return { success : true } ;
194+ } else {
195+ return { success : false , message : "Error reporting user" } ;
196+ }
197+ } catch ( error ) {
198+ console . error ( "Error in reportUser:" , error ) ;
199+ return { success : false , message : "An unexpected error occurred" } ;
200+ }
201+ }
202+
203+ const handleBlock = async ( ) => {
204+ // Check if user have an account i.e. not a anonymous user
205+ if ( authState . loginType === "anonymous" ) {
206+ setDialog ( {
207+ isOpen : true ,
208+ text : "You have to create an account first to access this feature!" ,
209+ yesBtnText : "Create an account" ,
210+ noBtnText : "Back to chat" ,
211+ handler : ( ) => navigate ( "/profile" )
212+ } )
213+ return
214+ }
215+
216+ try {
217+ const result = await blockUser ( ) ;
218+ if ( result . success ) {
219+ alert ( 'User blocked successfully' ) ;
220+ closeChatHandler ( false )
221+ } else {
222+ alert ( result . message || "Error blocking user. Please try again later." ) ;
223+ }
224+ } catch ( err ) {
225+ console . error ( "Error in handleBlock:" , err ) ;
226+ }
227+ }
228+
173229 useKeyPress ( [ 'x' ] , ( ) => handleClose ( ) , ShortcutFlags . ctrl | ShortcutFlags . shift ) ;
174230 useKeyPress ( [ 'n' ] , ( ) => handleClose ( true ) , ShortcutFlags . ctrl | ShortcutFlags . alt ) ;
175231
@@ -279,6 +335,11 @@ const Anonymous = ({
279335 < span className = "text-gray-500 text-xs" > Ctrl + Alt + N</ span >
280336 </ div >
281337 </ Dropdown . Item >
338+ < Dropdown . Item onClick = { ( ) => handleBlock ( ) } className = "sm:w-[200px]" >
339+ < div className = "flex items-center justify-between gap-2 flex-wrap" >
340+ < span className = 'text-red' > Block User</ span >
341+ </ div >
342+ </ Dropdown . Item >
282343 </ Dropdown >
283344 </ div >
284345 < div
0 commit comments