2018-11-30 17:18:41 +08:00
import React , { Component , Fragment } from 'react' ;
2019-03-19 10:35:21 +08:00
import cookie from 'react-cookies' ;
2018-11-30 17:18:41 +08:00
import { seafileAPI } from '../../utils/seafile-api' ;
2018-12-12 11:38:27 +08:00
import { gettext , loginUrl } from '../../utils/constants' ;
2018-12-28 15:19:10 +08:00
import { Utils } from '../../utils/utils' ;
import Repo from '../../models/repo' ;
2019-02-13 14:21:39 +08:00
import Loading from '../../components/loading' ;
2018-12-03 16:21:09 +08:00
import CommonToolbar from '../../components/toolbar/common-toolbar' ;
import RepoViewToolbar from '../../components/toolbar/repo-view-toobar' ;
2018-12-12 15:34:54 +08:00
import LibDetail from '../../components/dirent-detail/lib-details' ;
2019-02-13 14:21:39 +08:00
import MylibRepoListView from './mylib-repo-list-view' ;
2018-11-30 17:18:41 +08:00
class MyLibraries extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
errorMsg : '' ,
2019-02-13 14:21:39 +08:00
isLoading : true ,
repoList : [ ] ,
isShowDetails : false ,
2019-04-12 14:30:08 +08:00
sortBy : cookie . load ( 'seafile-repo-dir-sort-by' ) || 'name' , // 'name' or 'time'
sortOrder : cookie . load ( 'seafile-repo-dir-sort-order' ) || 'asc' , // 'asc' or 'desc'
2018-11-30 17:18:41 +08:00
} ;
2019-02-13 14:21:39 +08:00
this . emptyMessage = (
< div className = "empty-tip" >
< h2 > { gettext ( 'You have not created any libraries' ) } < / h 2 >
< p > { gettext ( 'You can create a library to organize your files. For example, you can create one for each of your projects. Each library can be synchronized and shared separately.' ) } < / p >
< / d i v >
) ;
2018-11-30 17:18:41 +08:00
}
componentDidMount ( ) {
2018-12-12 17:51:12 +08:00
seafileAPI . listRepos ( { type : 'mine' } ) . then ( ( res ) => {
2018-11-30 17:18:41 +08:00
// res: {data: {...}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
2018-12-28 15:19:10 +08:00
let repoList = res . data . repos . map ( ( item ) => {
return new Repo ( item ) ;
} ) ;
2018-11-30 17:18:41 +08:00
this . setState ( {
2019-02-13 14:21:39 +08:00
isLoading : false ,
repoList : Utils . sortRepos ( repoList , this . state . sortBy , this . state . sortOrder )
2018-11-30 17:18:41 +08:00
} ) ;
} ) . catch ( ( error ) => {
if ( error . response ) {
if ( error . response . status == 403 ) {
this . setState ( {
2019-02-13 14:21:39 +08:00
isLoading : false ,
2018-12-12 17:51:12 +08:00
errorMsg : gettext ( 'Permission denied' )
2018-11-30 17:18:41 +08:00
} ) ;
location . href = ` ${ loginUrl } ?next= ${ encodeURIComponent ( location . href ) } ` ;
} else {
this . setState ( {
2019-02-13 14:21:39 +08:00
isLoading : false ,
2018-12-12 17:51:12 +08:00
errorMsg : gettext ( 'Error' )
2018-11-30 17:18:41 +08:00
} ) ;
}
} else {
this . setState ( {
2019-02-13 14:21:39 +08:00
isLoading : false ,
2018-12-12 17:51:12 +08:00
errorMsg : gettext ( 'Please check the network.' )
2018-11-30 17:18:41 +08:00
} ) ;
}
} ) ;
}
2018-12-07 10:36:59 +08:00
onCreateRepo = ( repo ) => {
2018-12-17 15:12:10 +08:00
let permission = repo . permission ;
2018-12-07 10:36:59 +08:00
seafileAPI . createMineRepo ( repo ) . then ( ( res ) => {
2018-12-17 15:12:10 +08:00
let repo = {
repo _id : res . data . repo _id ,
repo _name : res . data . repo _name ,
size : res . data . repo _size ,
mtime : res . data . mtime ,
owner _email : res . data . email ,
encrypted : res . data . encrypted ,
permission : permission ,
} ;
2019-02-13 14:21:39 +08:00
this . state . repoList . unshift ( repo ) ;
this . setState ( { repoList : this . state . repoList } ) ;
2018-12-07 10:36:59 +08:00
} ) ;
}
2019-02-13 14:21:39 +08:00
sortRepoList = ( sortBy , sortOrder ) => {
2019-04-12 14:30:08 +08:00
cookie . save ( 'seafile-repo-dir-sort-by' , sortBy ) ;
cookie . save ( 'seafile-repo-dir-sort-order' , sortOrder ) ;
2018-12-28 15:19:10 +08:00
this . setState ( {
sortBy : sortBy ,
sortOrder : sortOrder ,
2019-02-13 14:21:39 +08:00
repoList : Utils . sortRepos ( this . state . repoList , sortBy , sortOrder )
2018-12-28 15:19:10 +08:00
} ) ;
}
2018-12-12 15:34:54 +08:00
onTransferRepo = ( repoID ) => {
2019-02-13 14:21:39 +08:00
let repoList = this . state . repoList . filter ( item => {
2018-12-12 15:34:54 +08:00
return item . repo _id !== repoID ;
2019-01-31 17:37:02 +08:00
} ) ;
2019-02-13 14:21:39 +08:00
this . setState ( { repoList : repoList } ) ;
2018-12-07 02:46:47 +00:00
}
2018-12-12 15:34:54 +08:00
onRenameRepo = ( repo , newName ) => {
2019-02-13 14:21:39 +08:00
let repoList = this . state . repoList . map ( item => {
2018-12-12 15:34:54 +08:00
if ( item . repo _id === repo . repo _id ) {
item . repo _name = newName ;
2018-12-07 02:46:47 +00:00
}
2018-12-12 15:34:54 +08:00
return item ;
} ) ;
2019-02-13 14:21:39 +08:00
this . setState ( { repoList : repoList } ) ;
2018-12-12 15:34:54 +08:00
}
onDeleteRepo = ( repo ) => {
2019-02-13 14:21:39 +08:00
let repoList = this . state . repoList . filter ( item => {
2018-12-12 15:34:54 +08:00
return item . repo _id !== repo . repo _id ;
} ) ;
2019-02-13 14:21:39 +08:00
this . setState ( { repoList : repoList } ) ;
2018-12-12 15:34:54 +08:00
}
2019-02-13 14:21:39 +08:00
onRepoClick = ( repo ) => {
2019-01-17 17:05:08 +08:00
if ( this . state . isShowDetails ) {
this . onRepoDetails ( repo ) ;
}
}
2018-12-12 15:34:54 +08:00
onRepoDetails = ( repo ) => {
this . setState ( {
2019-02-13 14:21:39 +08:00
currentRepo : repo ,
2018-12-12 15:34:54 +08:00
isShowDetails : true ,
2019-01-31 17:37:02 +08:00
} ) ;
2018-12-12 15:34:54 +08:00
}
closeDetails = ( ) => {
2019-02-13 14:21:39 +08:00
this . setState ( { isShowDetails : ! this . state . isShowDetails } ) ;
2018-12-07 02:46:47 +00:00
}
2018-11-30 17:18:41 +08:00
render ( ) {
return (
< Fragment >
2019-02-20 11:54:25 +08:00
< div className = "main-panel-north border-left-show" >
2018-12-07 13:21:43 +08:00
< RepoViewToolbar onShowSidePanel = { this . props . onShowSidePanel } onCreateRepo = { this . onCreateRepo } libraryType = { 'mine' } / >
2018-12-03 16:21:09 +08:00
< CommonToolbar onSearchedClick = { this . props . onSearchedClick } / >
< / d i v >
2018-12-12 15:34:54 +08:00
< div className = "main-panel-center flex-row" >
2018-11-30 17:18:41 +08:00
< div className = "cur-view-container" >
< div className = "cur-view-path" >
2018-12-12 17:51:12 +08:00
< h3 className = "sf-heading" > { gettext ( 'My Libraries' ) } < / h 3 >
2018-11-30 17:18:41 +08:00
< / d i v >
< div className = "cur-view-content" >
2019-02-13 14:21:39 +08:00
{ this . state . isLoading && < Loading / > }
{ ! this . state . isLoading && this . state . errorMsg && < p className = "error text-center" > { this . state . errorMsg } < / p > }
{ ! this . state . isLoading && this . state . repoList . length === 0 && this . emptyMessage }
{ ! this . state . isLoading && this . state . repoList . length > 0 &&
< MylibRepoListView
sortBy = { this . state . sortBy }
sortOrder = { this . state . sortOrder }
repoList = { this . state . repoList }
onRenameRepo = { this . onRenameRepo }
onDeleteRepo = { this . onDeleteRepo }
onTransferRepo = { this . onTransferRepo }
onRepoClick = { this . onRepoClick }
sortRepoList = { this . sortRepoList }
/ >
}
2018-11-30 17:18:41 +08:00
< / d i v >
< / d i v >
2018-12-12 15:34:54 +08:00
{ this . state . isShowDetails && (
< div className = "cur-view-detail" >
< LibDetail
currentRepo = { this . state . currentRepo }
closeDetails = { this . closeDetails }
/ >
< / d i v >
) }
2018-11-30 17:18:41 +08:00
< / d i v >
< / F r a g m e n t >
) ;
}
}
export default MyLibraries ;