about summary refs log tree commit diff
path: root/app/assets/javascripts/components/features/public_timeline/index.jsx
blob: 450725af69ff54dfa6a1d49b08dc6e77148816c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { connect }         from 'react-redux';
import PureRenderMixin     from 'react-addons-pure-render-mixin';
import ImmutablePropTypes  from 'react-immutable-proptypes';
import StatusList          from '../../components/status_list';
import Column              from '../ui/components/column';
import Immutable           from 'immutable';
import { makeGetTimeline } from '../../selectors';
import {
  updateTimeline,
  refreshTimeline,
  expandTimeline
}                          from '../../actions/timelines';
import { deleteStatus }    from '../../actions/statuses';
import { replyCompose }    from '../../actions/compose';
import {
  favourite,
  reblog,
  unreblog,
  unfavourite
}                          from '../../actions/interactions';

const makeMapStateToProps = () => {
  const getTimeline = makeGetTimeline();

  const mapStateToProps = (state) => ({
    statuses: getTimeline(state, 'public'),
    me: state.getIn(['timelines', 'me'])
  });

  return mapStateToProps;
};

const PublicTimeline = React.createClass({

  propTypes: {
    statuses: ImmutablePropTypes.list.isRequired,
    me: React.PropTypes.number.isRequired,
    dispatch: React.PropTypes.func.isRequired
  },

  mixins: [PureRenderMixin],

  componentWillMount () {
    const { dispatch } = this.props;

    dispatch(refreshTimeline('public'));

    if (typeof App !== 'undefined') {
      this.subscription = App.cable.subscriptions.create('PublicChannel', {

        received (data) {
          dispatch(updateTimeline('public', JSON.parse(data.message)));
        }

      });
    }
  },

  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
      this.subscription.unsubscribe();
    }
  },

  handleReply (status) {
    this.props.dispatch(replyCompose(status));
  },

  handleReblog (status) {
    if (status.get('reblogged')) {
      this.props.dispatch(unreblog(status));
    } else {
      this.props.dispatch(reblog(status));
    }
  },

  handleFavourite (status) {
    if (status.get('favourited')) {
      this.props.dispatch(unfavourite(status));
    } else {
      this.props.dispatch(favourite(status));
    }
  },

  handleDelete (status) {
    this.props.dispatch(deleteStatus(status.get('id')));
  },

  handleScrollToBottom () {
    this.props.dispatch(expandTimeline('public'));
  },

  render () {
    const { statuses, me } = this.props;

    return (
      <Column icon='globe' heading='Public'>
        <StatusList statuses={statuses} me={me} onScrollToBottom={this.handleScrollToBottom} onReply={this.handleReply} onReblog={this.handleReblog} onFavourite={this.handleFavourite} onDelete={this.handleDelete} />
      </Column>
    );
  },

});

export default connect(makeMapStateToProps)(PublicTimeline);