import React from 'react';
import PropTypes from 'prop-types';
import copy from 'copy-to-clipboard';
import moment from 'moment';
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, Alert } from 'reactstrap';
import { gettext, shareLinkPasswordMinLength, canSendShareLinkEmail } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
import SharedUploadInfo from '../../models/shared-upload-info';
import toaster from '../toast';
import SendLink from '../send-link';
import SessionExpiredTip from '../session-expired-tip';
const propTypes = {
itemPath: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
closeShareDialog: PropTypes.func.isRequired,
};
class GenerateUploadLink extends React.Component {
constructor(props) {
super(props);
this.state = {
showPasswordInput: false,
passwordVisible: false,
password: '',
passwdnew: '',
sharedUploadInfo: null,
isSendLinkShown: false,
isExpireChecked: false,
expireDays: 0,
};
}
componentDidMount() {
this.getUploadLink();
}
getUploadLink = () => {
let path = this.props.itemPath;
let repoID = this.props.repoID;
seafileAPI.getUploadLinks(repoID, path).then((res) => {
if (res.data.length !== 0) {
let sharedUploadInfo = new SharedUploadInfo(res.data[0]);
this.setState({sharedUploadInfo: sharedUploadInfo});
}
}).catch((err) => {
if (err.response.status === 403) {
toaster.danger(
,
{id: 'session_expired', duration: 3600}
);
this.props.closeShareDialog();
}
});
}
addPassword = () => {
this.setState({
showPasswordInput: !this.state.showPasswordInput,
password: '',
passwdnew: '',
errorInfo: ''
});
}
togglePasswordVisible = () => {
this.setState({
passwordVisible: !this.state.passwordVisible
});
}
generatePassword = () => {
let val = Utils.generatePassword(shareLinkPasswordMinLength);
this.setState({
password: val,
passwordnew: val
});
}
inputPassword = (e) => {
this.setState({
password: e.target.value
});
}
inputPasswordNew = (e) => {
this.setState({
passwordnew: e.target.value
});
}
generateUploadLink = () => {
let path = this.props.itemPath;
let repoID = this.props.repoID;
let { password, expireDays } = this.state;
let isValid = this.validateParamsInput();
if (isValid) {
seafileAPI.createUploadLink(repoID, path, password, expireDays).then((res) => {
let sharedUploadInfo = new SharedUploadInfo(res.data);
this.setState({sharedUploadInfo: sharedUploadInfo});
});
}
}
validateParamsInput = () => {
let { showPasswordInput , password, passwordnew, isExpireChecked, expireDays } = this.state;
// check password params
if (showPasswordInput) {
if (password.length === 0) {
this.setState({errorInfo: 'Please enter password'});
return false;
}
if (password.length < shareLinkPasswordMinLength) {
this.setState({errorInfo: 'Password is too short'});
return false;
}
if (password !== passwordnew) {
this.setState({errorInfo: 'Passwords don\'t match'});
return false;
}
}
// check expire day params
let reg = /^\d+$/;
if (isExpireChecked) {
if (!expireDays) {
this.setState({errorInfo: 'Please enter days'});
return false;
}
if (!reg.test(expireDays)) {
this.setState({errorInfo: 'Please enter a non-negative integer'});
return false;
}
this.setState({expireDays: parseInt(expireDays)});
}
return true;
}
onExpireChecked = (e) => {
this.setState({isExpireChecked: e.target.checked});
}
onExpireDaysChanged = (e) => {
let day = e.target.value.trim();
this.setState({expireDays: day});
}
onCopyUploadLink = () => {
let uploadLink = this.state.sharedUploadInfo.link;
copy(uploadLink);
toaster.success(gettext('Upload link is copied to the clipboard.'));
this.props.closeShareDialog();
}
deleteUploadLink = () => {
let sharedUploadInfo = this.state.sharedUploadInfo;
seafileAPI.deleteUploadLink(sharedUploadInfo.token).then(() => {
this.setState({
showPasswordInput: false,
password: '',
passwordnew: '',
sharedUploadInfo: null,
});
});
}
toggleSendLink = () => {
this.setState({
isSendLinkShown: !this.state.isSendLinkShown
});
}
render() {
const { isSendLinkShown } = this.state;
let passwordLengthTip = gettext('(at least {passwordLength} characters)');
passwordLengthTip = passwordLengthTip.replace('{passwordLength}', shareLinkPasswordMinLength);
if (this.state.sharedUploadInfo) {
let sharedUploadInfo = this.state.sharedUploadInfo;
return (
{canSendShareLinkEmail && !isSendLinkShown && }
{!isSendLinkShown && }
{isSendLinkShown &&
}
);
}
return (
);
}
}
GenerateUploadLink.propTypes = propTypes;
export default GenerateUploadLink;