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

146 lines
4.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import copy from 'copy-to-clipboard';
import { gettext, serviceURL } from '../../utils/constants';
import SeahubModalHeader from '@/components/common/seahub-modal-header';
import { Button, Modal, ModalBody, ModalFooter, Alert, InputGroup, InputGroupText } from 'reactstrap';
import toaster from '../toast';
import wikiAPI from '../../utils/wiki-api';
2024-09-21 09:18:04 +00:00
import '../../css/publish-wiki-dialog.css';
const propTypes = {
wiki: PropTypes.object,
onPublish: PropTypes.func.isRequired,
toggleCancel: PropTypes.func.isRequired,
handleCustomUrl: PropTypes.func.isRequired
};
2024-09-21 09:18:04 +00:00
const DEFAULT_URL = serviceURL + '/wiki/publish/';
class PublishWikiDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
url: this.props.customUrlString,
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);
}
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: '',
});
} else {
2024-09-21 09:18:04 +00:00
this.props.onPublish(DEFAULT_URL + this.state.url.trim());
this.toggle();
}
};
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: '' });
this.props.handleCustomUrl('');
toaster.success(gettext('Wiki custom URL deleted'));
}).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');
return { isValid, errMessage };
}
return { isValid, errMessage };
};
copyLink = () => {
2024-09-21 09:18:04 +00:00
copy(DEFAULT_URL + this.state.url.trim());
toaster.success(gettext('URL is copied to the clipboard'));
};
2024-09-21 09:18:04 +00:00
onClickPreText = () => {
this.newInput.current.focus();
};
render() {
return (
<Modal isOpen={true} toggle={this.toggle}>
<SeahubModalHeader toggle={this.toggle}>{gettext('Publish Wiki')}</SeahubModalHeader>
<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"
value={this.state.url}
onChange={this.handleChange}
2024-09-21 09:18:04 +00:00
onKeyDown={this.handleKeyDown}
ref={this.newInput}
/>
2024-09-21 09:18:04 +00:00
<InputGroupText>
<Button color="primary" onClick={this.copyLink} className="border-0">{gettext('Copy')}</Button>
2024-09-21 09:18:04 +00:00
</InputGroupText>
</InputGroup>
2024-09-21 09:18:04 +00:00
<p className='sf-tip-default mt-2'>
{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>
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
</ModalBody>
<ModalFooter>
{this.props.customUrlString !== '' &&
2024-11-19 03:22:10 +00:00
<Button color="secondary" onClick={this.deleteCustomUrl}>{gettext('Unpublish')}</Button>
}
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
);
}
}
PublishWikiDialog.propTypes = propTypes;
export default PublishWikiDialog;