import React from 'react';
import PropTypes from 'prop-types';
import { View } from 'react-native';
import { LoginButton } from 'react-native-fbsdk';

import { PERMISSIONS } from '../../constants/constants.js';

export default function FacebookLogin({
    doCancelAction,
    doErrorAction,
    doLogoutAction,
    doSuccessAction,
}) {
    return (
        <View>
            <LoginButton
                publishPermissions={PERMISSIONS.FACEBOOK}
                onLoginFinished={(error, result) => {
                    if (error) {
                        doErrorAction(error);
                    } else if (result.isCancelled) {
                        doCancelAction();
                    } else {
                        doSuccessAction(result);
                    }
                }}
                onLogoutFinished={doLogoutAction}
            />
        </View>
    );
}

FacebookLogin.propTypes = {
    doCancelAction: PropTypes.func.isRequired,
    doErrorAction: PropTypes.func.isRequired,
    doLogoutAction: PropTypes.func.isRequired,
    doSuccessAction: PropTypes.func.isRequired,
};
