1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-03 16:10:26 +00:00
Files
seahub/frontend/src/pages/my-libs/my-libs.js

179 lines
5.7 KiB
JavaScript
Raw Normal View History

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';
import { Utils } from '../../utils/utils';
import Repo from '../../models/repo';
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';
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: '',
isLoading: true,
repoList: [],
isShowDetails: false,
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
};
this.emptyMessage = (
<div className="empty-tip">
<h2>{gettext('You have not created any libraries')}</h2>
<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>
</div>
);
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: {…}, …}
let repoList = res.data.repos.map((item) => {
return new Repo(item);
});
2018-11-30 17:18:41 +08:00
this.setState({
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({
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({
isLoading: false,
2018-12-12 17:51:12 +08:00
errorMsg: gettext('Error')
2018-11-30 17:18:41 +08:00
});
}
} else {
this.setState({
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,
};
this.state.repoList.unshift(repo);
this.setState({repoList: this.state.repoList});
2018-12-07 10:36:59 +08:00
});
}
sortRepoList = (sortBy, sortOrder) => {
cookie.save('seafile-repo-dir-sort-by', sortBy);
cookie.save('seafile-repo-dir-sort-order', sortOrder);
this.setState({
sortBy: sortBy,
sortOrder: sortOrder,
repoList: Utils.sortRepos(this.state.repoList, sortBy, sortOrder)
});
}
2018-12-12 15:34:54 +08:00
onTransferRepo = (repoID) => {
let repoList = this.state.repoList.filter(item => {
2018-12-12 15:34:54 +08:00
return item.repo_id !== repoID;
});
this.setState({repoList: repoList});
2018-12-07 02:46:47 +00:00
}
2018-12-12 15:34:54 +08:00
onRenameRepo = (repo, newName) => {
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;
});
this.setState({repoList: repoList});
2018-12-12 15:34:54 +08:00
}
onDeleteRepo = (repo) => {
let repoList = this.state.repoList.filter(item => {
2018-12-12 15:34:54 +08:00
return item.repo_id !== repo.repo_id;
});
this.setState({repoList: repoList});
2018-12-12 15:34:54 +08:00
}
onRepoClick = (repo) => {
if (this.state.isShowDetails) {
this.onRepoDetails(repo);
}
}
2018-12-12 15:34:54 +08:00
onRepoDetails = (repo) => {
this.setState({
currentRepo: repo,
2018-12-12 15:34:54 +08:00
isShowDetails: true,
});
2018-12-12 15:34:54 +08:00
}
closeDetails = () => {
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">
<RepoViewToolbar onShowSidePanel={this.props.onShowSidePanel} onCreateRepo={this.onCreateRepo} libraryType={'mine'}/>
2018-12-03 16:21:09 +08:00
<CommonToolbar onSearchedClick={this.props.onSearchedClick} />
</div>
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')}</h3>
2018-11-30 17:18:41 +08:00
</div>
<div className="cur-view-content">
{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
</div>
</div>
2018-12-12 15:34:54 +08:00
{this.state.isShowDetails && (
<div className="cur-view-detail">
<LibDetail
currentRepo={this.state.currentRepo}
closeDetails={this.closeDetails}
/>
</div>
)}
2018-11-30 17:18:41 +08:00
</div>
</Fragment>
);
}
}
export default MyLibraries;