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.

77 lines
2.4 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { makeGetAccount } from '../../../selectors';
  5. import ImmutablePureComponent from 'react-immutable-pure-component';
  6. import ImmutablePropTypes from 'react-immutable-proptypes';
  7. import Avatar from '../../../components/avatar';
  8. import DisplayName from '../../../components/display_name';
  9. import IconButton from '../../../components/icon_button';
  10. import { defineMessages, injectIntl } from 'react-intl';
  11. import { removeFromListEditor, addToListEditor } from '../../../actions/lists';
  12. const messages = defineMessages({
  13. remove: { id: 'lists.account.remove', defaultMessage: 'Remove from list' },
  14. add: { id: 'lists.account.add', defaultMessage: 'Add to list' },
  15. });
  16. const makeMapStateToProps = () => {
  17. const getAccount = makeGetAccount();
  18. const mapStateToProps = (state, { accountId, added }) => ({
  19. account: getAccount(state, accountId),
  20. added: typeof added === 'undefined' ? state.getIn(['listEditor', 'accounts', 'items']).includes(accountId) : added,
  21. });
  22. return mapStateToProps;
  23. };
  24. const mapDispatchToProps = (dispatch, { accountId }) => ({
  25. onRemove: () => dispatch(removeFromListEditor(accountId)),
  26. onAdd: () => dispatch(addToListEditor(accountId)),
  27. });
  28. class Account extends ImmutablePureComponent {
  29. static propTypes = {
  30. account: ImmutablePropTypes.map.isRequired,
  31. intl: PropTypes.object.isRequired,
  32. onRemove: PropTypes.func.isRequired,
  33. onAdd: PropTypes.func.isRequired,
  34. added: PropTypes.bool,
  35. };
  36. static defaultProps = {
  37. added: false,
  38. };
  39. render () {
  40. const { account, intl, onRemove, onAdd, added } = this.props;
  41. let button;
  42. if (added) {
  43. button = <IconButton icon='times' title={intl.formatMessage(messages.remove)} onClick={onRemove} />;
  44. } else {
  45. button = <IconButton icon='plus' title={intl.formatMessage(messages.add)} onClick={onAdd} />;
  46. }
  47. return (
  48. <div className='account'>
  49. <div className='account__wrapper'>
  50. <div className='account__display-name'>
  51. <div className='account__avatar-wrapper'><Avatar account={account} size={36} /></div>
  52. <DisplayName account={account} />
  53. </div>
  54. <div className='account__relationship'>
  55. {button}
  56. </div>
  57. </div>
  58. </div>
  59. );
  60. }
  61. }
  62. export default connect(makeMapStateToProps, mapDispatchToProps)(injectIntl(Account));