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.

79 lines
2.2 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 } from '../../actions/accounts';
  5. import Button from '../../components/button';
  6. function selectAccount(state, id) {
  7. return state.getIn(['timelines', 'accounts', id], null);
  8. }
  9. const mapStateToProps = (state, props) => ({
  10. account: selectAccount(state, Number(props.params.accountId))
  11. });
  12. const Account = React.createClass({
  13. propTypes: {
  14. params: React.PropTypes.object.isRequired,
  15. dispatch: React.PropTypes.func.isRequired,
  16. account: ImmutablePropTypes.map
  17. },
  18. mixins: [PureRenderMixin],
  19. componentWillMount () {
  20. this.props.dispatch(fetchAccount(this.props.params.accountId));
  21. },
  22. componentWillReceiveProps(nextProps) {
  23. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  24. this.props.dispatch(fetchAccount(nextProps.params.accountId));
  25. }
  26. },
  27. handleFollowClick () {
  28. this.props.dispatch(followAccount(this.props.account.get('id')));
  29. },
  30. handleUnfollowClick () {
  31. this.props.dispatch(unfollowAccount(this.props.account.get('id')));
  32. },
  33. render () {
  34. const { account } = this.props;
  35. let action;
  36. if (account === null) {
  37. return <div>Loading {this.props.params.accountId}...</div>;
  38. }
  39. if (account.get('following')) {
  40. action = <Button text='Unfollow' onClick={this.handleUnfollowClick} />;
  41. } else {
  42. action = <Button text='Follow' onClick={this.handleFollowClick} />
  43. }
  44. return (
  45. <div>
  46. <p>
  47. {account.get('display_name')}
  48. {account.get('acct')}
  49. </p>
  50. {account.get('url')}
  51. <p>{account.get('note')}</p>
  52. {account.get('followers_count')} followers<br />
  53. {account.get('following_count')} following<br />
  54. {account.get('statuses_count')} posts
  55. <p>{action}</p>
  56. </div>
  57. );
  58. }
  59. });
  60. export default connect(mapStateToProps)(Account);