-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a506d6
commit ce9ddda
Showing
6 changed files
with
194 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from './hooks/hooks'; | ||
export * from './store/store'; | ||
export * from './constants/const'; | ||
export * from './slices/authSlice'; | ||
export * from './store/store'; | ||
export { default as useAxiosPrivate } from './hooks/useAxiosPrivate'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as SocketClient } from './SocketClient'; | ||
export { default as AuthRouteController } from './AuthRouteController'; | ||
export { default as SocketClient } from './SocketClient'; | ||
export { default as FetchLatestMessage } from './useFetchLatestMessage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { IChat, IMessage } from '../../../typings'; | ||
import { useAppSelector, useAxiosPrivate } from '../../app'; | ||
import { MessageRequests } from '../../app/features/requests'; | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
|
||
const FetchLatestMessage = (chat: IChat): IMessage => { | ||
const axiosInstance = useAxiosPrivate(); | ||
const messageRequests = useMemo( | ||
() => new MessageRequests(axiosInstance), | ||
[axiosInstance] | ||
); | ||
const newMessage = useAppSelector((state) => state.socketReduce.newMessage); | ||
const notifications = useAppSelector( | ||
(state) => state.notificationReduce.notifications | ||
); | ||
const [latestMessage, setLatestMessage] = useState<IMessage | undefined>( | ||
undefined | ||
); | ||
|
||
const getMessages = useCallback(async () => { | ||
if (!chat._id) return; | ||
const response = (await messageRequests.useGetLastChatMessage( | ||
chat._id as string | ||
)) as { success: boolean; data: IMessage }; | ||
setLatestMessage(response.data); | ||
}, [chat._id, messageRequests]); | ||
|
||
useEffect(() => { | ||
getMessages(); | ||
}, [getMessages, newMessage, notifications]); | ||
|
||
return latestMessage as IMessage; | ||
}; | ||
|
||
export default FetchLatestMessage; |