-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchMethods.js
211 lines (190 loc) · 6.41 KB
/
BranchMethods.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, { Component } from 'react'
import { ScrollView, StyleSheet, Text, View } from 'react-native'
import Button from './Button'
import branch, { RegisterViewEvent } from 'react-native-branch'
const defaultBUO = {
title: 'wallo'
}
class BranchMethods extends Component {
buo = null
state = {
results: [],
}
componentWillUnmount() {
if (!this.buo) return
this.buo.release()
}
createBranchUniversalObject = async () => {
try {
let result = await branch.createBranchUniversalObject('abc', defaultBUO)
if (this.buo) this.buo.release()
this.buo = result
console.log('createBranchUniversalObject', result)
this.addResult('success', 'createBranchUniversalObject', result)
} catch (err) {
console.log('createBranchUniversalObject err', err.toString())
this.addResult('error', 'createBranchUniversalObject', err.toString())
}
}
generateShortUrl = async () => {
if (!this.buo) await this.createBranchUniversalObject()
try {
let result = await this.buo.generateShortUrl()
console.log('generateShortUrl', result)
this.addResult('success', 'generateShortUrl', result)
} catch (err) {
console.log('generateShortUrl err', err)
this.addResult('error', 'generateShortUrl', err.toString())
}
}
listOnSpotlight = async () => {
if (!this.buo) await this.createBranchUniversalObject()
try {
let result = await this.buo.listOnSpotlight()
console.log('listOnSpotlight', result)
this.addResult('success', 'listOnSpotlight', result)
} catch (err) {
console.log('listOnSpotlight err', err.toString())
this.addResult('error', 'listOnSpotlight', err.toString())
}
}
showShareSheet = async () => {
if (!this.buo) await this.createBranchUniversalObject()
try {
let result = await this.buo.showShareSheet()
console.log('showShareSheet', result)
this.addResult('success', 'showShareSheet', result)
} catch (err) {
console.log('showShareSheet err', err.toString())
this.addResult('error', 'showShareSheet', err.toString())
}
}
redeemRewards = async (bucket) => {
try {
let result = await branch.redeemRewards(5, bucket)
console.log('redeemRewards', result)
this.addResult('success', 'redeemRewards', result)
} catch (err) {
console.log('redeemRewards err', {...err}, err.message, err.toString())
this.addResult('error', 'redeemRewards', err.toString())
}
}
loadRewards = async() => {
try {
let result = await branch.loadRewards()
console.log('loadRewards', result)
this.addResult('success', 'loadRewards', result)
} catch (err) {
console.log('loadRewards err', err.toString())
this.addResult('error', 'loadRewards', err.toString())
}
}
getCreditHistory = async() => {
try {
let result = await branch.getCreditHistory()
console.log('getCreditHistory', result)
this.addResult('success', 'getCreditHistory', result)
} catch (err) {
console.log('getCreditHistory err', err.toString())
this.addResult('error', 'getCreditHistory', err.toString())
}
}
userCompletedAction = async() => {
if (!this.buo) await this.createBranchUniversalObject()
try {
let result = await this.buo.userCompletedAction(RegisterViewEvent)
console.log('userCompletedAction', result)
this.addResult('success', 'userCompletedAction', result)
} catch (err) {
console.log('userCompletedAction err', err.toString())
this.addResult('error', 'userCompletedAction', err.toString())
}
}
sendCommerceEvent = async() => {
try {
let result = await branch.sendCommerceEvent(20.00, {"key": "value"})
console.log('sendCommerceEvent', result)
this.addResult('success', 'sendCommerceEvent', result)
} catch (err) {
console.log('sendCommerceEvent err', err.toString())
this.addResult('error', 'sendCommerceEvent', err.toString())
}
}
addResult(type, slug, payload) {
let result = { type, slug, payload }
this.setState({
results: [result, ...this.state.results].slice(0, 10)
})
}
render() {
return (
<View style={styles.container}>
<View style={styles.resultsContainer}>
<Text style={styles.header}>RESULTS</Text>
<ScrollView style={styles.scrollContainer}>
{this.state.results.length === 0 && <Text>No Results yet, run a method below</Text>}
{this.state.results.map((result, i) => {
return (
<View key={i} style={styles.result}>
<Text style={result.type === 'success' ? styles.textSucccess : styles.textError}>{`${result.slug} (${result.type})`}</Text>
<Text style={styles.textSmall}>{JSON.stringify(result.payload, null, 2)}</Text>
</View>
)
})}
</ScrollView>
</View>
<Text style={styles.header}>METHODS</Text>
<ScrollView style={styles.buttonsContainer}>
<Button onPress={this.createBranchUniversalObject}>createBranchUniversalObject</Button>
<Button onPress={this.userCompletedAction}>userCompletedAction</Button>
<Button onPress={this.sendCommerceEvent}>sendCommerceEvent</Button>
<Button onPress={this.generateShortUrl}>generateShortUrl</Button>
<Button onPress={this.listOnSpotlight}>listOnSpotlight</Button>
<Button onPress={this.showShareSheet}>showShareSheet</Button>
<Button onPress={this.redeemRewards.bind(this, '')}>redeemRewards</Button>
<Button onPress={this.redeemRewards.bind(this, 'testBucket')}>redeemRewards (with bucket)</Button>
<Button onPress={this.loadRewards}>loadRewards</Button>
<Button onPress={this.getCreditHistory}>getCreditHistory</Button>
</ScrollView>
</View>
)
}
}
export default BranchMethods
const styles = StyleSheet.create({
container: {
marginTop: 30,
flex: 1,
},
header: {
backgroundColor: '#ccc',
padding: 5,
paddingLeft: 10,
borderTopWidth: 1,
borderBottomWidth: 1,
borderColor: '#aaa',
fontSize: 10,
fontWeight: 'bold',
},
resultsContainer: {
height: 200,
},
scrollContainer: {
padding: 5,
},
result: {
padding: 5,
},
textSmall: {
fontSize: 10,
},
textSucccess: {
color: '#6c9c5d',
},
textError: {
color: '#a03d31',
},
buttonsContainer: {
flex: 1,
}
})