-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.js
63 lines (52 loc) · 2.11 KB
/
index.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
/**
* @file User Screen.
* @author Vadim Savin
*/
import React, { useState, useEffect } from 'react';
import { View, Text } from 'react-native';
import Purchases from 'react-native-purchases';
import { ENTITLEMENT_ID } from '../../constants';
import { LoginForm, LogoutButton, RestorePurchasesButton } from '../../components';
import styles from './styles.js';
/*
The app's user tab to display user's details like subscription status and ID's.
*/
const UserScreen = () => {
const [isAnonymous, setIsAnonymous] = useState(true);
const [userId, setUserId] = useState(null);
const [subscriptionActive, setSubscriptionActive] = useState(false);
// get the latest details about the user (is anonymous, user id, has active subscription)
const getUserDetails = async () => {
setIsAnonymous(await Purchases.isAnonymous());
setUserId(await Purchases.getAppUserID());
const customerInfo = await Purchases.getCustomerInfo();
setSubscriptionActive(typeof customerInfo.entitlements.active[ENTITLEMENT_ID] !== 'undefined');
};
useEffect(() => {
// Get user details when component first mounts
getUserDetails();
}, []);
useEffect(() => {
// Subscribe to purchaser updates
Purchases.addCustomerInfoUpdateListener(getUserDetails);
return () => {
Purchases.removeCustomerInfoUpdateListener(getUserDetails);
};
});
return (
<View style={styles.page}>
{/* The user's current app user ID and subscription status */}
<Text style={styles.headline}>Current User Identifier</Text>
<Text style={styles.userIdentifier}>{userId}</Text>
<Text style={styles.headline}>Subscription Status</Text>
<Text style={{ color: subscriptionActive ? 'green' : 'red' }}>
{subscriptionActive ? 'Active' : 'Not Active'}
</Text>
{/* Authentication UI */}
{isAnonymous ? <LoginForm onLogin={getUserDetails} /> : <LogoutButton onLogout={getUserDetails} />}
{/* You should always give users the option to restore purchases to connect their purchase to their current app user ID */}
<RestorePurchasesButton />
</View>
);
};
export default UserScreen;