|
1 | | -import { View, Text } from 'react-native'; |
2 | | -import { HyperProvider } from '@juspay-tech/react-native-hyperswitch'; |
3 | | -import PaymentScreen from './PaymentScreen'; |
| 1 | +import { useState } from 'react'; |
| 2 | +import { View, Text, TouchableOpacity, StyleSheet } from 'react-native'; |
| 3 | +import { |
| 4 | + HyperInit, |
| 5 | +} from '@juspay-tech/react-native-hyperswitch'; |
| 6 | +import UIScreen from './UIScreen'; |
| 7 | +import HeadlessScreen from './HeadlessScreen'; |
| 8 | + |
| 9 | +type TabType = 'ui' | 'headless'; |
4 | 10 |
|
5 | 11 | export default function App() { |
| 12 | + const [activeTab, setActiveTab] = useState<TabType>('ui'); |
| 13 | + |
6 | 14 | const publishableKey = process.env.HYPERSWITCH_PUBLISHABLE_KEY; |
7 | 15 | const profileId = process.env.PROFILE_ID; |
8 | 16 |
|
9 | | - return publishableKey && profileId ? ( |
10 | | - <HyperProvider publishableKey={publishableKey} profileId={profileId}> |
11 | | - <PaymentScreen /> |
12 | | - </HyperProvider> |
13 | | - ) : ( |
14 | | - <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
15 | | - <Text>Configure env and restart Metro server</Text> |
| 17 | + const hyperPromise = |
| 18 | + publishableKey && profileId ? |
| 19 | + HyperInit(publishableKey, profileId) |
| 20 | + : null; |
| 21 | + |
| 22 | + if (!publishableKey || !profileId) { |
| 23 | + return ( |
| 24 | + <View style={styles.centerContainer}> |
| 25 | + <Text>Configure env and restart Metro server</Text> |
| 26 | + </View> |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + if (!hyperPromise) { |
| 31 | + return ( |
| 32 | + <View style={styles.centerContainer}> |
| 33 | + <Text>Initializing...</Text> |
| 34 | + </View> |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return ( |
| 39 | + <View style={styles.container}> |
| 40 | + <View style={styles.tabContainer}> |
| 41 | + <TouchableOpacity |
| 42 | + style={[styles.tab, activeTab === 'ui' && styles.activeTab]} |
| 43 | + onPress={() => setActiveTab('ui')} |
| 44 | + > |
| 45 | + <Text style={[styles.tabText, activeTab === 'ui' && styles.activeTabText]}> |
| 46 | + UI Mode |
| 47 | + </Text> |
| 48 | + </TouchableOpacity> |
| 49 | + <TouchableOpacity |
| 50 | + style={[styles.tab, activeTab === 'headless' && styles.activeTab]} |
| 51 | + onPress={() => setActiveTab('headless')} |
| 52 | + > |
| 53 | + <Text style={[styles.tabText, activeTab === 'headless' && styles.activeTabText]}> |
| 54 | + Headless Mode |
| 55 | + </Text> |
| 56 | + </TouchableOpacity> |
| 57 | + </View> |
| 58 | + |
| 59 | + <View style={styles.content}> |
| 60 | + {activeTab === 'ui' ? ( |
| 61 | + <UIScreen hyperPromise={hyperPromise} /> |
| 62 | + ) : ( |
| 63 | + <HeadlessScreen hyperPromise={hyperPromise} /> |
| 64 | + )} |
| 65 | + </View> |
16 | 66 | </View> |
17 | 67 | ); |
18 | 68 | } |
| 69 | + |
| 70 | +const styles = StyleSheet.create({ |
| 71 | + container: { |
| 72 | + flex: 1, |
| 73 | + }, |
| 74 | + centerContainer: { |
| 75 | + flex: 1, |
| 76 | + alignItems: 'center', |
| 77 | + justifyContent: 'center', |
| 78 | + }, |
| 79 | + subText: { |
| 80 | + fontSize: 12, |
| 81 | + color: '#666', |
| 82 | + marginTop: 8, |
| 83 | + }, |
| 84 | + tabContainer: { |
| 85 | + flexDirection: 'row', |
| 86 | + backgroundColor: '#f5f5f5', |
| 87 | + paddingTop: 50, |
| 88 | + borderBottomWidth: 1, |
| 89 | + borderBottomColor: '#ddd', |
| 90 | + }, |
| 91 | + tab: { |
| 92 | + flex: 1, |
| 93 | + paddingVertical: 16, |
| 94 | + alignItems: 'center', |
| 95 | + }, |
| 96 | + activeTab: { |
| 97 | + backgroundColor: '#fff', |
| 98 | + borderBottomWidth: 2, |
| 99 | + borderBottomColor: '#007AFF', |
| 100 | + }, |
| 101 | + tabText: { |
| 102 | + fontSize: 16, |
| 103 | + color: '#666', |
| 104 | + fontWeight: '500', |
| 105 | + }, |
| 106 | + activeTabText: { |
| 107 | + color: '#007AFF', |
| 108 | + fontWeight: '600', |
| 109 | + }, |
| 110 | + content: { |
| 111 | + flex: 1, |
| 112 | + }, |
| 113 | +}); |
0 commit comments