2018-12-06 03:28:16 +00:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
2019-01-04 10:11:13 +00:00
import copy from 'copy-to-clipboard' ;
2019-02-12 03:16:49 +00:00
import { Button , Form , FormGroup , FormText , Label , Input , InputGroup , InputGroupAddon , Alert } from 'reactstrap' ;
2019-05-06 08:08:34 +00:00
import { gettext , shareLinkPasswordMinLength } from '../../utils/constants' ;
2019-01-04 10:11:13 +00:00
import { seafileAPI } from '../../utils/seafile-api' ;
2019-05-06 08:08:34 +00:00
import { Utils } from '../../utils/utils' ;
2019-01-04 10:11:13 +00:00
import SharedUploadInfo from '../../models/shared-upload-info' ;
import toaster from '../toast' ;
2018-12-06 03:28:16 +00:00
const propTypes = {
itemPath : PropTypes . string . isRequired ,
2019-01-05 12:40:41 +00:00
repoID : PropTypes . string . isRequired ,
closeShareDialog : PropTypes . func . isRequired ,
2018-12-06 03:28:16 +00:00
} ;
class GenerateUploadLink extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
showPasswordInput : false ,
passwordVisible : false ,
password : '' ,
passwdnew : '' ,
2019-01-04 10:11:13 +00:00
sharedUploadInfo : null ,
2018-12-06 03:28:16 +00:00
} ;
}
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 ) {
2019-01-04 10:11:13 +00:00
let sharedUploadInfo = new SharedUploadInfo ( res . data [ 0 ] ) ;
this . setState ( { sharedUploadInfo : sharedUploadInfo } ) ;
2018-12-06 03:28:16 +00:00
}
} ) ;
}
addPassword = ( ) => {
this . setState ( {
showPasswordInput : ! this . state . showPasswordInput ,
password : '' ,
passwdnew : '' ,
errorInfo : ''
} ) ;
}
togglePasswordVisible = ( ) => {
this . setState ( {
passwordVisible : ! this . state . passwordVisible
} ) ;
}
generatePassword = ( ) => {
2019-05-06 08:08:34 +00:00
let val = Utils . generatePassword ( shareLinkPasswordMinLength ) ;
2018-12-06 03:28:16 +00:00
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 ;
if ( this . state . showPasswordInput && ( this . state . password == '' ) ) {
this . setState ( {
errorInfo : gettext ( 'Please enter password' )
} ) ;
}
2019-05-06 08:08:34 +00:00
else if ( this . state . showPasswordInput && ( this . state . showPasswordInput && this . state . password . length < shareLinkPasswordMinLength ) ) {
2018-12-06 03:28:16 +00:00
this . setState ( {
errorInfo : gettext ( 'Password is too short' )
} ) ;
}
else if ( this . state . showPasswordInput && ( this . state . password !== this . state . passwordnew ) ) {
this . setState ( {
errorInfo : gettext ( 'Passwords don\'t match' )
} ) ;
} else {
seafileAPI . createUploadLink ( repoID , path , this . state . password ) . then ( ( res ) => {
2019-01-04 10:11:13 +00:00
let sharedUploadInfo = new SharedUploadInfo ( res . data ) ;
this . setState ( { sharedUploadInfo : sharedUploadInfo } ) ;
2018-12-06 03:28:16 +00:00
} ) ;
}
}
2019-01-04 10:11:13 +00:00
onCopyUploadLink = ( ) => {
let uploadLink = this . state . sharedUploadInfo . link ;
copy ( uploadLink ) ;
toaster . success ( gettext ( 'Upload link is copied to the clipboard.' ) ) ;
2019-01-05 12:40:41 +00:00
this . props . closeShareDialog ( ) ;
2019-01-04 10:11:13 +00:00
}
2018-12-06 03:28:16 +00:00
deleteUploadLink = ( ) => {
2019-01-31 09:37:02 +00:00
let sharedUploadInfo = this . state . sharedUploadInfo ;
2019-01-04 10:11:13 +00:00
seafileAPI . deleteUploadLink ( sharedUploadInfo . token ) . then ( ( ) => {
2018-12-06 03:28:16 +00:00
this . setState ( {
showPasswordInput : false ,
password : '' ,
passwordnew : '' ,
2019-01-04 10:11:13 +00:00
sharedUploadInfo : null ,
2018-12-06 03:28:16 +00:00
} ) ;
} ) ;
}
render ( ) {
2019-05-06 08:08:34 +00:00
let passwordLengthTip = gettext ( '(at least {passwordLength} characters)' ) ;
passwordLengthTip = passwordLengthTip . replace ( '{passwordLength}' , shareLinkPasswordMinLength ) ;
2019-01-04 10:11:13 +00:00
if ( this . state . sharedUploadInfo ) {
let sharedUploadInfo = this . state . sharedUploadInfo ;
2018-12-06 03:28:16 +00:00
return (
2019-01-04 10:11:13 +00:00
< div >
< Form className = "mb-4" >
< FormGroup >
< dt className = "text-secondary font-weight-normal" > { gettext ( 'Upload Link:' ) } < / d t >
< dd className = "d-flex" >
< span > { sharedUploadInfo . link } < / s p a n >
< span className = "far fa-copy action-icon" onClick = { this . onCopyUploadLink } > < / s p a n >
< / d d >
< / F o r m G r o u p >
< / F o r m >
2018-12-06 03:28:16 +00:00
< Button onClick = { this . deleteUploadLink } > { gettext ( 'Delete' ) } < / B u t t o n >
2019-01-04 10:11:13 +00:00
< / d i v >
2018-12-06 03:28:16 +00:00
) ;
}
return (
< Form className = "generate-upload-link" >
< FormGroup >
< FormText className = "tip" > { gettext ( 'You can share the generated link to others and then they can upload files to this directory via the link.' ) } < / F o r m T e x t >
< / F o r m G r o u p >
< FormGroup check >
< Label check >
< Input type = "checkbox" onChange = { this . addPassword } / > { ' ' } { gettext ( 'Add password protection' ) }
< / L a b e l >
< / F o r m G r o u p >
{ this . state . showPasswordInput &&
< FormGroup className = "link-operation-content" >
2018-12-20 07:55:07 +00:00
{ /* todo translate */ }
2019-05-06 08:08:34 +00:00
< Label className = "font-weight-bold" > { gettext ( 'Password' ) } < / L a b e l > { ' ' } < s p a n c l a s s N a m e = " t i p " > { p a s s w o r d L e n g t h T i p } < / s p a n >
2018-12-06 03:28:16 +00:00
< InputGroup className = "passwd" >
< Input type = { this . state . passwordVisible ? 'text' : 'password' } value = { this . state . password || '' } onChange = { this . inputPassword } / >
< InputGroupAddon addonType = "append" >
< Button onClick = { this . togglePasswordVisible } > < i className = { ` link-operation-icon fas ${ this . state . passwordVisible ? 'fa-eye' : 'fa-eye-slash' } ` } > < / i > < / B u t t o n >
< Button onClick = { this . generatePassword } > < i className = "link-operation-icon fas fa-magic" > < / i > < / B u t t o n >
< / I n p u t G r o u p A d d o n >
< / I n p u t G r o u p >
2019-01-28 08:35:45 +00:00
< Label className = "font-weight-bold" > { gettext ( 'Password again' ) } < / L a b e l >
2018-12-06 03:28:16 +00:00
< Input className = "passwd" type = { this . state . passwordVisible ? 'text' : 'password' } value = { this . state . passwordnew || '' } onChange = { this . inputPasswordNew } / >
< / F o r m G r o u p >
}
2019-02-12 03:16:49 +00:00
{ this . state . errorInfo && < Alert color = "danger" className = "mt-2" > { this . state . errorInfo } < / A l e r t > }
2018-12-06 03:28:16 +00:00
< Button className = "generate-link-btn" onClick = { this . generateUploadLink } > { gettext ( 'Generate' ) } < / B u t t o n >
< / F o r m >
) ;
}
}
GenerateUploadLink . propTypes = propTypes ;
export default GenerateUploadLink ;