Advertisement
jnhaile

App.js

Mar 11th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createStackNavigator } from '@react-navigation/stack';
  2. import { createAppContainer } from '@react-navigation/native';
  3. import React, { useReducer, useMemo, useEffect, createContext } from 'react';
  4. import { StyleSheet } from 'react-native';
  5. import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
  6. //import * as React from "react";
  7. import AsyncStorage from '@react-native-async-storage/async-storage';
  8. import { Button, Text, TextInput, View } from 'react-native';
  9. import { NavigationContainer } from '@react-navigation/native';
  10.  
  11. import LoginScreen from './pages/LoginScreen';
  12. import TabContainer from './pages/TabContainer';
  13. import HomeScreen from './pages/HomeScreen';
  14. import CalendarScreen from './pages/CalendarScreen';
  15. import PlanScreen from './pages/PlanScreen';
  16. import ContactScreen from './pages/ContactScreen';
  17. import InformationScreen from './pages/InformationScreen';
  18.  
  19. const AuthContext = React.createContext();
  20. const Stack = createStackNavigator();
  21.  
  22. const styles = StyleSheet.create({ //I believe we will be able to create default styles that should make the code more readable later
  23. InputView: {
  24.   flex: 1,
  25.   justifyContent: "center",
  26.   alignItems: "center"
  27. }
  28. });
  29.  
  30. function SplashScreen() {
  31.   return (
  32.     <View>
  33.       <Text>Loading...</Text>
  34.     </View>
  35.   );
  36. }
  37.  
  38. export default function App({ navigation })
  39. {
  40.   const [state, dispatch] = React.useReducer(
  41.     (prevState, action) => {
  42.       switch (action.type) {
  43.         case 'RESTORE_TOKEN':
  44.           return {
  45.             ...prevState,
  46.             userToken: action.token,
  47.             isLoading: false,
  48.           };
  49.         case 'SIGN_IN':
  50.           return {
  51.             ...prevState,
  52.             isSignout: false,
  53.             userToken: action.token,
  54.           };
  55.         case 'SIGN_OUT':
  56.           return {
  57.             ...prevState,
  58.             isSignout: true,
  59.             userToken: null,
  60.           };
  61.       }
  62.     },
  63.     {
  64.       isLoading: true,
  65.       isSignout: false,
  66.       userToken: null,
  67.     }
  68.   );
  69.  
  70.   React.useEffect(() => {
  71.       const bootstrapAsync = async () => {
  72.         let userToken;
  73.  
  74.         try {
  75.           userToken = await AsyncStorage.getItem('userToken');
  76.         } catch (e) {
  77.           //Restoring Token Failed
  78.         }
  79.  
  80.         dispatch({ type: 'RESTORE_TOKEN', token: userToken});
  81.       };
  82.  
  83.       bootstrapAsync();
  84.     }, []);
  85.  
  86.   const authContext = React.useMemo(
  87.     () => ({
  88.       signIn: async data => {
  89.         //THIS IS WHERE SERVER ACCESS AND AUTHENTICATION WOULD OCCUR, WE DO NOT HAVE THIS YET
  90.  
  91.         dispatch({ type: 'SIGN_IN', token: 'dummy_auth_token' });
  92.       },
  93.       signOut: () => dispatch({ type: 'SIGN_OUT'}),
  94.       signUp: async data => {
  95.         //THIS IS WHERE A USER WOULD BE DIRECTED TOWARDS A SERVER TO RECIEVE A TOKEN TO LOGIN WITH
  96.         dispatch({ type: 'SIGN_IN', token: 'dummy_auth_token' });
  97.       },
  98.     }),
  99.     []
  100.   );
  101.  
  102.   return(
  103.     <AuthContext.Provider value={authContext}>
  104.       <NavigationContainer>
  105.         <Stack.Navigator>
  106.           {state.isLoading ? (
  107.             <Stack.Screen name="Splash" component={SplashScreen} />
  108.             ) : state.userToken == null ? (
  109.               <Stack.Screen name='Login' component={LoginScreen} options={{ headerShown: false, animationTypeForReplace: state.isSignout ? 'pop' : 'push' }}/>
  110.             ) : (
  111.               <Stacl.Screen name="Main" component={TabContainer} options={{ headerShown: false }} />
  112.             )}
  113.         </Stack.Navigator>
  114.       </NavigationContainer>
  115.     </AuthContext.Provider>
  116.     );
  117. }
  118.  
  119. //export default App; //App is the default page that will show up on loading in, I believe it takes the uppermost Tab defined in the "App" function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement