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.

126 lines
3.9 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 {
  5. fetchAccount,
  6. followAccount,
  7. unfollowAccount,
  8. blockAccount,
  9. unblockAccount,
  10. fetchAccountTimeline,
  11. expandAccountTimeline
  12. } from '../../actions/accounts';
  13. import { deleteStatus } from '../../actions/statuses';
  14. import { replyCompose } from '../../actions/compose';
  15. import {
  16. favourite,
  17. reblog,
  18. unreblog,
  19. unfavourite
  20. } from '../../actions/interactions';
  21. import Header from './components/header';
  22. import {
  23. selectStatus,
  24. selectAccount
  25. } from '../../reducers/timelines';
  26. import StatusList from '../../components/status_list';
  27. import LoadingIndicator from '../../components/loading_indicator';
  28. import Immutable from 'immutable';
  29. import ActionBar from './components/action_bar';
  30. function selectStatuses(state, accountId) {
  31. return state.getIn(['timelines', 'accounts_timelines', accountId], Immutable.List()).map(id => selectStatus(state, id)).filterNot(status => status === null);
  32. };
  33. const mapStateToProps = (state, props) => ({
  34. account: selectAccount(state, Number(props.params.accountId)),
  35. statuses: selectStatuses(state, Number(props.params.accountId)),
  36. me: state.getIn(['timelines', 'me'])
  37. });
  38. const Account = React.createClass({
  39. propTypes: {
  40. params: React.PropTypes.object.isRequired,
  41. dispatch: React.PropTypes.func.isRequired,
  42. account: ImmutablePropTypes.map,
  43. statuses: ImmutablePropTypes.list
  44. },
  45. mixins: [PureRenderMixin],
  46. componentWillMount () {
  47. this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
  48. this.props.dispatch(fetchAccountTimeline(Number(this.props.params.accountId)));
  49. },
  50. componentWillReceiveProps(nextProps) {
  51. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  52. this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
  53. this.props.dispatch(fetchAccountTimeline(Number(nextProps.params.accountId)));
  54. }
  55. },
  56. handleFollow () {
  57. if (this.props.account.getIn(['relationship', 'following'])) {
  58. this.props.dispatch(unfollowAccount(this.props.account.get('id')));
  59. } else {
  60. this.props.dispatch(followAccount(this.props.account.get('id')));
  61. }
  62. },
  63. handleBlock () {
  64. if (this.props.account.getIn(['relationship', 'blocking'])) {
  65. this.props.dispatch(unblockAccount(this.props.account.get('id')));
  66. } else {
  67. this.props.dispatch(blockAccount(this.props.account.get('id')));
  68. }
  69. },
  70. handleReply (status) {
  71. this.props.dispatch(replyCompose(status));
  72. },
  73. handleReblog (status) {
  74. if (status.get('reblogged')) {
  75. this.props.dispatch(unreblog(status));
  76. } else {
  77. this.props.dispatch(reblog(status));
  78. }
  79. },
  80. handleFavourite (status) {
  81. if (status.get('favourited')) {
  82. this.props.dispatch(unfavourite(status));
  83. } else {
  84. this.props.dispatch(favourite(status));
  85. }
  86. },
  87. handleDelete (status) {
  88. this.props.dispatch(deleteStatus(status.get('id')));
  89. },
  90. handleScrollToBottom () {
  91. this.props.dispatch(expandAccountTimeline(this.props.account.get('id')));
  92. },
  93. render () {
  94. const { account, statuses, me } = this.props;
  95. if (account === null) {
  96. return <LoadingIndicator />;
  97. }
  98. return (
  99. <div style={{ display: 'flex', flexDirection: 'column', 'flex': '0 0 auto', height: '100%' }}>
  100. <Header account={account} />
  101. <ActionBar account={account} me={me} onFollow={this.handleFollow} onBlock={this.handleBlock} />
  102. <StatusList statuses={statuses} me={me} onScrollToBottom={this.handleScrollToBottom} onReply={this.handleReply} onReblog={this.handleReblog} onFavourite={this.handleFavourite} />
  103. </div>
  104. );
  105. }
  106. });
  107. export default connect(mapStateToProps)(Account);