forked from smuxx/react-native-linkedin
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApp.tsx
More file actions
153 lines (140 loc) · 3.66 KB
/
Copy pathApp.tsx
File metadata and controls
153 lines (140 loc) · 3.66 KB
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
import React, { ReactElement, useEffect, useRef, useState } 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',
},
});
export default function AppContainer(): ReactElement {
const [, setAccessToken] = useState<string | undefined>();
const [, setExpiresIn] = useState<any | undefined>();
const [refreshing, setRefreshing] = useState<boolean>(false);
const [localizedFirstName, setLocalizedFirstName] = useState<string>();
const [, setMessage] = useState<string | undefined>();
const modal = useRef<any>(null);
useEffect(() => {
StatusBar.setHidden(true);
}, []);
const getUser = async (data: LinkedInToken) => {
const { access_token, authentication_code } = data;
if (!authentication_code) {
setRefreshing(true);
const response = await fetch('https://api.linkedin.com/v2/me', {
method: 'GET',
headers: {
Authorization: 'Bearer ' + access_token,
},
});
const payload = await response.json();
setAccessToken(payload.access_token);
setExpiresIn(payload?.expires_in);
setLocalizedFirstName(payload?.localizedFirstName);
setMessage(payload?.message);
setRefreshing(false);
} else {
// eslint-disable-next-line no-alert
alert(`authentication_code = ${authentication_code}`);
}
};
const renderItem = (label: string, value: string): ReactElement => {
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>
) : (
<></>
);
};
const signOut = () => {
setRefreshing(true);
modal.current?.logoutAsync().then(() => {
setLocalizedFirstName(undefined);
setRefreshing(false);
});
};
return (
<View style={styles.container}>
<View style={styles.linkedInContainer}>
<LinkedInModal
ref={modal}
clientID={CLIENT_ID}
clientSecret={CLIENT_SECRET}
redirectUri={REDIRECT_URL}
onSuccess={getUser}
/>
<Button
title="Open from external"
onPress={() => {
modal.current?.open();
}}
/>
</View>
{refreshing && <ActivityIndicator size="large" />}
{localizedFirstName && (
<>
<View style={styles.userContainer}>
{renderItem('Last name', localizedFirstName)}
</View>
<Button title="Log Out" onPress={signOut} />
</>
)}
</View>
);
}