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' ;
2024-04-19 14:51:41 +08:00
import { gettext , canAddGroup } from '../../utils/constants' ;
2018-12-10 11:52:44 +08:00
import { seafileAPI } from '../../utils/seafile-api' ;
2019-07-16 10:01:09 +08:00
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' ;
2024-04-19 14:51:41 +08:00
import TopToolbar from '../../components/toolbar/top-toolbar' ;
2018-12-12 18:24:47 +08:00
import GroupsToolbar from '../../components/toolbar/groups-toolbar' ;
2019-06-11 14:11:10 +08:00
import EmptyTip from '../../components/empty-tip' ;
2024-04-19 14:51:41 +08:00
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 {
2020-11-02 13:56:35 +08:00
2018-12-10 11:52:44 +08:00
constructor ( props ) {
super ( props ) ;
this . state = {
isLoading : true ,
errorMsg : '' ,
2024-04-19 14:51:41 +08:00
groupList : [ ]
2019-01-31 17:37:02 +08:00
} ;
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 ) ;
2024-04-19 14:51:41 +08:00
group . repos = item . repos . map ( item => {
return new Repo ( item ) ;
} ) ;
2018-12-10 11:52:44 +08:00
return group ;
2019-01-31 17:37:02 +08:00
} ) ;
2018-12-10 11:52:44 +08:00
this . setState ( {
isLoading : false ,
2020-01-02 11:29:31 +08:00
groupList : groupList . sort ( ( a , b ) => {
return a . name . toLowerCase ( ) < b . name . toLowerCase ( ) ? - 1 : 1 ;
} )
2018-12-10 11:52:44 +08:00
} ) ;
} ) . catch ( ( error ) => {
2019-12-05 15:45:16 +08:00
this . setState ( {
isLoading : false ,
errorMsg : Utils . getErrorMsg ( error , true ) // true: show login tip if 403
2020-11-02 13:56:35 +08:00
} ) ;
2018-12-10 11:52:44 +08:00
} ) ;
2023-09-13 08:40:50 +08:00
} ;
2018-12-08 08:37:18 +08:00
2024-04-19 14:51:41 +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 ( {
2024-04-19 14:51:41 +08:00
groupList : newList
2018-12-12 18:24:47 +08:00
} ) ;
2023-09-13 08:40:50 +08:00
} ;
2018-12-12 18:24:47 +08:00
componentDidMount ( ) {
this . listGroups ( ) ;
}
2024-04-19 14:51:41 +08:00
updateGroup = ( group ) => {
const { groupList } = this . state ;
2018-12-29 18:25:18 +08:00
this . setState ( {
2024-04-19 14:51:41 +08:00
groupList : groupList . map ( ( item ) => {
if ( item . id == group . id ) {
item = group ;
}
return item ;
} )
2018-12-29 18:25:18 +08:00
} ) ;
2023-09-13 08:40:50 +08:00
} ;
2018-12-29 18:25:18 +08:00
2018-12-08 08:37:18 +08:00
render ( ) {
2019-06-11 14:11:10 +08:00
const emptyTip = (
< EmptyTip >
2020-01-03 16:50:17 +08:00
< h2 > { gettext ( 'No groups' ) } < / h 2 >
2019-06-11 14:11:10 +08:00
{ 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 >
2019-06-11 14:11:10 +08:00
}
< / E m p t y T i p >
) ;
2018-12-08 08:37:18 +08:00
return (
2018-12-10 11:52:44 +08:00
< Fragment >
2024-04-19 14:51:41 +08:00
< TopToolbar
2018-12-12 18:24:47 +08:00
onShowSidePanel = { this . props . onShowSidePanel }
onSearchedClick = { this . props . onSearchedClick }
2024-04-19 14:51:41 +08:00
>
{ canAddGroup && < GroupsToolbar onCreateGroup = { this . onCreateGroup } / > }
< / T o p T o o l b a r >
2018-12-29 18:25:18 +08:00
< 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" >
2019-01-31 17:37:02 +08:00
< h3 className = "sf-heading" > { gettext ( 'My Groups' ) } < / h 3 >
2018-12-10 11:52:44 +08:00
< / d i v >
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 / > }
2019-12-19 13:44:30 +08:00
{ ( ! this . state . isLoading && this . state . errorMsg ) && < div className = "error text-center mt-2" > { this . state . errorMsg } < / d i v > }
2019-06-11 14:11:10 +08:00
{ ( ! 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 (
2024-04-19 14:51:41 +08:00
< GroupItem
2020-11-02 13:56:35 +08:00
key = { index }
group = { group }
2024-04-19 14:51:41 +08:00
updateGroup = { this . updateGroup }
2018-12-29 18:25:18 +08:00
/ >
2018-12-10 11:52:44 +08:00
) ;
} ) }
< / d i v >
< / d i v >
< / d i v >
< / F r a g m e n t >
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 ;