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.

125 lines
3.9 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { changeReportComment, submitReport } from '../../actions/reports';
  4. import { refreshAccountTimeline } from '../../actions/timelines';
  5. import PropTypes from 'prop-types';
  6. import ImmutablePropTypes from 'react-immutable-proptypes';
  7. import Column from '../ui/components/column';
  8. import Button from '../../components/button';
  9. import { makeGetAccount } from '../../selectors';
  10. import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
  11. import StatusCheckBox from './containers/status_check_box_container';
  12. import Immutable from 'immutable';
  13. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  14. const messages = defineMessages({
  15. heading: { id: 'report.heading', defaultMessage: 'New report' },
  16. placeholder: { id: 'report.placeholder', defaultMessage: 'Additional comments' },
  17. submit: { id: 'report.submit', defaultMessage: 'Submit' },
  18. });
  19. const makeMapStateToProps = () => {
  20. const getAccount = makeGetAccount();
  21. const mapStateToProps = state => {
  22. const accountId = state.getIn(['reports', 'new', 'account_id']);
  23. return {
  24. isSubmitting: state.getIn(['reports', 'new', 'isSubmitting']),
  25. account: getAccount(state, accountId),
  26. comment: state.getIn(['reports', 'new', 'comment']),
  27. statusIds: Immutable.OrderedSet(state.getIn(['timelines', `account:${accountId}`, 'items'])).union(state.getIn(['reports', 'new', 'status_ids'])),
  28. };
  29. };
  30. return mapStateToProps;
  31. };
  32. class Report extends React.PureComponent {
  33. static contextTypes = {
  34. router: PropTypes.object,
  35. };
  36. static propTypes = {
  37. isSubmitting: PropTypes.bool,
  38. account: ImmutablePropTypes.map,
  39. statusIds: ImmutablePropTypes.orderedSet.isRequired,
  40. comment: PropTypes.string.isRequired,
  41. dispatch: PropTypes.func.isRequired,
  42. intl: PropTypes.object.isRequired,
  43. };
  44. componentWillMount () {
  45. if (!this.props.account) {
  46. this.context.router.history.replace('/');
  47. }
  48. }
  49. componentDidMount () {
  50. if (!this.props.account) {
  51. return;
  52. }
  53. this.props.dispatch(refreshAccountTimeline(this.props.account.get('id')));
  54. }
  55. componentWillReceiveProps (nextProps) {
  56. if (this.props.account !== nextProps.account && nextProps.account) {
  57. this.props.dispatch(refreshAccountTimeline(nextProps.account.get('id')));
  58. }
  59. }
  60. handleCommentChange = (e) => {
  61. this.props.dispatch(changeReportComment(e.target.value));
  62. }
  63. handleSubmit = () => {
  64. this.props.dispatch(submitReport());
  65. this.context.router.history.replace('/');
  66. }
  67. render () {
  68. const { account, comment, intl, statusIds, isSubmitting } = this.props;
  69. if (!account) {
  70. return null;
  71. }
  72. return (
  73. <Column heading={intl.formatMessage(messages.heading)} icon='flag'>
  74. <ColumnBackButtonSlim />
  75. <div className='report scrollable'>
  76. <div className='report__target'>
  77. <FormattedMessage id='report.target' defaultMessage='Reporting' />
  78. <strong>{account.get('acct')}</strong>
  79. </div>
  80. <div className='scrollable report__statuses'>
  81. <div>
  82. {statusIds.map(statusId => <StatusCheckBox id={statusId} key={statusId} disabled={isSubmitting} />)}
  83. </div>
  84. </div>
  85. <div className='report__textarea-wrapper'>
  86. <textarea
  87. className='report__textarea'
  88. placeholder={intl.formatMessage(messages.placeholder)}
  89. value={comment}
  90. onChange={this.handleCommentChange}
  91. disabled={isSubmitting}
  92. />
  93. <div className='report__submit'>
  94. <div className='report__submit-button'><Button disabled={isSubmitting} text={intl.formatMessage(messages.submit)} onClick={this.handleSubmit} /></div>
  95. </div>
  96. </div>
  97. </div>
  98. </Column>
  99. );
  100. }
  101. }
  102. export default connect(makeMapStateToProps)(injectIntl(Report));