forked from hashgit/react-native-linkedin
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.tsx
158 lines (145 loc) · 3.55 KB
/
App.tsx
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
import React from 'react'
import {
StyleSheet,
View,
Text,
Button,
ActivityIndicator,
StatusBar,
} from 'react-native'
import { CLIENT_ID, CLIENT_SECRET, REDIRECT_URL } from './config'
import LinkedInModal, { LinkedInToken } from './src/'
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center',
},
userContainer: {
width: '100%',
padding: 10,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center',
},
picture: {
width: 200,
height: 200,
borderRadius: 100,
resizeMode: 'cover',
marginBottom: 15,
},
item: {
width: '100%',
flexDirection: 'row',
marginVertical: 5,
justifyContent: 'center',
alignItems: 'center',
},
label: {
marginRight: 10,
},
value: {
fontWeight: 'bold',
marginLeft: 10,
},
linkedInContainer: {
justifyContent: 'center',
alignItems: 'center',
},
labelContainer: {
alignItems: 'flex-end',
},
valueContainer: {
justifyContent: 'center',
alignItems: 'flex-start',
},
})
interface State {
access_token?: string
expires_in?: number
refreshing: boolean
localizedFirstName?: string
message?: string
}
export default class AppContainer extends React.Component<{}, State> {
state = {
access_token: undefined,
expires_in: undefined,
refreshing: false,
localizedFirstName: undefined,
message: undefined,
}
modal = React.createRef<LinkedInModal>()
constructor(props: any) {
super(props)
StatusBar.setHidden(true)
}
getUser = async (data: LinkedInToken) => {
const { access_token, authentication_code } = data
if (!authentication_code) {
this.setState({ refreshing: true })
const response = await fetch('https://api.linkedin.com/v2/me', {
method: 'GET',
headers: {
Authorization: 'Bearer ' + access_token,
},
})
const payload = await response.json()
this.setState({ ...payload, refreshing: false })
} else {
alert(`authentication_code = ${authentication_code}`)
}
}
renderItem(label: string, value: string) {
return value ? (
<View style={styles.item}>
<View style={styles.labelContainer}>
<Text style={styles.label}>{label}</Text>
</View>
<Text>👉</Text>
<View style={styles.valueContainer}>
<Text style={styles.value}>{value}</Text>
</View>
</View>
) : null
}
signOut = () => {
this.setState({ refreshing: true })
this.modal.current
.logoutAsync()
.then(() =>
this.setState({ localizedFirstName: undefined, refreshing: false }),
)
}
render() {
const { refreshing, localizedFirstName } = this.state
return (
<View style={styles.container}>
<View style={styles.linkedInContainer}>
<LinkedInModal
ref={this.modal}
clientID={CLIENT_ID}
clientSecret={CLIENT_SECRET}
redirectUri={REDIRECT_URL}
onSuccess={this.getUser}
/>
<Button
title="Open from external"
onPress={() => this.modal.current.open()}
/>
</View>
{refreshing && <ActivityIndicator size="large" />}
{localizedFirstName && (
<>
<View style={styles.userContainer}>
{this.renderItem('Last name', localizedFirstName)}
</View>
<Button title="Log Out" onPress={this.signOut} />
</>
)}
</View>
)
}
}