onion/js/components/ascribe_social_share/facebook_share_button.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-11-09 17:46:49 +01:00
'use strict';
import React from 'react';
2016-01-19 09:10:33 +01:00
import FacebookHandler from '../../third_party/facebook_handler';
2016-01-06 15:13:09 +01:00
2015-11-09 17:46:49 +01:00
import AppConstants from '../../constants/application_constants';
import { InjectInHeadUtils } from '../../utils/inject_utils';
let FacebookShareButton = React.createClass({
propTypes: {
type: React.PropTypes.string
},
getDefaultProps() {
return {
type: 'button'
};
},
2016-01-06 15:13:09 +01:00
getInitialState() {
return FacebookHandler.getState();
},
2015-11-09 17:46:49 +01:00
componentDidMount() {
2016-01-06 15:13:09 +01:00
FacebookHandler.listen(this.onChange);
2016-01-06 15:13:09 +01:00
this.loadFacebook();
},
shouldComponentUpdate(nextProps, nextState) {
// Don't update if the props haven't changed or the FB SDK loading status is still the same
return this.props.type !== nextProps.type || nextState.loaded !== this.state.loaded;
},
2016-01-06 15:13:09 +01:00
componentDidUpdate() {
// If the component changes, we need to reparse the share button's XFBML.
// To prevent cases where the Facebook SDK hasn't been loaded yet at this stage,
// let's make sure that it's injected before trying to reparse.
this.loadFacebook();
},
onChange(state) {
this.setState(state);
},
loadFacebook() {
InjectInHeadUtils
.inject(AppConstants.facebook.sdkUrl)
.then(() => {
if (this.state.loaded) {
FB.XFBML.parse(this.refs.fbShareButton.getDOMNode().parent)
}
});
2015-11-09 17:46:49 +01:00
},
render() {
return (
<span
ref="fbShareButton"
2015-11-09 19:01:27 +01:00
className="fb-share-button btn btn-ascribe-social"
2015-11-09 17:46:49 +01:00
data-layout={this.props.type}>
</span>
2015-11-09 17:46:49 +01:00
);
}
});
export default FacebookShareButton;