import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import { apiErrorHandler, apiSuccessHandler } from '../../../util/apiNotification'; class UserGroupCreateForm extends React.Component { constructor(props) { super(props); this.state = { name: '', }; this.xss = window.xss; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.validateForm = this.validateForm.bind(this); } handleChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value, }); } async handleSubmit(e) { e.preventDefault(); try { const res = await this.props.crowi.apiPost('/v3/user-groups/create', { name: this.state.name, }); if (res.ok) { const { userGroup, userGroupRelation } = res; this.props.onCreate(userGroup, userGroupRelation); this.setState({ name: '' }); apiSuccessHandler(`Created a user group "${this.xss.process(userGroup.name)}"`); } else { throw new Error(`Unable to create a group "${this.xss.process(this.state.name)}"`); } } catch (err) { apiErrorHandler(err); } } validateForm() { return this.state.name !== ''; } render() { const { t } = this.props; return (
{this.props.isAclEnabled ? ( ) : ( t('user_group_management.deny_create_group') ) }