UserGroupEditForm.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import AppContainer from '../../../services/AppContainer';
  7. import AdminUserGroupDetailContainer from '../../../services/AdminUserGroupDetailContainer';
  8. import { toastSuccess, toastError } from '../../../util/apiNotification';
  9. class UserGroupEditForm extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. const { adminUserGroupDetailContainer } = props;
  13. const { userGroup } = adminUserGroupDetailContainer.state;
  14. this.state = {
  15. name: userGroup.name,
  16. nameCache: userGroup.name, // cache for name. update every submit
  17. };
  18. this.xss = window.xss;
  19. this.changeUserGroupName = this.changeUserGroupName.bind(this);
  20. this.handleSubmit = this.handleSubmit.bind(this);
  21. this.validateForm = this.validateForm.bind(this);
  22. }
  23. changeUserGroupName(event) {
  24. this.setState({
  25. name: event.target.value,
  26. });
  27. }
  28. async handleSubmit(e) {
  29. e.preventDefault();
  30. try {
  31. const res = await this.props.adminUserGroupDetailContainer.updateUserGroup({
  32. name: this.state.name,
  33. });
  34. toastSuccess(`Updated the group name to "${this.xss.process(res.data.userGroup.name)}"`);
  35. this.setState({ nameCache: this.state.name });
  36. }
  37. catch (err) {
  38. toastError(new Error('Unable to update the group name'));
  39. }
  40. }
  41. validateForm() {
  42. return (
  43. this.state.name !== this.state.nameCache
  44. && this.state.name !== ''
  45. );
  46. }
  47. render() {
  48. const { t, adminUserGroupDetailContainer } = this.props;
  49. return (
  50. <form className="form-horizontal" onSubmit={this.handleSubmit}>
  51. <fieldset>
  52. <legend>{t('admin:user_group_management.basic_info')}</legend>
  53. <div className="form-group">
  54. <label htmlFor="name" className="col-sm-2 control-label">{t('Name')}</label>
  55. <div className="col-sm-4">
  56. <input className="form-control" type="text" name="name" value={this.state.name} onChange={this.changeUserGroupName} />
  57. </div>
  58. </div>
  59. <div className="form-group">
  60. <label className="col-sm-2 control-label">{t('Created')}</label>
  61. <div className="col-sm-4">
  62. <input
  63. type="text"
  64. className="form-control"
  65. value={dateFnsFormat(new Date(adminUserGroupDetailContainer.state.userGroup.createdAt), 'yyyy-MM-dd')}
  66. disabled
  67. />
  68. </div>
  69. </div>
  70. <div className="form-group">
  71. <div className="col-sm-offset-2 col-sm-10">
  72. <button type="submit" className="btn btn-primary" disabled={!this.validateForm()}>{t('Update')}</button>
  73. </div>
  74. </div>
  75. </fieldset>
  76. </form>
  77. );
  78. }
  79. }
  80. UserGroupEditForm.propTypes = {
  81. t: PropTypes.func.isRequired, // i18next
  82. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  83. adminUserGroupDetailContainer: PropTypes.instanceOf(AdminUserGroupDetailContainer).isRequired,
  84. };
  85. /**
  86. * Wrapper component for using unstated
  87. */
  88. const UserGroupEditFormWrapper = (props) => {
  89. return createSubscribedElement(UserGroupEditForm, props, [AppContainer, AdminUserGroupDetailContainer]);
  90. };
  91. export default withTranslation()(UserGroupEditFormWrapper);