You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.3 KiB

  1. import { connect } from 'react-redux';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import { fetchAccount, followAccount, unfollowAccount, fetchAccountTimeline } from '../../actions/accounts';
  5. import { replyCompose } from '../../actions/compose';
  6. import { favourite, reblog } from '../../actions/interactions';
  7. import Header from './components/header';
  8. import { selectStatus } from '../../reducers/timelines';
  9. import StatusList from '../../components/status_list';
  10. import Immutable from 'immutable';
  11. function selectAccount(state, id) {
  12. return state.getIn(['timelines', 'accounts', id], null);
  13. };
  14. function selectStatuses(state, accountId) {
  15. return state.getIn(['timelines', 'accounts_timelines', accountId], Immutable.List()).map(id => selectStatus(state, id)).filterNot(status => status === null);
  16. };
  17. const mapStateToProps = (state, props) => ({
  18. account: selectAccount(state, Number(props.params.accountId)),
  19. statuses: selectStatuses(state, Number(props.params.accountId))
  20. });
  21. const Account = React.createClass({
  22. propTypes: {
  23. params: React.PropTypes.object.isRequired,
  24. dispatch: React.PropTypes.func.isRequired,
  25. account: ImmutablePropTypes.map,
  26. statuses: ImmutablePropTypes.list
  27. },
  28. mixins: [PureRenderMixin],
  29. componentWillMount () {
  30. this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
  31. this.props.dispatch(fetchAccountTimeline(Number(this.props.params.accountId)));
  32. },
  33. componentWillReceiveProps(nextProps) {
  34. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  35. this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
  36. this.props.dispatch(fetchAccountTimeline(Number(nextProps.params.accountId)));
  37. }
  38. },
  39. handleFollow () {
  40. this.props.dispatch(followAccount(this.props.account.get('id')));
  41. },
  42. handleUnfollow () {
  43. this.props.dispatch(unfollowAccount(this.props.account.get('id')));
  44. },
  45. handleReply (status) {
  46. this.props.dispatch(replyCompose(status));
  47. },
  48. handleReblog (status) {
  49. this.props.dispatch(reblog(status));
  50. },
  51. handleFavourite (status) {
  52. this.props.dispatch(favourite(status));
  53. },
  54. render () {
  55. const { account, statuses } = this.props;
  56. if (account === null) {
  57. return <div>Loading {this.props.params.accountId}...</div>;
  58. }
  59. return (
  60. <div style={{ display: 'flex', flexDirection: 'column', 'flex': '0 0 auto', height: '100%' }}>
  61. <Header account={account} onFollow={this.handleFollow} onUnfollow={this.handleUnfollow} />
  62. <StatusList statuses={statuses} onReply={this.handleReply} onReblog={this.handleReblog} onFavourite={this.handleFavourite} />
  63. </div>
  64. );
  65. }
  66. });
  67. export default connect(mapStateToProps)(Account);