Skip to content

Commit

Permalink
Merge branch 'main' into feature/translate
Browse files Browse the repository at this point in the history
  • Loading branch information
hee-suh authored Mar 15, 2022
2 parents c10387d + 82ab55d commit 5dc188d
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 160 deletions.
98 changes: 51 additions & 47 deletions react-native/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import AppLoading from 'expo-app-loading';
import useFonts from './hooks/useFonts';
import { theme } from './core/theme';

import { AuthProvider } from './contexts/Auth';

import LoginScreen from './screens/LoginScreen';
import JoinScreen from './screens/JoinScreen';
import HomeScreen from './screens/HomeScreen';
Expand Down Expand Up @@ -36,52 +38,54 @@ export default function App() {
}

return (
<NativeBaseProvider theme={nativeBaseTheme}>
<NavigationContainer>
<Stack.Navigator
initialRouteName="Login"
>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="Join"
component={JoinScreen}
/>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
headerStyle: { backgroundColor: theme.colors.primary },
title: "NotiNote",
headerBackVisible: false,
headerRight: () => <LogoutButton/>,

headerTitle: (props) => ( // App Logo
<Image
style={{ width: 90, height: 50 }}
source={require('./assets/images/notinote-icon-white.png')}
resizeMode='contain'
/>
),
}}
/>
<Stack.Screen
name="Translate"
component={TranslateScreen}
/>
<Stack.Screen
name="Search"
component={SearchScreen}
/>
<Stack.Screen
name="SearchResult"
component={SearchResultScreen}
/>
</Stack.Navigator>
</NavigationContainer>
</NativeBaseProvider>
<AuthProvider>
<NativeBaseProvider theme={nativeBaseTheme}>
<NavigationContainer>
<Stack.Navigator
initialRouteName="Login"
>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="Join"
component={JoinScreen}
/>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
headerStyle: { backgroundColor: theme.colors.primary },
title: "NotiNote",
headerBackVisible: false,
headerRight: () => <LogoutButton/>,

headerTitle: (props) => ( // App Logo
<Image
style={{ width: 90, height: 50 }}
source={require('./assets/images/notinote-icon-white.png')}
resizeMode='contain'
/>
),
}}
/>
<Stack.Screen
name="Translate"
component={TranslateScreen}
/>
<Stack.Screen
name="Search"
component={SearchScreen}
/>
<Stack.Screen
name="SearchResult"
component={SearchResultScreen}
/>
</Stack.Navigator>
</NavigationContainer>
</NativeBaseProvider>
</AuthProvider>
);
}
6 changes: 3 additions & 3 deletions react-native/components/LogoutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import React, { useState } from 'react';
import { TouchableOpacity, Alert } from 'react-native';
import { useNavigation, StackActions } from '@react-navigation/native';
import { AntDesign } from '@expo/vector-icons';
import { useAuth } from '../contexts/Auth';

const LogoutButton = () => {
const navigation = useNavigation();
const auth = useAuth();
const [showBox, setShowBox] = useState(true);

const LogoutConfirm = () => {
Expand All @@ -15,9 +17,7 @@ const LogoutButton = () => {
text: "Yes",
onPress: () => {
setShowBox(false);
// await fetch('https://fetch.url/logout').then(
// response => {
// console.log(response);
auth.signOut();
navigation.dispatch(StackActions.popToTop())
},
}, {
Expand Down
99 changes: 99 additions & 0 deletions react-native/contexts/Auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { createContext, useState, useContext, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';

import { authService } from '../services/authService';
import { AuthData, JoinData } from '../types';

interface AuthContextData {
authData?: AuthData;
loading: boolean;
signUp(data: JoinData): Promise<void>;
signIn(accessToken: string): Promise<void>;
signOut(): void;
};

// Create the Auth Context with the data type specified
// and a empty object
const AuthContext = createContext<AuthContextData>({} as AuthContextData);

const AuthProvider: React.FC = ({ children }) => {
const [authData, setAuthData] = useState<AuthData>();

// the AuthContext start with loading equals true
// and stay like this, until the data be load from Async Storage
const [loading, setLoading] = useState(true);

useEffect(() => {
// Every time the App is opened, this provider is rendered
// and call de loadStorage function.
loadStorageData();
}, []);

async function loadStorageData(): Promise<void> {
try {
//Try get the data from Async Storage
const authDataSerialized = await AsyncStorage.getItem('@AuthData');
if (authDataSerialized) {
//If there are data, it's converted to an Object and the state is updated.
const _authData: AuthData = JSON.parse(authDataSerialized);
setAuthData(_authData);
}
} catch (error) {
} finally {
// loading finished
setLoading(false);
}
}

const signUp = async (data: JoinData) => {
const _authData = await authService.signUp(data);

setAuthData(_authData);

AsyncStorage.setItem('@AuthData', JSON.stringify(_authData));
};

const signIn = async (accessToken: string) => {
const _authData = await authService.signIn(accessToken);

// Set the data in the context, so the App can be notified
// and send the user to the AuthStack
setAuthData(_authData);

// Persist the data in the Async Storage
// to be recovered in the next user session.
AsyncStorage.setItem('@AuthData', JSON.stringify(_authData));
};

const signOut = async () => {
// Remove data from context, so the App can be notified
// and send the user to the AuthStack
setAuthData(undefined);

// Remove the data from Async Storage
// to NOT be recoverede in next session.
await AsyncStorage.removeItem('@AuthData');
};

return (
// This component will be used to encapsulate the whole App,
// so all components will have access to the Context
<AuthContext.Provider value={{authData, loading, signUp, signIn, signOut}}>
{children}
</AuthContext.Provider>
);
};

// A simple hooks to facilitate the access to the AuthContext
// and permit components to subscribe to AuthContext updates
function useAuth(): AuthContextData {
const context = useContext(AuthContext);

if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}

return context;
}

export {AuthContext, AuthProvider, useAuth};
6 changes: 3 additions & 3 deletions react-native/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const emailValidator = (email: string) => {
export const emailValidator = (email: string | undefined) => {
const re = /\S+@\S+\.\S+/;

if (!email || email.length <= 0) return 'Email cannot be empty.';
Expand All @@ -7,13 +7,13 @@ export const emailValidator = (email: string) => {
return '';
};

export const passwordValidator = (password: string) => {
export const passwordValidator = (password: string | undefined) => {
if (!password || password.length <= 0) return 'Password cannot be empty.';

return '';
};

export const nameValidator = (name: string) => {
export const nameValidator = (name: string | undefined) => {
if (!name || name.length <= 0) return 'Name cannot be empty.';

return '';
Expand Down
8 changes: 3 additions & 5 deletions react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
},
"dependencies": {
"@expo-google-fonts/lora": "^0.2.2",
"@react-native-async-storage/async-storage": "~1.15.0",
"@react-navigation/native": "^6.0.8",
"@react-navigation/native-stack": "^6.5.0",
"axios": "^0.26.1",
"expo": "~44.0.0",
"expo-app-loading": "~1.3.0",
"expo-application": "^4.0.2",
Expand Down Expand Up @@ -42,18 +44,14 @@
"react-native-vector-icons": "^9.1.0",
"react-native-web": "0.17.1",
"react-navigation": "^4.4.4",
"react-navigation-stack": "^2.10.4",
"react-redux": "^7.2.6",
"redux": "^4.1.2"
"react-navigation-stack": "^2.10.4"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@types/mime": "^2.0.3",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"@types/react-native-dotenv": "^0.2.0",
"@types/react-redux": "^7.1.22",
"@types/redux": "^3.6.0",
"typescript": "~4.3.5"
},
"private": true
Expand Down
Loading

0 comments on commit 5dc188d

Please sign in to comment.