From 2291e3907e8978e036aa3493e89d5f2ddc1320b8 Mon Sep 17 00:00:00 2001 From: Tatiana Ubozhenko Date: Fri, 29 Sep 2023 00:19:21 +0300 Subject: [PATCH] bt-808: * fix chatId problem --- .../chat/screens/chat-list/chat-list.tsx | 16 ++++++---- mobile/src/bundles/chat/screens/chat/chat.tsx | 20 +++++++++--- .../contact-candidate/contact-candidate.tsx | 31 +++++++++---------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/mobile/src/bundles/chat/screens/chat-list/chat-list.tsx b/mobile/src/bundles/chat/screens/chat-list/chat-list.tsx index 0e0811b3a..d6a633d66 100644 --- a/mobile/src/bundles/chat/screens/chat-list/chat-list.tsx +++ b/mobile/src/bundles/chat/screens/chat-list/chat-list.tsx @@ -12,7 +12,6 @@ import { View, } from '~/bundles/common/components/components'; import { - DataStatus, RootScreenName, TextCategory, UserRole, @@ -38,9 +37,8 @@ import { styles } from './styles'; const ChatList: React.FC = () => { const dispatch = useAppDispatch(); const { currentUserData: user } = useAppSelector(({ auth }) => auth); - const { chats, current, dataStatus } = useAppSelector(({ chat }) => chat); - - const isChatsLoading = dataStatus === DataStatus.PENDING; + const { chats, current } = useAppSelector(({ chat }) => chat); + const [isDataLoaded, setDataLoaded] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const navigation = @@ -48,7 +46,13 @@ const ChatList: React.FC = () => { useEffect(() => { if (user) { - void dispatch(chatActions.getAllChatsByUserId(user.id)); + void dispatch(chatActions.getAllChatsByUserId(user.id)).then(() => { + setDataLoaded(true); + }); + + return () => { + setDataLoaded(false); + }; } }, [dispatch, user, current.messages.length]); @@ -92,7 +96,7 @@ const ChatList: React.FC = () => { [navigation], ); - if (isChatsLoading) { + if (!isDataLoaded) { return ; } diff --git a/mobile/src/bundles/chat/screens/chat/chat.tsx b/mobile/src/bundles/chat/screens/chat/chat.tsx index c552b12a3..e3b13b4ed 100644 --- a/mobile/src/bundles/chat/screens/chat/chat.tsx +++ b/mobile/src/bundles/chat/screens/chat/chat.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { ChatHeader, @@ -7,7 +7,7 @@ import { } from '~/bundles/chat/components/components'; import { actions as chatActions } from '~/bundles/chat/store'; import { type ChatMessagesCreateRequestDto } from '~/bundles/chat/types/types'; -import { FlatList, View } from '~/bundles/common/components/components'; +import { FlatList, Loader, View } from '~/bundles/common/components/components'; import { useAppDispatch, useAppRoute, @@ -23,9 +23,11 @@ import { styles } from './styles'; const Chat: React.FC = () => { const route = useAppRoute(); const dispatch = useAppDispatch(); + const { current } = useAppSelector(({ chat }) => chat); + const [isDataLoaded, setDataLoaded] = useState(false); + const { chatId, partnerName, partnerAvatar, partnerId } = route.params as ChatNavigationProperties; - const { current } = useAppSelector(({ chat }) => chat); useEffect(() => { void dispatch( @@ -33,7 +35,13 @@ const Chat: React.FC = () => { chatId, employerId: partnerId, }), - ); + ).then(() => { + setDataLoaded(true); + }); + + return () => { + setDataLoaded(false); + }; }, [dispatch, chatId, partnerId]); const handleSendMessage = useCallback( @@ -58,6 +66,10 @@ const Chat: React.FC = () => { ); }; + if (!isDataLoaded) { + return ; + } + return ( { const route = useAppRoute(); const { currentUserData } = useAppSelector(({ auth }) => auth); const { chats, dataStatus } = useAppSelector(({ chat }) => chat); - const { talentId, profileName } = - route.params as ContactTalentNavigationPropertiesType; + const isLoading = dataStatus === DataStatus.PENDING; const navigation = useNavigation>(); + const dispatch = useAppDispatch(); - const startedChat = chats.find((chat) => chat.chatId === talentId); + const { talentId } = route.params as ContactTalentNavigationPropertiesType; + + const startedChat = chats.find(({ participants }) => { + const { sender, receiver } = participants; + return ( + (sender.id === currentUserData?.id && receiver.id === talentId) || + (receiver.id === currentUserData?.id && sender.id === talentId) + ); + }); useEffect(() => { if (currentUserData?.id) { void dispatch(getAllChatsByUserId(currentUserData.id)); } - }, [currentUserData, dispatch]); + }, [currentUserData?.id, dispatch, talentId]); useEffect(() => { if (startedChat) { @@ -61,38 +69,27 @@ const ContactCandidate: React.FC = () => { }), ); } - }, [navigation, startedChat]); + }, [navigation, chats, startedChat]); const handleFormSubmit = useCallback( (payload: ContactCandidateDto): void => { if (currentUserData?.id && talentId) { void dispatch( chatActions.createMessage({ - chatId: talentId, senderId: currentUserData.id, receiverId: talentId, message: payload.message, }), ); - - navigation.dispatch( - StackActions.replace(RootScreenName.CHAT, { - partnerName: profileName, - partnerId: talentId, - chatId: talentId, - }), - ); } }, - [profileName, navigation, dispatch, currentUserData?.id, talentId], + [dispatch, currentUserData?.id, talentId], ); const handleContactClose = useCallback((): void => { navigation.goBack(); }, [navigation]); - const isLoading = dataStatus === DataStatus.PENDING; - return ( <>