import React from 'react'; import PropTypes from 'prop-types'; import { gettext } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api'; import { Modal, ModalHeader, ModalBody, ModalFooter, Input, Button } from 'reactstrap'; import { Utils } from '../../utils/utils'; class CreateGroupDialog extends React.Component { constructor(props) { super(props); this.state = { groupName: '', errorMsg: '', isSubmitBtnActive: false, }; } handleGroupChange = (event) => { let name = event.target.value; if (!name.trim()) { this.setState({ isSubmitBtnActive: false }); } else { this.setState({ isSubmitBtnActive: true }); } this.setState({ groupName: name }); if (this.state.errorMsg) { this.setState({ errorMsg: '' }); } }; handleSubmitGroup = () => { let name = this.state.groupName.trim(); if (name) { let that = this; seafileAPI.createGroup(name).then((res) => { that.props.onCreateGroup(res.data); this.props.toggleDialog(); }).catch((error) => { let errorMsg = Utils.getErrorMsg(error); this.setState({ errorMsg: errorMsg }); }); } else { this.setState({ errorMsg: gettext('Name is required') }); } this.setState({ groupName: '', }); }; handleKeyDown = (e) => { if (e.keyCode === 13) { this.handleSubmitGroup(); e.preventDefault(); } }; render() { return ( {gettext('New Group')} {this.state.errorMsg} ); } } const CreateGroupDialogPropTypes = { toggleDialog: PropTypes.func.isRequired, onCreateGroup: PropTypes.func.isRequired }; CreateGroupDialog.propTypes = CreateGroupDialogPropTypes; export default CreateGroupDialog;