import React from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap'; import { gettext, maxFileName } from '../../utils/constants'; const propTypes = { onCreateRepo: PropTypes.func.isRequired, onCreateToggle: PropTypes.func.isRequired }; class CreateDepartmentRepoDialog extends React.Component { constructor(props) { super(props); this.state = { repoName: '', errMessage: '', isSubmitBtnActive: false, }; } handleChange = (e) => { if (!e.target.value.trim()) { this.setState({isSubmitBtnActive: false}); } else { this.setState({isSubmitBtnActive: true}); } this.setState({ repoName: e.target.value, }); } handleSubmit = () => { let isValid = this.validateRepoName(); if (isValid) { let repo = this.createRepo(this.state.repoName); this.props.onCreateRepo(repo, 'department'); } } handleKeyPress = (e) => { if (e.key === 'Enter') { this.handleSubmit(); e.preventDefault(); } } toggle = () => { this.props.onCreateToggle(); } validateRepoName = () => { let errMessage = ''; let repoName = this.state.repoName.trim(); if (!repoName.length) { errMessage = gettext('Name is required'); this.setState({errMessage: errMessage}); return false; } if (repoName.indexOf('/') > -1) { errMessage = gettext('Name should not include \'/\'.'); this.setState({errMessage: errMessage}); return false; } return true; } createRepo = (repoName) => { let repo = { repo_name: repoName }; return repo; } render() { return ( {gettext('New Department Library')}
{this.state.errMessage && {this.state.errMessage}}
); } } CreateDepartmentRepoDialog.propTypes = propTypes; export default CreateDepartmentRepoDialog;