-
Notifications
You must be signed in to change notification settings - Fork 327
/
index.ios.js
112 lines (94 loc) · 2.68 KB
/
index.ios.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
View,
ViewPropTypes,
StyleSheet,
NativeModules,
NativeMethodsMixin,
DeviceEventEmitter,
requireNativeComponent
} from 'react-native';
const FBLoginManager = NativeModules.MFBLoginManager;
const RCTMFBLogin = requireNativeComponent('RCTMFBLogin', FBLogin);
const styles = StyleSheet.create({
base: {
width: 175,
height: 30,
},
});
class FBLogin extends Component {
constructor (props) {
super(props);
this.bindAll = this.bindAll.bind(this);
this.bindAll();
this.statics = {
Events : FBLoginManager.Events,
};
this.state = {
credentials : null,
subscriptions : [],
}
}
bindAll() {
for( const prop in NativeMethodsMixin) {
if( typeof NativeMethodsMixin[ prop ] === 'function') {
this[prop] = NativeMethodsMixin[prop].bind(this);
}
}
}
componentWillMount(){
const subscriptions = this.state.subscriptions;
// For each event key in FBLoginManager constantsToExport
// Create listener and call event handler from props
// e.g. this.props.onError, this.props.onLogin
Object.keys(FBLoginManager.Events).forEach((event) => {
subscriptions.push(DeviceEventEmitter.addListener(
FBLoginManager.Events[event],
(eventData) => {
// event handler defined? call it and pass along any event data
let eventHandler = this.props["on"+event];
eventHandler && eventHandler(eventData);
}
));
});
// Add listeners to state
this.setState({ subscriptions : subscriptions });
}
componentWillUnmount(){
const subscriptions = this.state.subscriptions;
subscriptions.forEach(subscription => subscription.remove());
this.mounted = false;
}
componentDidMount(){
this.mounted = true;
FBLoginManager.getCredentials((error, data) => {
if( !this.mounted ) return;
if (!error) {
this.setState({ credentials : data.credentials });
} else {
this.setState({ credentials : null });
}
});
}
render() {
return <RCTMFBLogin {...this.props} style={[styles.base, this.props.style]} />
}
}
const viewPropTypes = ViewPropTypes || View.propTypes;
FBLogin.propTypes = {
style: viewPropTypes.style,
permissions: PropTypes.array, // default: ["public_profile", "email"]
loginBehavior: PropTypes.number, // default: Native
onLogin: PropTypes.func,
onLogout: PropTypes.func,
onLoginFound: PropTypes.func,
onLoginNotFound: PropTypes.func,
onError: PropTypes.func,
onCancel: PropTypes.func,
onPermissionsMissing: PropTypes.func,
};
module.exports = {
FBLogin,
FBLoginManager
};