Created
September 15, 2015 12:03
-
-
Save juampynr/6fc75b2d3efba3690a38 to your computer and use it in GitHub Desktop.
Render Disqus comments in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Comments component. | |
* | |
* Displays comments from Disqus for the current URL. | |
*/ | |
'use strict'; | |
var React = require('react'); | |
var Settings = require('somepath/settings'); | |
var Comments = React.createClass({ | |
propTypes: { | |
identifier: React.PropTypes.string.isRequired, | |
title: React.PropTypes.string.isRequired | |
}, | |
componentDidMount: function() { | |
var _this = this; | |
// Set Disqus variables. | |
/*eslint-disable camelcase */ | |
global.disqus_shortname = window.location.hostname === 'www.lullabot.com' ? Settings.disqus.shortName : Settings.disqus.shortNameDev; | |
global.disqus_identifier = this.props.identifier; | |
global.disqus_title = this.props.title; | |
/*eslint-enable camelcase */ | |
// Load Disqus widget. | |
(function() { | |
// Only load the Disqus library on first load. Otherwise reset the widget. | |
/*eslint-disable camelcase */ | |
if (!document.querySelectorAll('script[src="//' + global.disqus_shortname + '.disqus.com/embed.js"]').length) { | |
/*eslint-enable camelcase */ | |
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; | |
dsq.src = '//' + global.disqus_shortname + '.disqus.com/embed.js'; | |
/*eslint-enable camelcase */ | |
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); | |
/*eslint-disable camelcase */ | |
} | |
else { | |
global.DISQUS.reset({ | |
reload: true, | |
config: function () { | |
/*eslint-enable camelcase */ | |
this.page.identifier = global.disqus_identifier; | |
/*eslint-disable camelcase */ | |
this.page.url = window.location.href; | |
this.page.title = _this.props.title; | |
} | |
}); | |
} | |
})(); | |
}, | |
render: function() { | |
return ( | |
<div className="comments"> | |
<div id="disqus_thread"></div> | |
</div> | |
); | |
} | |
}); | |
module.exports = Comments; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment