-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyNavigation.js
198 lines (192 loc) · 5.77 KB
/
MyNavigation.js
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
188
189
190
191
192
193
194
195
196
197
198
import React, { useContext } from 'react';
import { Alert, Text, TouchableOpacity, View } from 'react-native';
import { UserContext } from './contexts/user';
import LoginScreen from './screens/LoginScreen';
import HomeScreen from './screens/HomeScreen';
import CommentsScreen from './screens/CommentsScreen';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { logout } from './services/auth';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { NavigationContainer } from '@react-navigation/native';
import { ASYNC_STORAGE_KEY } from '@env';
import CreatePostScreen from './screens/CreatePostScreen';
import AddCaptionScreen from './screens/AddCaptionScreen';
import MyProfileScreen from './screens/MyProfileScreen';
import UserProfileScreen from './screens/UserProfileScreen';
import MessagesScreen from './screens/MessagesScreen';
import ChatScreen from './screens/ChatScreen';
const Stack = createNativeStackNavigator();
export default function MyNavigation() {
const [user, setUser] = useContext(UserContext);
// Async Storage => store data
const storeData = async (value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem(ASYNC_STORAGE_KEY, jsonValue);
} catch (e) {
// saving error
}
};
const showAlert = (navigation) => {
Alert.alert('Log out?', 'Are you sure, you want to log out?', [
{
text: 'No',
style: 'cancel',
},
{
text: 'Yes',
style: 'default',
onPress: () => {
logoutButton(navigation);
},
},
]);
};
const logoutButton = async (navigation) => {
const res = await logout();
if (res) {
storeData(null);
setUser(null);
}
AsyncStorage.clear();
// Resetting Login Screen Index to 0
navigation.reset({
index: 0,
routes: [{ name: 'LoginScreen' }],
});
};
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="LoginScreen">
<Stack.Screen
options={() => ({
title: 'Social Media',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
name="LoginScreen"
component={LoginScreen}
/>
<Stack.Screen
name="HomeScreen"
options={({ navigation, route }) => ({
title: ' Social Media',
headerStyle: {
backgroundColor: 'lightgreen',
},
// headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
headerRight: () => (
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity
onPress={() => showAlert(navigation)}
style={{
borderRadius: 10,
justifyContent: 'center',
padding: 5,
height: 35,
width: 80,
backgroundColor: 'wheat',
}}
>
<Text style={{ alignSelf: 'center', fontWeight: 'bold', color: 'black' }}>
LOG OUT
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate('MessagesScreen')}
style={{
borderRadius: 10,
justifyContent: 'center',
padding: 0,
height: 35,
width: 86,
backgroundColor: 'wheat',
marginHorizontal: 5,
}}
>
<Text style={{ alignSelf: 'center', fontWeight: 'bold' }}>MESSAGES</Text>
</TouchableOpacity>
</View>
),
})}
component={HomeScreen}
/>
<Stack.Screen
name="CommentsScreen"
options={() => ({
title: 'Comments',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={CommentsScreen}
/>
<Stack.Screen
name="MyProfileScreen"
options={() => ({
title: 'My Profile',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={MyProfileScreen}
/>
<Stack.Screen
name="UserProfileScreen"
options={() => ({
title: 'Social Media',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={UserProfileScreen}
/>
<Stack.Screen
name="CreatePostScreen"
options={() => ({
title: 'Take a Picture',
headerStyle: {
backgroundColor: 'lightgreen',
fontWeight: 'bold',
},
})}
component={CreatePostScreen}
/>
<Stack.Screen
name="AddCaptionScreen"
options={() => ({
title: 'Add Caption',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={AddCaptionScreen}
/>
<Stack.Screen
name="MessagesScreen"
options={() => ({
title: 'Messages',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={MessagesScreen}
/>
<Stack.Screen
name="ChatScreen"
options={() => ({
title: 'Messages',
headerStyle: {
backgroundColor: 'lightgreen',
},
})}
component={ChatScreen}
/>
</Stack.Navigator>
</NavigationContainer>
);
}