import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import copy from 'copy-to-clipboard';
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, Alert } from 'reactstrap';
import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax, shareLinkExpireDaysDefault, shareLinkPasswordMinLength, canSendShareLinkEmail } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
import SharedLinkInfo from '../../models/shared-link-info';
import toaster from '../toast';
import Loading from '../loading';
import SendLink from '../send-link';
const propTypes = {
itemPath: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
closeShareDialog: PropTypes.func.isRequired,
};
class GenerateShareLink extends React.Component {
constructor(props) {
super(props);
this.isExpireDaysNoLimit = (parseInt(shareLinkExpireDaysMin) === 0 && parseInt(shareLinkExpireDaysMax) === 0 && shareLinkExpireDaysDefault == 0);
this.defaultExpireDays = this.isExpireDaysNoLimit ? '' : shareLinkExpireDaysDefault;
this.state = {
isValidate: false,
isShowPasswordInput: false,
isPasswordVisible: false,
isExpireChecked: !this.isExpireDaysNoLimit,
password: '',
passwdnew: '',
expireDays: this.defaultExpireDays,
errorInfo: '',
sharedLinkInfo: null,
isNoticeMessageShow: false,
isLoading: true,
fileInfo: null,
isSendLinkShown: false
};
this.permissions = {
'can_edit': false,
'can_download': true
};
this.isOfficeFile = Utils.isOfficeFile(this.props.itemPath);
}
componentDidMount() {
let path = this.props.itemPath;
let repoID = this.props.repoID;
seafileAPI.getShareLink(repoID, path).then((res) => {
if (res.data.length !== 0) {
let sharedLinkInfo = new SharedLinkInfo(res.data[0]);
this.setState({
isLoading: false,
sharedLinkInfo: sharedLinkInfo
});
} else {
this.setState({isLoading: false});
}
});
if (this.isOfficeFile) {
seafileAPI.getFileInfo(repoID, path).then((res) => {
if (res.data) {
this.setState({fileInfo: res.data});
}
});
}
}
onPasswordInputChecked = () => {
this.setState({
isShowPasswordInput: !this.state.isShowPasswordInput,
password: '',
passwdnew: '',
errorInfo: ''
});
}
togglePasswordVisible = () => {
this.setState({
isPasswordVisible: !this.state.isPasswordVisible
});
}
generatePassword = () => {
let val = Utils.generatePassword(shareLinkPasswordMinLength);
this.setState({
password: val,
passwdnew: val
});
}
inputPassword = (e) => {
let passwd = e.target.value.trim();
this.setState({password: passwd});
}
inputPasswordNew = (e) => {
let passwd = e.target.value.trim();
this.setState({passwdnew: passwd});
}
setPermission = (permission) => {
if (permission == 'previewAndDownload') {
this.permissions = {
'can_edit': false,
'can_download': true
};
} else if (permission == 'preview') {
this.permissions = {
'can_edit': false,
'can_download': false
};
} else if (permission == 'editOnCloudAndDownload'){
this.permissions = {
'can_edit': true,
'can_download': true
};
}
}
generateShareLink = () => {
let isValid = this.validateParamsInput();
if (isValid) {
this.setState({errorInfo: ''});
let { itemPath, repoID } = this.props;
let { password, isExpireChecked, expireDays } = this.state;
let permissions = this.permissions;
permissions = JSON.stringify(permissions);
const expireDaysSent = isExpireChecked ? expireDays : '';
seafileAPI.createShareLink(repoID, itemPath, password, expireDaysSent, permissions).then((res) => {
let sharedLinkInfo = new SharedLinkInfo(res.data);
this.setState({sharedLinkInfo: sharedLinkInfo});
}).catch((error) => {
toaster.danger(error.response.data.error_msg);
});
}
}
onCopySharedLink = () => {
let sharedLink = this.state.sharedLinkInfo.link;
copy(sharedLink);
toaster.success(gettext('Share link is copied to the clipboard.'));
this.props.closeShareDialog();
}
onCopyDownloadLink = () => {
let downloadLink = this.state.sharedLinkInfo.link + '?dl';
copy(downloadLink);
toaster.success(gettext('Direct download link is copied to the clipboard.'));
this.props.closeShareDialog();
}
deleteShareLink = () => {
let sharedLinkInfo = this.state.sharedLinkInfo;
seafileAPI.deleteShareLink(sharedLinkInfo.token).then(() => {
this.setState({
password: '',
passwordnew: '',
isShowPasswordInput: false,
expireDays: this.defaultExpireDays,
isExpireChecked: !this.isExpireDaysNoLimit,
errorInfo: '',
sharedLinkInfo: null,
isNoticeMessageShow: false,
});
this.permissions = {
'can_edit': false,
'can_download': true
};
});
}
onExpireChecked = (e) => {
this.setState({isExpireChecked: e.target.checked});
}
onExpireDaysChanged = (e) => {
let day = e.target.value.trim();
this.setState({expireDays: day});
}
validateParamsInput = () => {
let { isShowPasswordInput , password, passwdnew, isExpireChecked, expireDays } = this.state;
// validate password
if (isShowPasswordInput) {
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 !== passwdnew) {
this.setState({errorInfo: 'Passwords don\'t match'});
return false;
}
}
// validate days
// no limit
let reg = /^\d+$/;
if (this.isExpireDaysNoLimit) {
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)});
}
} else {
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;
}
expireDays = parseInt(expireDays);
let minDays = parseInt(shareLinkExpireDaysMin);
let maxDays = parseInt(shareLinkExpireDaysMax);
if (minDays !== 0 && maxDays !== maxDays) {
if (expireDays < minDays) {
this.setState({errorInfo: 'Please enter valid days'});
return false;
}
}
if (minDays === 0 && maxDays !== 0 ) {
if (expireDays > maxDays) {
this.setState({errorInfo: 'Please enter valid days'});
return false;
}
}
if (minDays !== 0 && maxDays !== 0) {
if (expireDays < minDays || expireDays > maxDays) {
this.setState({errorInfo: 'Please enter valid days'});
return false;
}
}
this.setState({expireDays: expireDays});
}
return true;
}
onNoticeMessageToggle = () => {
this.setState({isNoticeMessageShow: !this.state.isNoticeMessageShow});
}
toggleSendLink = () => {
this.setState({ isSendLinkShown: !this.state.isSendLinkShown });
}
render() {
if (this.state.isLoading) {
return
{gettext('If the share link is deleted, no one will be able to access it any more.')}
{' '}