|
1 | | -import Courier from "@trycourier/courier-react-native"; |
| 1 | +import Courier, { useCourier } from "@trycourier/courier-react-native"; |
2 | 2 | import React, { useEffect, useRef, useState } from "react"; |
3 | | -import { Button, Modal, StyleSheet, Text, TextInput, TouchableOpacity, View } from "react-native"; |
| 3 | +import { ActivityIndicator, Button, Modal, StyleSheet, Text, TextInput, TouchableOpacity, View } from "react-native"; |
4 | 4 |
|
5 | 5 | const Auth = () => { |
6 | 6 |
|
7 | | - const [userId, setUserId] = useState<string | undefined>(undefined) |
| 7 | + const { auth, push } = useCourier(); |
8 | 8 |
|
9 | 9 | useEffect(() => { |
10 | 10 |
|
11 | 11 | Courier.shared.iOSForegroundPresentationOptions({ |
12 | 12 | options: ['sound', 'badge', 'list', 'banner'], |
13 | 13 | }); |
14 | | - |
15 | | - setUserId(Courier.shared.userId); |
16 | 14 |
|
17 | 15 | }, []); |
18 | 16 |
|
19 | 17 | async function signIn(userId: string) { |
20 | 18 |
|
21 | 19 | try { |
22 | | - |
23 | | - await Courier.shared.signIn({ |
| 20 | + |
| 21 | + await auth?.signIn({ |
24 | 22 | accessToken: 'pk_prod_MVPCX80QWXMJ1HQMTZNBFE4ZQYJS', |
25 | 23 | clientKey: 'MWVmNzI3ZDUtZDk2NS00OGU1LThjMjQtMDgwMjlkYjM1YWYx', |
26 | 24 | userId: userId, |
27 | | - }); |
| 25 | + }) |
28 | 26 |
|
29 | | - const requestStatus = await Courier.shared.requestNotificationPermission(); |
| 27 | + const requestStatus = await push?.requestNotificationPermission(); |
30 | 28 | console.log('Request Notification Status: ' + requestStatus); |
31 | | - |
32 | | - const getStatus = await Courier.shared.getNotificationPermissionStatus(); |
33 | | - console.log('Get Notification Status: ' + getStatus); |
| 29 | + console.log('Get Notification Status: ' + push?.notificationPermissionStatus); |
34 | 30 |
|
35 | 31 | } catch (e) { |
36 | 32 |
|
37 | 33 | console.error(e); |
38 | 34 |
|
39 | 35 | } |
40 | 36 |
|
41 | | - setUserId(Courier.shared.userId); |
42 | | - |
43 | 37 | } |
44 | 38 |
|
45 | 39 | async function signOut() { |
46 | | - |
47 | | - try { |
48 | | - |
49 | | - await Courier.shared.signOut(); |
50 | | - |
51 | | - } catch (e) { |
52 | | - |
53 | | - console.error(e); |
54 | | - |
55 | | - } |
56 | | - |
57 | | - setUserId(Courier.shared.userId); |
58 | | - |
| 40 | + await auth?.signOut(); |
59 | 41 | } |
60 | 42 |
|
61 | 43 | const styles = StyleSheet.create({ |
@@ -114,7 +96,7 @@ const Auth = () => { |
114 | 96 |
|
115 | 97 | const handleButtonPress = () => { |
116 | 98 |
|
117 | | - if (userId) { |
| 99 | + if (auth?.userId) { |
118 | 100 | signOut(); |
119 | 101 | } else { |
120 | 102 | setModalVisible(true); |
@@ -163,8 +145,18 @@ const Auth = () => { |
163 | 145 |
|
164 | 146 | return ( |
165 | 147 | <View style={styles.container}> |
166 | | - {userId && <Text style={styles.text}>{userId}</Text>} |
167 | | - <AuthButton buttonText={userId ? 'Sign Out' : 'Sign In'} /> |
| 148 | + |
| 149 | + {auth?.isLoading && ( |
| 150 | + <ActivityIndicator size="small" /> |
| 151 | + )} |
| 152 | + |
| 153 | + {!auth?.isLoading && ( |
| 154 | + <> |
| 155 | + {auth?.userId && <Text style={styles.text}>{auth?.userId}</Text>} |
| 156 | + <AuthButton buttonText={auth?.userId ? 'Sign Out' : 'Sign In'} /> |
| 157 | + </> |
| 158 | + )} |
| 159 | + |
168 | 160 | </View> |
169 | 161 | ); |
170 | 162 |
|
|
0 commit comments