|
| 1 | +// Copyright (c) 2022, Oracle and/or its affiliates. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +import React, {useState} from 'react'; |
| 5 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 6 | +import LoginForm from './components/Login/LoginForm'; |
| 7 | +import Home from './components/Home'; |
| 8 | +import AccountDetail from './components/Accounts/AccountDetail'; |
| 9 | +import {NavigationContainer} from '@react-navigation/native'; |
| 10 | +import {createNativeStackNavigator} from '@react-navigation/native-stack'; |
| 11 | +import CloudBankMasthead from './components/UI/CloudBankMasthead'; |
| 12 | +import Transfer from './components/Transfer/Transfer'; |
| 13 | +import Deposit from './components/Deposit/Deposit'; |
| 14 | +import Payment from './components/Payment/Payment'; |
| 15 | +import {Alert} from 'react-native'; |
| 16 | + |
| 17 | +const Stack = createNativeStackNavigator(); |
| 18 | + |
| 19 | +const App = () => { |
| 20 | + // isLoggedIn is used to control whether to show the login screen or the main app ui |
| 21 | + const [isLoggedIn, setIsLoggedIn] = useState(false); |
| 22 | + // user stores the username of the logged in user |
| 23 | + const [user, setUser] = useState(''); |
| 24 | + |
| 25 | + // called when the user logs out |
| 26 | + const logoutHandler = () => { |
| 27 | + AsyncStorage.removeItem('isLoggedIn'); |
| 28 | + setIsLoggedIn(false); |
| 29 | + }; |
| 30 | + |
| 31 | + // called when the user logs in |
| 32 | + const loginHandler = (username, password, serverAddress) => { |
| 33 | + // do some very basic form validation |
| 34 | + if (username.length < 1) { |
| 35 | + Alert.alert('You must enter your username'); |
| 36 | + return; |
| 37 | + } |
| 38 | + if (password.length < 1) { |
| 39 | + Alert.alert('You must enter your password'); |
| 40 | + return; |
| 41 | + } |
| 42 | + // TODO call API to validate user exists, etc. |
| 43 | + |
| 44 | + // store the logged in state and the server address that the user entered |
| 45 | + AsyncStorage.setItem('isLoggedIn', '1'); |
| 46 | + AsyncStorage.setItem('serverAddress', serverAddress); |
| 47 | + setUser(username); |
| 48 | + setIsLoggedIn(true); |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <NavigationContainer> |
| 53 | + <Stack.Navigator |
| 54 | + screenOptions={{ |
| 55 | + headerTitle: props => <CloudBankMasthead />, |
| 56 | + }}> |
| 57 | + {isLoggedIn ? ( |
| 58 | + <> |
| 59 | + <Stack.Screen name="Home"> |
| 60 | + {props => ( |
| 61 | + <Home {...props} onLogout={logoutHandler} user={user} /> |
| 62 | + )} |
| 63 | + </Stack.Screen> |
| 64 | + <Stack.Screen name="AccountDetail"> |
| 65 | + {props => <AccountDetail {...props} />} |
| 66 | + </Stack.Screen> |
| 67 | + <Stack.Screen name="Transfer"> |
| 68 | + {props => <Transfer {...props} user={user} />} |
| 69 | + </Stack.Screen> |
| 70 | + <Stack.Screen name="Payment"> |
| 71 | + {props => <Payment {...props} user={user} />} |
| 72 | + </Stack.Screen> |
| 73 | + <Stack.Screen name="Deposit"> |
| 74 | + {props => <Deposit {...props} user={user} />} |
| 75 | + </Stack.Screen> |
| 76 | + </> |
| 77 | + ) : ( |
| 78 | + <> |
| 79 | + <Stack.Screen name="Login"> |
| 80 | + {props => <LoginForm {...props} onLogin={loginHandler} />} |
| 81 | + </Stack.Screen> |
| 82 | + </> |
| 83 | + )} |
| 84 | + </Stack.Navigator> |
| 85 | + </NavigationContainer> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default App; |
0 commit comments