2024-09-12 06:32:00 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import copy from 'copy-to-clipboard';
|
|
|
|
import { gettext, serviceURL } from '../../utils/constants';
|
2024-12-24 03:20:40 +00:00
|
|
|
import SeahubModalHeader from '@/components/common/seahub-modal-header';
|
|
|
|
import { Button, Modal, ModalBody, ModalFooter, Alert, InputGroup, InputGroupText } from 'reactstrap';
|
2024-09-12 06:32:00 +00:00
|
|
|
import toaster from '../toast';
|
|
|
|
import wikiAPI from '../../utils/wiki-api';
|
|
|
|
|
2024-09-21 09:18:04 +00:00
|
|
|
import '../../css/publish-wiki-dialog.css';
|
|
|
|
|
2024-09-12 06:32:00 +00:00
|
|
|
const propTypes = {
|
|
|
|
wiki: PropTypes.object,
|
|
|
|
onPublish: PropTypes.func.isRequired,
|
|
|
|
toggleCancel: PropTypes.func.isRequired,
|
2024-12-25 07:43:07 +00:00
|
|
|
handleCustomUrl: PropTypes.func.isRequired
|
2024-09-12 06:32:00 +00:00
|
|
|
};
|
|
|
|
|
2024-09-21 09:18:04 +00:00
|
|
|
const DEFAULT_URL = serviceURL + '/wiki/publish/';
|
|
|
|
|
2024-09-12 06:32:00 +00:00
|
|
|
class PublishWikiDialog extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2024-12-25 07:43:07 +00:00
|
|
|
url: this.props.customUrlString,
|
2024-09-12 06:32:00 +00:00
|
|
|
errMessage: '',
|
|
|
|
isSubmitBtnActive: false,
|
|
|
|
};
|
|
|
|
this.newInput = React.createRef();
|
2024-09-21 09:18:04 +00:00
|
|
|
this.preTextRef = React.createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
setTimeout(() => {
|
|
|
|
const preTextWidth = this.preTextRef.current.offsetWidth;
|
|
|
|
this.newInput.current.style.paddingLeft = (preTextWidth + 12) + 'px';
|
|
|
|
this.newInput.current.focus();
|
|
|
|
}, 1);
|
2024-09-12 06:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
this.setState({
|
|
|
|
isSubmitBtnActive: !!e.target.value.trim(),
|
|
|
|
url: e.target.value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
let { isValid, errMessage } = this.validateInput();
|
|
|
|
if (!isValid) {
|
|
|
|
this.setState({
|
|
|
|
errMessage: errMessage,
|
2024-09-21 09:18:04 +00:00
|
|
|
url: '',
|
2024-09-12 06:32:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
2024-09-21 09:18:04 +00:00
|
|
|
this.props.onPublish(DEFAULT_URL + this.state.url.trim());
|
|
|
|
this.toggle();
|
2024-09-12 06:32:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
deleteCustomUrl = () => {
|
|
|
|
let wiki_id = this.props.wiki.id;
|
|
|
|
wikiAPI.deletePublishWikiLink(wiki_id).then((res) => {
|
2024-09-21 09:18:04 +00:00
|
|
|
this.setState({ url: '' });
|
2024-12-25 07:43:07 +00:00
|
|
|
this.props.handleCustomUrl('');
|
2024-10-16 06:09:14 +00:00
|
|
|
toaster.success(gettext('Wiki custom URL deleted'));
|
2024-09-12 06:32:00 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response) {
|
|
|
|
let errorMsg = error.response.data.error_msg;
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleSubmit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
toggle = () => {
|
|
|
|
this.props.toggleCancel();
|
|
|
|
};
|
|
|
|
|
|
|
|
validateInput = () => {
|
|
|
|
let url = this.state.url.trim();
|
|
|
|
let isValid = true;
|
|
|
|
let errMessage = '';
|
|
|
|
if (!url) {
|
|
|
|
isValid = false;
|
2024-09-21 09:18:04 +00:00
|
|
|
errMessage = gettext('URL is required');
|
2024-09-12 06:32:00 +00:00
|
|
|
return { isValid, errMessage };
|
|
|
|
}
|
|
|
|
return { isValid, errMessage };
|
|
|
|
};
|
|
|
|
|
|
|
|
copyLink = () => {
|
2024-09-21 09:18:04 +00:00
|
|
|
copy(DEFAULT_URL + this.state.url.trim());
|
2024-09-12 06:32:00 +00:00
|
|
|
toaster.success(gettext('URL is copied to the clipboard'));
|
|
|
|
};
|
|
|
|
|
2024-09-21 09:18:04 +00:00
|
|
|
onClickPreText = () => {
|
|
|
|
this.newInput.current.focus();
|
|
|
|
};
|
|
|
|
|
2024-09-12 06:32:00 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
2024-12-24 03:20:40 +00:00
|
|
|
<SeahubModalHeader toggle={this.toggle}>{gettext('Publish Wiki')}</SeahubModalHeader>
|
2024-09-12 06:32:00 +00:00
|
|
|
<ModalBody>
|
|
|
|
<p>{gettext('Customize URL')}</p>
|
2024-09-21 09:18:04 +00:00
|
|
|
<InputGroup className="publish-wiki-custom-url-inputs">
|
|
|
|
<span className="input-pretext" ref={this.preTextRef} onClick={this.onClickPreText}>{DEFAULT_URL}</span>
|
|
|
|
<input
|
|
|
|
className="publish-wiki-custom-url-input form-control"
|
|
|
|
type="text"
|
2024-09-12 06:32:00 +00:00
|
|
|
value={this.state.url}
|
|
|
|
onChange={this.handleChange}
|
2024-09-21 09:18:04 +00:00
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
ref={this.newInput}
|
2024-09-12 06:32:00 +00:00
|
|
|
/>
|
2024-09-21 09:18:04 +00:00
|
|
|
<InputGroupText>
|
2024-09-12 06:32:00 +00:00
|
|
|
<Button color="primary" onClick={this.copyLink} className="border-0">{gettext('Copy')}</Button>
|
2024-09-21 09:18:04 +00:00
|
|
|
</InputGroupText>
|
2024-09-12 06:32:00 +00:00
|
|
|
</InputGroup>
|
2024-09-21 09:18:04 +00:00
|
|
|
<p className='sf-tip-default mt-2'>
|
2024-09-12 06:32:00 +00:00
|
|
|
{gettext('The custom part of the URL must be between 5 and 30 characters long and may only contain letters (a-z), numbers, and hyphens.')}
|
2024-09-21 09:18:04 +00:00
|
|
|
</p>
|
2024-09-12 06:32:00 +00:00
|
|
|
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2024-12-25 07:43:07 +00:00
|
|
|
{this.props.customUrlString !== '' &&
|
2024-11-19 03:22:10 +00:00
|
|
|
<Button color="secondary" onClick={this.deleteCustomUrl}>{gettext('Unpublish')}</Button>
|
|
|
|
}
|
2024-09-12 06:32:00 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PublishWikiDialog.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default PublishWikiDialog;
|