UserGroupCreateForm.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* eslint-disable react/no-multi-comp */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { Subscribe } from 'unstated';
  5. import { withTranslation } from 'react-i18next';
  6. import { apiErrorHandler, apiSuccessHandler } from '../../../util/apiNotification';
  7. class UserGroupCreateForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. name: '',
  12. };
  13. this.xss = window.xss;
  14. this.handleChange = this.handleChange.bind(this);
  15. this.handleSubmit = this.handleSubmit.bind(this);
  16. this.validateForm = this.validateForm.bind(this);
  17. }
  18. handleChange(event) {
  19. const target = event.target;
  20. const value = target.type === 'checkbox' ? target.checked : target.value;
  21. const name = target.name;
  22. this.setState({
  23. [name]: value,
  24. });
  25. }
  26. async handleSubmit(e) {
  27. e.preventDefault();
  28. try {
  29. const res = await this.props.crowi.apiv3.post('/user-groups', {
  30. name: this.state.name,
  31. });
  32. const userGroup = res.data.userGroup;
  33. const userGroupId = userGroup._id;
  34. const res2 = await this.props.crowi.apiv3.get(`/user-groups/${userGroupId}/users`);
  35. const { users } = res2.data;
  36. this.props.onCreate(userGroup, users);
  37. this.setState({ name: '' });
  38. apiSuccessHandler(`Created a user group "${this.xss.process(userGroup.name)}"`);
  39. }
  40. catch (err) {
  41. apiErrorHandler(err);
  42. }
  43. }
  44. validateForm() {
  45. return this.state.name !== '';
  46. }
  47. render() {
  48. const { t } = this.props;
  49. return (
  50. <div>
  51. <p>
  52. {this.props.isAclEnabled
  53. ? (
  54. <button type="button" data-toggle="collapse" className="btn btn-default" href="#createGroupForm">
  55. { t('user_group_management.create_group') }
  56. </button>
  57. )
  58. : (
  59. t('user_group_management.deny_create_group')
  60. )
  61. }
  62. </p>
  63. <form onSubmit={this.handleSubmit}>
  64. <div id="createGroupForm" className="collapse">
  65. <div className="form-group">
  66. <label htmlFor="name">{ t('user_group_management.group_name') }</label>
  67. <textarea
  68. id="name"
  69. name="name"
  70. className="form-control"
  71. placeholder={t('user_group_management.group_example')}
  72. value={this.state.name}
  73. onChange={this.handleChange}
  74. >
  75. </textarea>
  76. </div>
  77. <button type="submit" className="btn btn-primary" disabled={!this.validateForm()}>{ t('Create') }</button>
  78. </div>
  79. <input type="hidden" name="_csrf" defaultValue={this.props.crowi.csrfToken} />
  80. </form>
  81. </div>
  82. );
  83. }
  84. }
  85. /**
  86. * Wrapper component for using unstated
  87. */
  88. class UserGroupCreateFormWrapper extends React.PureComponent {
  89. render() {
  90. return (
  91. <Subscribe to={[]}>
  92. {() => (
  93. // eslint-disable-next-line arrow-body-style
  94. <UserGroupCreateForm {...this.props} />
  95. )}
  96. </Subscribe>
  97. );
  98. }
  99. }
  100. UserGroupCreateForm.propTypes = {
  101. t: PropTypes.func.isRequired, // i18next
  102. crowi: PropTypes.object.isRequired,
  103. isAclEnabled: PropTypes.bool,
  104. onCreate: PropTypes.func.isRequired,
  105. };
  106. UserGroupCreateFormWrapper.propTypes = {
  107. t: PropTypes.func.isRequired, // i18next
  108. crowi: PropTypes.object.isRequired,
  109. isAclEnabled: PropTypes.bool,
  110. onCreate: PropTypes.func.isRequired,
  111. };
  112. export default withTranslation()(UserGroupCreateFormWrapper);