DefinitelyTyped/types/react-native-fbsdk/react-native-fbsdk-tests.tsx
2019-08-15 14:38:50 -07:00

110 lines
3.1 KiB
TypeScript

import * as React from 'react';
import * as FBSDK from 'react-native-fbsdk';
import { View } from 'react-native';
declare function alert(s: string): void;
const {
LoginButton,
AccessToken,
LoginManager,
ShareDialog,
AppEventsLogger,
GraphRequest,
GraphRequestManager,
} = FBSDK;
export class Login extends React.Component {
render() {
return (
<View>
<LoginButton
permissions={["publish_actions"]}
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
if (data) {
alert(data.accessToken.toString());
}
}
);
}
}
}
onLogoutFinished={() => alert("logout.")}
/>
</View>
);
}
}
// Attempt a login using the Facebook login dialog asking for default permissions.
LoginManager.logInWithPermissions(['public_profile']).then(
result => {
if (result.isCancelled) {
alert('Login cancelled');
} else {
alert(`Login success with permissions: ${result.grantedPermissions}`);
}
},
error => {
alert(`Login fail with error: ${error}`);
}
);
// Build up a shareable link.
const shareLinkContent: FBSDK.ShareLinkContent = {
contentType: 'link',
contentUrl: "https://facebook.com",
contentDescription: 'Wow, check out this great site!',
};
// Share the link using the share dialog.
export const shareLinkWithShareDialog = (): void => {
ShareDialog.canShow(shareLinkContent).then(
(canShow) => {
if (canShow) {
return ShareDialog.show(shareLinkContent);
}
}
).then(
(result) => {
if (result.isCancelled) {
alert('Share cancelled');
} else {
alert(`Share success with postId: ${result.postId}`);
}
},
(error: Error) => {
alert(`Share fail with error: ${error}`);
}
);
};
const obj = { param: 'value' };
AppEventsLogger.logPurchase(15, 'USD', obj);
const responseInfoCallback: FBSDK.GraphRequestCallback = (error, result) => {
if (error) {
alert(`Error fetching data: ${error}`);
} else {
alert(`Success fetching data: ${result}`);
}
};
// Create a graph request asking for user information with a callback to handle the response.
const infoRequest = new GraphRequest(
'/me',
null,
responseInfoCallback,
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start();