index.jsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { connect } from 'react-redux';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import StatusListContainer from '../ui/containers/status_list_container';
  4. import Column from '../ui/components/column';
  5. import {
  6. refreshTimeline,
  7. updateTimeline,
  8. deleteFromTimelines
  9. } from '../../actions/timelines';
  10. const HashtagTimeline = React.createClass({
  11. propTypes: {
  12. params: React.PropTypes.object.isRequired,
  13. dispatch: React.PropTypes.func.isRequired
  14. },
  15. mixins: [PureRenderMixin],
  16. _subscribe (dispatch, id) {
  17. if (typeof App !== 'undefined') {
  18. this.subscription = App.cable.subscriptions.create({
  19. channel: 'HashtagChannel',
  20. tag: id
  21. }, {
  22. received (data) {
  23. switch(data.type) {
  24. case 'update':
  25. return dispatch(updateTimeline('tag', JSON.parse(data.message)));
  26. case 'delete':
  27. return dispatch(deleteFromTimelines(data.id));
  28. }
  29. }
  30. });
  31. }
  32. },
  33. _unsubscribe () {
  34. if (typeof this.subscription !== 'undefined') {
  35. this.subscription.unsubscribe();
  36. }
  37. },
  38. componentWillMount () {
  39. const { dispatch } = this.props;
  40. const { id } = this.props.params;
  41. dispatch(refreshTimeline('tag', id));
  42. this._subscribe(dispatch, id);
  43. },
  44. componentWillReceiveProps (nextProps) {
  45. if (nextProps.params.id !== this.props.params.id) {
  46. this.props.dispatch(refreshTimeline('tag', nextProps.params.id));
  47. this._unsubscribe();
  48. this._subscribe(this.props.dispatch, nextProps.params.id);
  49. }
  50. },
  51. componentWillUnmount () {
  52. this._unsubscribe();
  53. },
  54. render () {
  55. const { id } = this.props.params;
  56. return (
  57. <Column icon='hashtag' heading={id}>
  58. <StatusListContainer type='tag' id={id} />
  59. </Column>
  60. );
  61. },
  62. });
  63. export default connect()(HashtagTimeline);