Skip to content

Commit

Permalink
added-redirect-to-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Renatavl committed Dec 18, 2024
1 parent 16ca620 commit ad030d7
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/containers/user-profile/profile-info/ProfileInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 && (
<Box sx={styles.buttonGroup}>
<AppButton
Expand All @@ -148,8 +180,9 @@ const ProfileInfo = ({ userData, myRole }: ProfileInfoProps) => {
</AppButton>

<AppButton
disabled
disabled={isChatsLoading}
fullWidth
onClick={onSendMessageClick}
size={isLaptopAndAbove ? SizeEnum.ExtraLarge : SizeEnum.Medium}
variant='contained'
>
Expand Down

0 comments on commit ad030d7

Please sign in to comment.