2018-12-07 16:01:23 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-12-24 03:20:40 +00:00
|
|
|
import { Button, Modal, ModalBody, ModalFooter, Input, Label } from 'reactstrap';
|
|
|
|
import SeahubModalHeader from '../common/seahub-modal-header';
|
2024-07-05 07:30:12 +00:00
|
|
|
import { gettext, isPro } from '../../utils/constants';
|
2024-05-30 02:04:03 +00:00
|
|
|
import wikiAPI from '../../utils/wiki-api';
|
2024-05-28 08:31:00 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
import toaster from '../toast';
|
2024-12-14 05:17:21 +00:00
|
|
|
import { SeahubSelect } from '../common/select';
|
2018-12-07 16:01:23 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
toggleCancel: PropTypes.func.isRequired,
|
|
|
|
addWiki: PropTypes.func.isRequired,
|
2024-09-20 08:04:35 +00:00
|
|
|
currentDeptID: PropTypes.string,
|
2018-12-07 16:01:23 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 03:57:30 +00:00
|
|
|
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,
|
2024-05-28 08:31:00 +00:00
|
|
|
selectedOption: null,
|
|
|
|
options: [],
|
2018-12-07 16:01:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-28 08:31:00 +00:00
|
|
|
componentDidMount() {
|
2024-07-05 07:43:50 +00:00
|
|
|
if (!isPro) return;
|
2024-05-30 02:04:03 +00:00
|
|
|
wikiAPI.listWikiDepartments().then(res => {
|
2024-05-28 08:31:00 +00:00
|
|
|
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;
|
2024-07-23 06:40:52 +00:00
|
|
|
obj.email = departments[i].email;
|
2024-05-28 08:31:00 +00:00
|
|
|
obj.label = departments[i].name;
|
|
|
|
options.push(obj);
|
|
|
|
}
|
2024-06-17 02:19:27 +00:00
|
|
|
this.setState({ options });
|
2024-09-20 08:04:35 +00:00
|
|
|
if (this.props.currentDeptID) {
|
|
|
|
const selectedOption = options.find(op => op.id == this.props.currentDeptID);
|
2024-06-17 02:19:27 +00:00
|
|
|
this.setState({ selectedOption });
|
|
|
|
}
|
2024-05-28 08:31:00 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
|
|
|
});
|
2024-05-30 02:04:03 +00:00
|
|
|
}
|
2024-05-28 08:31:00 +00:00
|
|
|
|
2018-12-07 16:01:23 +00:00
|
|
|
inputNewName = (e) => {
|
|
|
|
this.setState({
|
|
|
|
name: e.target.value,
|
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-07 16:01:23 +00:00
|
|
|
|
2023-11-14 12:25:25 +00:00
|
|
|
handleKeyDown = (e) => {
|
2018-12-07 16:01:23 +00:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleSubmit();
|
2020-11-02 05:56:35 +00:00
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-07 16:01:23 +00:00
|
|
|
|
|
|
|
handleSubmit = () => {
|
2024-05-15 03:57:30 +00:00
|
|
|
const wikiName = this.state.name.trim();
|
2024-05-28 08:31:00 +00:00
|
|
|
const departmentID = this.state.selectedOption ? this.state.selectedOption.id : null;
|
2024-05-15 03:57:30 +00:00
|
|
|
if (!wikiName) return;
|
2024-05-28 08:31:00 +00:00
|
|
|
this.props.addWiki(wikiName, departmentID);
|
2018-12-07 16:01:23 +00:00
|
|
|
this.props.toggleCancel();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-07 16:01:23 +00:00
|
|
|
|
|
|
|
toggle = () => {
|
|
|
|
this.props.toggleCancel();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-07 16:01:23 +00:00
|
|
|
|
2024-05-28 08:31:00 +00:00
|
|
|
handleSelectChange = (option) => {
|
|
|
|
this.setState({ selectedOption: option });
|
|
|
|
};
|
|
|
|
|
2018-12-07 16:01:23 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2023-11-30 05:58:06 +00:00
|
|
|
<Modal isOpen={true} autoFocus={false} toggle={this.toggle}>
|
2024-12-24 03:20:40 +00:00
|
|
|
<SeahubModalHeader toggle={this.toggle}>{gettext('Add Wiki')}</SeahubModalHeader>
|
2018-12-07 16:01:23 +00:00
|
|
|
<ModalBody>
|
2024-05-15 03:57:30 +00:00
|
|
|
<Label>{gettext('Name')}</Label>
|
2025-01-22 08:10:19 +00:00
|
|
|
<Input
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
autoFocus={true}
|
|
|
|
value={this.state.name}
|
|
|
|
onChange={this.inputNewName}
|
|
|
|
name="wiki-name"
|
|
|
|
/>
|
2024-07-05 07:43:50 +00:00
|
|
|
{isPro &&
|
2024-07-05 07:30:12 +00:00
|
|
|
<>
|
|
|
|
<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}
|
|
|
|
noOptionsMessage={() => {return gettext('No options available');}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|
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>
|
2024-06-17 02:19:27 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.name.trim()}>{gettext('Submit')}</Button>
|
2018-12-07 16:01:23 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 03:57:30 +00:00
|
|
|
AddWikiDialog.propTypes = propTypes;
|
2018-12-07 16:01:23 +00:00
|
|
|
|
2024-05-15 03:57:30 +00:00
|
|
|
export default AddWikiDialog;
|