1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-05 17:14:28 +00:00
seahub/frontend/src/components/dialog/create-group-dialog.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-12-12 10:24:47 +00:00
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';
2018-12-12 10:24:47 +00:00
class CreateGroupDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
groupName: '',
2018-12-13 09:55:22 +00:00
errorMsg: '',
isSubmitBtnActive: false,
2018-12-12 10:24:47 +00:00
};
}
handleGroupChange = (event) => {
2019-04-08 03:46:57 +00:00
let name = event.target.value;
if (!name.trim()) {
2024-07-18 03:58:42 +00:00
this.setState({ isSubmitBtnActive: false });
} else {
2024-07-18 03:58:42 +00:00
this.setState({ isSubmitBtnActive: true });
}
2018-12-12 10:24:47 +00:00
this.setState({
groupName: name
});
2018-12-13 09:55:22 +00:00
if (this.state.errorMsg) {
this.setState({
errorMsg: ''
});
2018-12-13 09:55:22 +00:00
}
};
2018-12-12 10:24:47 +00:00
handleSubmitGroup = () => {
let name = this.state.groupName.trim();
if (name) {
let that = this;
2024-07-18 03:58:42 +00:00
seafileAPI.createGroup(name).then((res) => {
that.props.onCreateGroup(res.data);
this.props.toggleDialog();
2018-12-13 09:55:22 +00:00
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
2024-07-18 03:58:42 +00:00
this.setState({ errorMsg: errorMsg });
2018-12-12 10:24:47 +00:00
});
2019-04-08 03:46:57 +00:00
} else {
this.setState({
errorMsg: gettext('Name is required')
});
2018-12-12 10:24:47 +00:00
}
this.setState({
groupName: '',
});
};
2018-12-13 09:55:22 +00:00
handleKeyDown = (e) => {
if (e.keyCode === 13) {
this.handleSubmitGroup();
2019-05-08 07:34:23 +00:00
e.preventDefault();
2018-12-13 09:55:22 +00:00
}
};
2018-12-12 10:24:47 +00:00
render() {
2024-07-18 03:58:42 +00:00
return (
<Modal isOpen={true} toggle={this.props.toggleDialog} autoFocus={false}>
<ModalHeader toggle={this.props.toggleDialog}>{gettext('New Group')}</ModalHeader>
2018-12-12 10:24:47 +00:00
<ModalBody>
<label htmlFor="groupName">{gettext('Name')}</label>
2019-05-08 07:34:23 +00:00
<Input
type="text"
id="groupName"
value={this.state.groupName}
onChange={this.handleGroupChange}
onKeyDown={this.handleKeyDown}
autoFocus={true}
2019-05-08 07:34:23 +00:00
/>
2018-12-13 09:55:22 +00:00
<span className="error">{this.state.errorMsg}</span>
2018-12-12 10:24:47 +00:00
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={this.props.toggleDialog}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.handleSubmitGroup} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
2018-12-12 10:24:47 +00:00
</ModalFooter>
</Modal>
);
}
}
const CreateGroupDialogPropTypes = {
toggleDialog: PropTypes.func.isRequired,
onCreateGroup: PropTypes.func.isRequired
2018-12-12 10:24:47 +00:00
};
CreateGroupDialog.propTypes = CreateGroupDialogPropTypes;
export default CreateGroupDialog;