1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-19 09:37:51 +00:00
seahub/frontend/src/components/dialog/add-wiki-dialog.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-12-07 16:01:23 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label } from 'reactstrap';
import { gettext } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
import toaster from '../toast';
import { SeahubSelect } from '../common/select';
2018-12-07 16:01:23 +00:00
const propTypes = {
toggleCancel: PropTypes.func.isRequired,
addWiki: PropTypes.func.isRequired,
};
class AddWikiDialog extends React.Component {
2018-12-07 16:01:23 +00:00
constructor(props) {
super(props);
this.state = {
2018-12-11 05:44:09 +00:00
name: '',
2019-05-17 03:11:59 +00:00
isSubmitBtnActive: false,
selectedOption: null,
options: [],
2018-12-07 16:01:23 +00:00
};
}
componentDidMount() {
this.listDepartments();
}
listDepartments = () => {
seafileAPI.listDepartments().then(res => {
const departments = res.data.sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
});
let options = [];
for (let i = 0 ; i < departments.length; i++) {
let obj = {};
obj.value = departments[i].name;
obj.id = departments[i].id;
obj.label = departments[i].name;
options.push(obj);
}
this.setState({options: options});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
};
2018-12-07 16:01:23 +00:00
inputNewName = (e) => {
this.setState({
name: e.target.value,
isSubmitBtnActive: !!e.target.value.trim(),
2018-12-07 16:01:23 +00:00
});
};
2018-12-07 16:01:23 +00:00
handleKeyDown = (e) => {
2018-12-07 16:01:23 +00:00
if (e.key === 'Enter') {
this.handleSubmit();
}
};
2018-12-07 16:01:23 +00:00
handleSubmit = () => {
const wikiName = this.state.name.trim();
const departmentID = this.state.selectedOption ? this.state.selectedOption.id : null;
if (!wikiName) return;
this.props.addWiki(wikiName, departmentID);
2018-12-07 16:01:23 +00:00
this.props.toggleCancel();
};
2018-12-07 16:01:23 +00:00
toggle = () => {
this.props.toggleCancel();
};
2018-12-07 16:01:23 +00:00
handleSelectChange = (option) => {
this.setState({ selectedOption: option });
};
2018-12-07 16:01:23 +00:00
render() {
return (
<Modal isOpen={true} autoFocus={false} toggle={this.toggle}>
<ModalHeader toggle={this.toggle}>{gettext('Add Wiki')}</ModalHeader>
2018-12-07 16:01:23 +00:00
<ModalBody>
<Label>{gettext('Name')}</Label>
<Input onKeyDown={this.handleKeyDown} autoFocus={true} value={this.state.name} onChange={this.inputNewName}/>
<Label className='mt-4'>{gettext('Wiki owner')} ({gettext('Optional')})</Label>
<SeahubSelect
onChange={this.handleSelectChange}
options={this.state.options}
hideSelectedOptions={true}
placeholder={gettext('Select a department')}
maxMenuHeight={200}
value={this.state.selectedOption}
components={{ NoOptionsMessage: (
<div style={{margin: '6px 10px', textAlign: 'center', color: 'hsl(0,0%,50%)'}}>{gettext('No department')}</div>
) }}
/>
2018-12-07 16:01:23 +00:00
</ModalBody>
<ModalFooter>
2018-12-08 05:31:41 +00:00
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
2019-05-17 03:11:59 +00:00
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
2018-12-07 16:01:23 +00:00
</ModalFooter>
</Modal>
);
}
}
AddWikiDialog.propTypes = propTypes;
2018-12-07 16:01:23 +00:00
export default AddWikiDialog;