-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
135 lines (117 loc) · 3.18 KB
/
App.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
/* eslint-disable react-native/no-inline-styles */
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useCallback, useEffect, useState} from 'react';
import {
ActivityIndicator,
Pressable,
SafeAreaView,
Text,
TextInput,
View,
} from 'react-native';
import {NavigationContainer, useNavigation} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome';
import {faCoffee} from '@fortawesome/free-solid-svg-icons';
const Stack = createNativeStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Access" component={AccessScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
const HomeScreen = () => {
const navigation = useNavigation();
return (
<SafeAreaView
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Pressable onPress={() => navigation.navigate('Access')}>
<Text style={{color: 'black'}}>Access screen</Text>
</Pressable>
</SafeAreaView>
);
};
const AccessScreen = () => {
const [isLoading, setLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [data, setData] = useState(null);
useEffect(() => {
load(false);
}, []);
const load = state => {
setShowPassword(false);
setLoading(true);
setTimeout(() => {
// Do some fake network request to simulate an access check
if (state) {
setData('Success');
} else {
setShowPassword(true);
}
setLoading(false);
}, 200);
};
const onPasswordSuccess = useCallback(() => {
load(true);
}, []);
return (
<SafeAreaView
style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
{isLoading && <ActivityIndicator color={'black'} />}
{!isLoading && showPassword && <Password onSuccess={onPasswordSuccess} />}
{!isLoading && !showPassword && data && (
<Data isLoading={isLoading} data={data} />
)}
</SafeAreaView>
);
};
const Password = ({onSuccess}) => {
const [value, setValue] = useState('');
return (
<TextInput
placeholder="password"
placeholderTextColor={'gray'}
value={value}
style={{
color: 'black',
}}
onChangeText={setValue}
onSubmitEditing={onSuccess}
/>
);
};
const Data = ({isLoading, data}) => {
const navigation = useNavigation();
useEffect(() => {
if (!isLoading && data) {
navigation.setOptions({
headerRight: () => <HeaderRight />,
});
}
}, [isLoading, navigation, data]);
return <Text style={{color: 'black'}}>{data}</Text>;
};
const HeaderRight = () => {
return (
<View style={{flexDirection: 'row'}}>
<FontAwesomeIcon size={24} icon={faCoffee} color={'red'} />
<FontAwesomeIcon size={24} icon={faCoffee} color={'red'} />
<FontAwesomeIcon size={24} icon={faCoffee} color={'red'} />
</View>
);
};
export default App;