From ad030d7ebb82872b69ef663c04aec7095dcbe453 Mon Sep 17 00:00:00 2001 From: renata_vl Date: Thu, 19 Dec 2024 00:37:36 +0200 Subject: [PATCH] added-redirect-to-chat --- .../user-profile/profile-info/ProfileInfo.tsx | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/containers/user-profile/profile-info/ProfileInfo.tsx b/src/containers/user-profile/profile-info/ProfileInfo.tsx index 348f58035..9e4db0e1d 100644 --- a/src/containers/user-profile/profile-info/ProfileInfo.tsx +++ b/src/containers/user-profile/profile-info/ProfileInfo.tsx @@ -23,11 +23,16 @@ import { SizeEnum, UserRoleEnum, ButtonVariantEnum, - UserResponse + UserResponse, + ChatResponse } from '~/types' import { createUrlPath, getDifferenceDates } from '~/utils/helper-functions' import { useAppDispatch } from '~/hooks/use-redux' import { openAlert } from '~/redux/features/snackbarSlice' +import { useChatContext } from '~/context/chat-context' +import useAxios from '~/hooks/use-axios' +import { useCallback } from 'react' +import { chatService } from '~/services/chat-service' interface ProfileInfoProps { userData: UserResponse @@ -37,6 +42,8 @@ interface ProfileInfoProps { const ProfileInfo = ({ userData, myRole }: ProfileInfoProps) => { const { t } = useTranslation() const navigate = useNavigate() + const { setChatInfo } = useChatContext() + const getChats = useCallback(() => chatService.getChats(), []) const { isLaptopAndAbove, isMobile } = useBreakpoints() const dispatch = useAppDispatch() const isMyProfile = useMatch(authRoutes.myProfile.path) @@ -132,6 +139,31 @@ const ProfileInfo = ({ userData, myRole }: ProfileInfoProps) => { } ].filter(Boolean) + const { + response: listOfChats, + loading: isChatsLoading, + fetchData + } = useAxios({ + service: getChats, + defaultResponse: [] + }) + + const onSendMessageClick = () => { + const existedChat = listOfChats.find((chat: ChatResponse) => + chat.members.some((member) => member.user._id == userData._id) + ) as ChatResponse | undefined + + setChatInfo({ + author: userData, + authorRole: userData.role[0] as UserRoleEnum.Student | UserRoleEnum.Tutor, + chatId: existedChat?._id || '', + updateInfo: () => {} + }) + + if (!existedChat?._id) { + void fetchData() + } + } const buttonGroup = !isMyProfile && ( {