1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-10 11:21:29 +00:00
Files
seahub/frontend/src/pages/groups/groups-view.js

129 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-12-10 11:52:44 +08:00
import React, { Fragment } from 'react';
2018-12-08 08:37:18 +08:00
import PropTypes from 'prop-types';
import { gettext, canAddGroup } from '../../utils/constants';
2018-12-10 11:52:44 +08:00
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
2018-12-10 11:52:44 +08:00
import Loading from '../../components/loading';
import Group from '../../models/group';
2018-12-18 17:21:01 +08:00
import Repo from '../../models/repo';
import TopToolbar from '../../components/toolbar/top-toolbar';
2018-12-12 18:24:47 +08:00
import GroupsToolbar from '../../components/toolbar/groups-toolbar';
import EmptyTip from '../../components/empty-tip';
import GroupItem from './group-item';
2018-12-08 08:37:18 +08:00
2018-12-10 11:52:44 +08:00
import '../../css/groups.css';
2018-12-08 08:37:18 +08:00
2018-12-10 11:52:44 +08:00
class GroupsView extends React.Component {
2018-12-10 11:52:44 +08:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
errorMsg: '',
groupList: []
};
2018-12-10 11:52:44 +08:00
}
2018-12-12 18:24:47 +08:00
listGroups = () => {
2019-01-08 13:55:00 +08:00
seafileAPI.listGroups(true).then((res) => {
2018-12-10 11:52:44 +08:00
// `{'with_repos': 1}`: list repos of every group
2018-12-10 13:30:44 +08:00
let groupList = res.data.map(item => {
2018-12-10 11:52:44 +08:00
let group = new Group(item);
group.repos = item.repos.map(item => {
return new Repo(item);
});
2018-12-10 11:52:44 +08:00
return group;
});
2018-12-10 11:52:44 +08:00
this.setState({
isLoading: false,
groupList: groupList.sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
})
2018-12-10 11:52:44 +08:00
});
}).catch((error) => {
this.setState({
isLoading: false,
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
});
2018-12-10 11:52:44 +08:00
});
};
2018-12-08 08:37:18 +08:00
onCreateGroup = (groupData) => {
const newGroup = new Group(groupData);
const { groupList: newList } = this.state;
newList.unshift(newGroup);
2018-12-12 18:24:47 +08:00
this.setState({
groupList: newList
2018-12-12 18:24:47 +08:00
});
};
2018-12-12 18:24:47 +08:00
componentDidMount() {
this.listGroups();
}
updateGroup = (group) => {
const { groupList } = this.state;
this.setState({
groupList: groupList.map((item) => {
if (item.id == group.id) {
item = group;
}
return item;
})
});
};
2018-12-08 08:37:18 +08:00
render() {
const emptyTip = (
<EmptyTip>
2020-01-03 16:50:17 +08:00
<h2>{gettext('No groups')}</h2>
{canAddGroup ?
2020-01-03 16:50:17 +08:00
<p>{gettext('You are not in any groups. Groups allow multiple people to collaborate on libraries. You can create a group by clicking the "New Group" button in the menu bar.')}</p> :
<p>{gettext('You are not in any groups. Groups allow multiple people to collaborate on libraries. Groups you join will be listed here.')}</p>
}
</EmptyTip>
);
2018-12-08 08:37:18 +08:00
return (
2018-12-10 11:52:44 +08:00
<Fragment>
<TopToolbar
2018-12-12 18:24:47 +08:00
onShowSidePanel={this.props.onShowSidePanel}
onSearchedClick={this.props.onSearchedClick}
>
{canAddGroup && <GroupsToolbar onCreateGroup={this.onCreateGroup} />}
</TopToolbar>
<div className="main-panel-center flex-row">
2018-12-10 11:52:44 +08:00
<div className="cur-view-container">
<div className="cur-view-path">
<h3 className="sf-heading">{gettext('My Groups')}</h3>
2018-12-10 11:52:44 +08:00
</div>
2018-12-13 16:35:11 +08:00
<div className="cur-view-content cur-view-content-groups">
2018-12-10 11:52:44 +08:00
{this.state.isLoading && <Loading />}
{(!this.state.isLoading && this.state.errorMsg) && <div className="error text-center mt-2">{this.state.errorMsg}</div>}
{(!this.state.isLoading && !this.state.errorMsg && this.state.groupList.length == 0) && emptyTip}
2018-12-10 11:52:44 +08:00
{!this.state.isLoading && this.state.groupList.map((group, index) => {
return (
<GroupItem
key={index}
group={group}
updateGroup={this.updateGroup}
/>
2018-12-10 11:52:44 +08:00
);
})}
</div>
</div>
</div>
</Fragment>
2018-12-08 08:37:18 +08:00
);
}
}
2018-12-12 18:24:47 +08:00
const GroupsViewPropTypes = {
onShowSidePanel: PropTypes.func.isRequired,
onSearchedClick: PropTypes.func.isRequired,
};
GroupsView.propTypes = GroupsViewPropTypes;
2018-12-08 08:37:18 +08:00
2018-12-10 11:52:44 +08:00
export default GroupsView;