From bc56de68eb8fc69be6c5c105236d0b6ecfac4b26 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Fri, 9 Apr 2021 11:30:45 -0700 Subject: Connecting user to streamo on Routes --- src/routes/Routes.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/routes') diff --git a/src/routes/Routes.tsx b/src/routes/Routes.tsx index 819ca785..173a6a6c 100644 --- a/src/routes/Routes.tsx +++ b/src/routes/Routes.tsx @@ -1,15 +1,17 @@ +import AsyncStorage from '@react-native-community/async-storage'; import messaging from '@react-native-firebase/messaging'; -import React, {useEffect, useState} from 'react'; +import React, {useContext, useEffect, useState} from 'react'; import DeviceInfo from 'react-native-device-info'; import SplashScreen from 'react-native-splash-screen'; -import {useDispatch, useSelector} from 'react-redux'; +import {useDispatch, useSelector, useStore} from 'react-redux'; +import {ChatContext} from '../App'; import {fcmService, getCurrentLiveVersions} from '../services'; import { updateNewNotificationReceived, updateNewVersionAvailable, } from '../store/actions'; import {RootState} from '../store/rootReducer'; -import {userLogin} from '../utils'; +import {userLogin, connectChatAccount} from '../utils'; import Onboarding from './onboarding'; import NavigationBar from './tabs'; @@ -17,6 +19,9 @@ const Routes: React.FC = () => { const { user: {userId}, } = useSelector((state: RootState) => state.user); + const state: RootState = useStore().getState(); + const loggedInUserId = state.user.user.userId; + const {chatClient} = useContext(ChatContext); const [newVersionAvailable, setNewVersionAvailable] = useState(false); const dispatch = useDispatch(); @@ -49,6 +54,7 @@ const Routes: React.FC = () => { if (userId) { fcmService.setUpPushNotifications(); fcmService.sendFcmTokenToServer(); + connectChatAccount(loggedInUserId, chatClient, dispatch); } }, []); -- cgit v1.2.3-70-g09d2 From bdd1ed17600da7b766e2b0fa97ad4cbf01234819 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Fri, 9 Apr 2021 13:46:42 -0700 Subject: removed client ready from redux --- src/components/profile/ProfileBody.tsx | 6 ++-- src/routes/Routes.tsx | 2 +- src/screens/chat/ChatListScreen.tsx | 51 +++++++++++++++------------------- src/store/actions/user.ts | 15 ---------- src/store/initialStates.ts | 1 - src/utils/messages.ts | 4 +-- 6 files changed, 28 insertions(+), 51 deletions(-) (limited to 'src/routes') diff --git a/src/components/profile/ProfileBody.tsx b/src/components/profile/ProfileBody.tsx index dc68446b..e23249fa 100644 --- a/src/components/profile/ProfileBody.tsx +++ b/src/components/profile/ProfileBody.tsx @@ -63,7 +63,6 @@ const ProfileBody: React.FC = ({ profile, ); - const {chatClientReady} = useSelector((state: RootState) => state.user); const {chatClient, setChannel} = useContext(ChatContext); const state: RootState = useStore().getState(); @@ -97,8 +96,9 @@ const ProfileBody: React.FC = ({ }; const onPressMessage = async () => { - if (!chatClientReady) { - Alert.alert('Something wrong with chat'); + if (!chatClient.user) { + // TODO: Add refresh control to retry establishing chat connection + Alert.alert('Unable to connect chat'); } const channel = chatClient.channel('messaging', { members: [loggedInUserId, String(userXId)], diff --git a/src/routes/Routes.tsx b/src/routes/Routes.tsx index 173a6a6c..adc6253b 100644 --- a/src/routes/Routes.tsx +++ b/src/routes/Routes.tsx @@ -54,7 +54,7 @@ const Routes: React.FC = () => { if (userId) { fcmService.setUpPushNotifications(); fcmService.sendFcmTokenToServer(); - connectChatAccount(loggedInUserId, chatClient, dispatch); + connectChatAccount(loggedInUserId, chatClient); } }, []); diff --git a/src/screens/chat/ChatListScreen.tsx b/src/screens/chat/ChatListScreen.tsx index dbdb7994..eb886232 100644 --- a/src/screens/chat/ChatListScreen.tsx +++ b/src/screens/chat/ChatListScreen.tsx @@ -31,9 +31,6 @@ interface ChatListScreenProps { */ const ChatListScreen: React.FC = () => { const {chatClient} = useContext(ChatContext); - const chatClientReady = useSelector( - (state: RootState) => state.user.chatClientReady, - ); const state: RootState = useStore().getState(); const loggedInUserId = state.user.user.userId; @@ -65,31 +62,29 @@ const ChatListScreen: React.FC = () => { channel.create(); }} /> - {chatClientReady && ( - - - - filters={memoizedFilters} - options={{ - presence: true, - state: true, - watch: true, - }} - sort={{last_message_at: -1}} - maxUnreadCount={99} - Preview={ChannelPreview} - /> - - - )} + + + + filters={memoizedFilters} + options={{ + presence: true, + state: true, + watch: true, + }} + sort={{last_message_at: -1}} + maxUnreadCount={99} + Preview={ChannelPreview} + /> + + diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index 0ed57fe6..c7d0d5a7 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -11,7 +11,6 @@ import {getTokenOrLogout} from '../../utils'; import { clearHeaderAndProfileImages, profileCompletionStageUpdated, - setChatClientReady, setIsOnboardedUser, setNewNotificationReceived, setNewVersionAvailable, @@ -235,17 +234,3 @@ export const suggestedPeopleAnimatedTutorialFinished = ( } }; -export const updateChatClientReady = ( - chatClientReady: boolean, -): ThunkAction, RootState, unknown, Action> => async ( - dispatch, -) => { - try { - dispatch({ - type: setChatClientReady.type, - payload: {chatClientReady}, - }); - } catch (error) { - console.log(error); - } -}; diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index 546c57a9..02331eb6 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -41,7 +41,6 @@ export const EMPTY_PROFILE_PREVIEW_LIST = []; export const NO_USER_DATA = { user: NO_USER, - chatClientReady: false, profile: NO_PROFILE, avatar: undefined, cover: undefined, diff --git a/src/utils/messages.ts b/src/utils/messages.ts index b2162d34..1c83ca9f 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -1,6 +1,6 @@ import AsyncStorage from '@react-native-community/async-storage'; import moment from 'moment'; -import {updateChatClientReady} from '../store/actions'; +// import {updateChatClientReady} from '../store/actions'; import {AppDispatch} from '../store/configureStore'; import {RootState} from '../store/rootReducer'; import {ChannelGroupedType} from '../types'; @@ -98,9 +98,7 @@ export const connectChatAccount = async ( }, chatToken, ); - dispatch(updateChatClientReady(true)); } catch (err) { - dispatch(updateChatClientReady(false)); console.log('Error while connecting user to Stream: ', err); } }; -- cgit v1.2.3-70-g09d2 From 3d26676ea1e818b585b4b224f643524994a0e893 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Fri, 9 Apr 2021 14:40:03 -0700 Subject: Connecting during login, app entry, msg btn tap --- src/components/profile/ProfileBody.tsx | 23 ++++++++++++++--------- src/routes/Routes.tsx | 5 ++++- src/screens/onboarding/Login.tsx | 6 +++--- 3 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src/routes') diff --git a/src/components/profile/ProfileBody.tsx b/src/components/profile/ProfileBody.tsx index e23249fa..eebdb167 100644 --- a/src/components/profile/ProfileBody.tsx +++ b/src/components/profile/ProfileBody.tsx @@ -21,6 +21,7 @@ import {NO_PROFILE} from '../../store/initialStates'; import {RootState} from '../../store/rootReducer'; import {ScreenType} from '../../types'; import { + connectChatAccount, getUserAsProfilePreviewType, SCREEN_HEIGHT, SCREEN_WIDTH, @@ -96,16 +97,20 @@ const ProfileBody: React.FC = ({ }; const onPressMessage = async () => { - if (!chatClient.user) { - // TODO: Add refresh control to retry establishing chat connection - Alert.alert('Unable to connect chat'); + let connected: boolean = !chatClient.user; + if (!connected) { + connected = await connectChatAccount(loggedInUserId, chatClient); + if (!connected) { + Alert.alert('Unable to connect chat'); + } + } else { + const channel = chatClient.channel('messaging', { + members: [loggedInUserId, String(userXId)], + }); + channel.create(); + setChannel(channel); + navigation.navigate('Chat'); } - const channel = chatClient.channel('messaging', { - members: [loggedInUserId, String(userXId)], - }); - channel.create(); - setChannel(channel); - navigation.navigate('Chat'); }; return ( diff --git a/src/routes/Routes.tsx b/src/routes/Routes.tsx index adc6253b..5ce0c771 100644 --- a/src/routes/Routes.tsx +++ b/src/routes/Routes.tsx @@ -54,10 +54,13 @@ const Routes: React.FC = () => { if (userId) { fcmService.setUpPushNotifications(); fcmService.sendFcmTokenToServer(); - connectChatAccount(loggedInUserId, chatClient); } }, []); + useEffect(() => { + connectChatAccount(loggedInUserId, chatClient); + }, [loggedInUserId]); + useEffect(() => { const checkVersion = async () => { const liveVersions = await getCurrentLiveVersions(); diff --git a/src/screens/onboarding/Login.tsx b/src/screens/onboarding/Login.tsx index 4f2b6a64..6d9abf82 100644 --- a/src/screens/onboarding/Login.tsx +++ b/src/screens/onboarding/Login.tsx @@ -29,7 +29,7 @@ import {OnboardingStackParams} from '../../routes/onboarding'; import {fcmService} from '../../services'; import {RootState} from '../../store/rootReducer'; import {BackgroundGradientType, UniversityType} from '../../types'; -import {normalize, userLogin} from '../../utils'; +import {connectChatAccount, normalize, userLogin} from '../../utils'; import UpdateRequired from './UpdateRequired'; type VerificationScreenRouteProp = RouteProp; @@ -162,14 +162,14 @@ const Login: React.FC = ({navigation}: LoginProps) => { await AsyncStorage.setItem('token', data.token); await AsyncStorage.setItem('userId', data.UserID); await AsyncStorage.setItem('username', username); - await AsyncStorage.setItem('chatToken', data.chatToken); } if (statusCode === 200 && data.isOnboarded) { //Stores token received in the response into client's AsynStorage try { - userLogin(dispatch, {userId: data.UserID, username}, chatClient); + userLogin(dispatch, {userId: data.UserID, username}); fcmService.sendFcmTokenToServer(); + connectChatAccount(data.UserID, chatClient); } catch (err) { Alert.alert(ERROR_INVALID_LOGIN); } -- cgit v1.2.3-70-g09d2