-
Notifications
You must be signed in to change notification settings - Fork 79
/
UpsellScreen.js
147 lines (138 loc) · 4.64 KB
/
UpsellScreen.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
136
137
138
139
140
141
142
143
144
145
146
import React from "react";
import {
StyleSheet,
SafeAreaView,
Button,
ScrollView,
View,
Text,
TouchableOpacity
} from "react-native";
import Purchases from "react-native-purchases";
import { StackActions, NavigationActions } from "react-navigation";
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
alignItems: "stretch",
backgroundColor: "#F5FCFF"
},
button: {
margin: 10
},
buttons: {
flex: 2,
justifyContent: "flex-start"
},
textButton: {
color: "#f2545b"
}
});
export default class UpsellScreen extends React.Component {
constructor() {
super();
this.state = {
offerings: {},
proAnnualPrice: "Loading",
proMonthlyPrice: "Loading"
};
}
async componentDidMount() {
try {
this.purchaserInfoUpdateListener = (info) => {
checkIfPro(info, this.props.navigation);
};
this.shouldPurchasePromoProduct = async deferredPurchase => {
this.deferredPurchase = deferredPurchase;
};
Purchases.addPurchaserInfoUpdateListener(this.purchaserInfoUpdateListener);
Purchases.addShouldPurchasePromoProductListener(this.shouldPurchasePromoProduct);
const offerings = await Purchases.getOfferings();
// eslint-disable-next-line no-console
console.log(JSON.stringify(offerings));
this.setState({
offerings,
proAnnualPrice: `Buy Annual w/ Trial ${
offerings.current.annual.product.price_string
}`,
proMonthlyPrice: `Buy Monthly w/ Trial ${
offerings.current.lifetime.product.price_string
}`
});
} catch (e) {
// eslint-disable-next-line no-console
console.log("Error handling");
}
}
async componentWillUnmount() {
Purchases.removePurchaserInfoUpdateListener(this.purchaserInfoUpdateListener);
Purchases.removeShouldPurchasePromoProductListener(this.shouldPurchasePromoProduct);
}
render() {
return (
<SafeAreaView style={{ flex: 1, backgroundColor: "#fff" }}>
<ScrollView style={{ flex: 1 }}>
<View style={styles.buttons}>
<View style={styles.button}>
<Button
color="#f2545b"
onPress={async () => {
const product = this.state.offerings.current.annual.product.identifier;
try {
const purchaseMade = await Purchases.purchaseProduct(product);
checkIfPro(purchaseMade.purchaserInfo, this.props.navigation);
} catch (e) {
if (!e.userCancelled) {
// eslint-disable-next-line no-console
console.log(`Error handling ${JSON.stringify(e)}`);
} else {
// eslint-disable-next-line no-console
console.log(`User cancelled ${JSON.stringify(e)}`);
}
}
}}
title={this.state.proAnnualPrice}
/>
</View>
<View style={styles.button}>
<Button
color="#f2545b"
onPress={async () => {
const aPackage = this.state.offerings.current.lifetime;
try {
const purchaseMade = await Purchases.purchasePackage(aPackage, {oldSKU: "old", prorationMode: Purchases.PRORATION_MODE.DEFERRED}, Purchases.PURCHASE_TYPE.SUBS);
checkIfPro(purchaseMade.purchaserInfo, this.props.navigation);
} catch (e) {
if (!e.userCancelled) {
// eslint-disable-next-line no-console
console.log(`Error handling ${JSON.stringify(e)}`);
} else {
// eslint-disable-next-line no-console
console.log(`User cancelled ${JSON.stringify(e)}`);
}
}
}}
title={this.state.proMonthlyPrice}
/>
</View>
<View style={{ margin: 10, alignItems: "center" }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate("Cats")}
>
<Text style={styles.textButton}>Not now</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
</SafeAreaView>
);
}
}
function checkIfPro(purchaserInfo, navigation) {
if (typeof purchaserInfo.entitlements.active.pro_cat !== "undefined") {
navigation.dispatch(StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "Cats" })]
}));
}
}