-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
187 lines (168 loc) · 5.1 KB
/
App.js
File metadata and controls
187 lines (168 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React, {useEffect, useState} from 'react';
import {
Alert,
Image,
Platform,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import {CometChat} from '@cometchat-pro/react-native-chat';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {NavigationContainer} from '@react-navigation/native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import CreateStory from './components/story/CreateStory';
import Login from './components/login/Login';
import SignUp from './components/register/SignUp';
import SuggestedFriends from './components/friend/SuggestedFriends';
import Tabs from './components/navigation/Tabs';
import {cometChatConfig} from './env';
import Context from './context';
import logOut from './images/logout.png';
import addUser from './images/add-user.png';
import plus from './images/plus.png';
const Stack = createNativeStackNavigator();
const App = () => {
const [user, setUser] = useState(null);
const initCometChat = async () => {
const appID = `${cometChatConfig.cometChatAppId}`;
const region = `${cometChatConfig.cometChatRegion}`;
const appSetting = new CometChat.AppSettingsBuilder()
.subscribePresenceForAllUsers()
.setRegion(region)
.build();
CometChat.init(appID, appSetting).then(
() => {
console.log('CometChat was initialized successfully');
},
(error) => {},
);
};
const initAuthenticatedUser = async () => {
const authenticatedUser = await AsyncStorage.getItem('auth');
setUser(() => (authenticatedUser ? JSON.parse(authenticatedUser) : null));
};
useEffect(() => {
initCometChat();
initAuthenticatedUser();
}, []);
const goToSuggestedFriends = (navigation) => () => {
navigation.navigate('SuggestedFriends');
};
const goToCreateStory = (navigation) => () => {
navigation.navigate('CreateStory');
};
const logout = (navigation) => () => {
Alert.alert('Confirm', 'Do you want to log out?', [
{
text: 'Cancel',
style: 'cancel',
},
{text: 'OK', onPress: () => handleLogout(navigation)},
]);
};
const handleLogout = (navigation) => {
CometChat.logout().then(
() => {
removeAuthedInfo();
backToLogin(navigation);
},
(error) => {
console.log('Logout failed with exception:', {error});
},
);
};
const removeAuthedInfo = () => {
AsyncStorage.removeItem('auth');
setUser(null);
};
const backToLogin = (navigation) => {
// reset routes history and back to the login page.
navigation.reset({
index: 0,
routes: [{name: 'Login'}],
});
};
if (user) {
return (
<Context.Provider value={{user, setUser}}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Tabs}
options={({navigation}) => ({
headerTitle: () => (
<View>
<Text style={styles.headerTitle}>Snapchat</Text>
</View>
),
headerLeft: () => (
<TouchableOpacity
style={[
styles.container,
Platform.OS === 'android' ? styles.mgr8 : null,
]}
onPress={goToSuggestedFriends(navigation)}>
<Image source={addUser} style={styles.headerIcon} />
</TouchableOpacity>
),
headerRight: () => (
<View style={styles.container}>
<TouchableOpacity
style={styles.headerGap}
onPress={goToCreateStory(navigation)}>
<Image source={plus} style={styles.headerIcon} />
</TouchableOpacity>
<TouchableOpacity onPress={logout(navigation)}>
<Image source={logOut} style={styles.headerIcon} />
</TouchableOpacity>
</View>
),
})}
/>
<Stack.Screen
name="SuggestedFriends"
component={SuggestedFriends}
options={{title: 'Add Friends'}}
/>
<Stack.Screen name="CreateStory" component={CreateStory} />
</Stack.Navigator>
</NavigationContainer>
</Context.Provider>
);
}
return (
<Context.Provider value={{user, setUser}}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="SignUp" component={SignUp} />
<Stack.Screen name="Home" component={Tabs} />
</Stack.Navigator>
</NavigationContainer>
</Context.Provider>
);
};
const styles = StyleSheet.create({
container: {
display: 'flex',
flexDirection: 'row',
},
headerGap: {
marginHorizontal: 8,
},
headerTitle: {
fontSize: 24,
fontWeight: 'bold',
},
headerIcon: {
height: 24,
width: 24,
},
mgr8: {
marginRight: 8,
},
});
export default App;