2018-12-07 10:36:59 +08:00
import React , { Component , Fragment } from 'react' ;
2018-12-04 16:52:27 +08:00
import { seafileAPI } from '../../utils/seafile-api' ;
import { Utils } from '../../utils/utils' ;
import { gettext , siteRoot , loginUrl , username } from '../../utils/constants' ;
import Loading from '../../components/loading' ;
import GroupRepoItem from './group-repo-item' ;
2018-12-07 10:36:59 +08:00
import CommonToolbar from '../../components/toolbar/common-toolbar' ;
import RepoViewToolbar from '../../components/toolbar/repo-view-toobar' ;
2018-12-04 16:52:27 +08:00
import '../../css/groups.css' ;
class Header extends Component {
render ( ) {
const { loading , errorMsg , data } = this . props . data ;
if ( loading ) {
return < Loading / > ;
} else if ( errorMsg ) {
return < p className = "error text-center" > { errorMsg } < / p > ;
} else {
/ *
admins : [ "lj@1.com" ]
avatar _url : "http://127.0.0.1:8000/media/avatars/groups/default.png"
created _at : "2018-10-25T08:18:11+00:00"
id : 2
name : "g1"
owner : "lj@1.com"
parent _group _id : 0
wiki _enabled : false
* /
const path = (
< div className = "group-path cur-view-path-path" >
< a href = { ` ${ siteRoot } groups/ ` } > { gettext ( "Groups" ) } < / a >
< span className = "path-split" > / < / s p a n >
< span className = "group-name" > { data . name } < / s p a n >
{ data . parent _group _id == 0 ? null :
< span className = "address-book-group-icon icon-building" title = { gettext ( "This is a special group representing a department." ) } > < / s p a n >
}
< / d i v >
) ;
let showSettingsIcon = true ;
if ( data . parent _group _id != 0 && data . admins . indexOf ( username ) == - 1 ) {
showSettingsIcon = false ;
}
// TODO: click icon
const toolbar = (
< div className = "group-toolbar-2" >
{ showSettingsIcon ? < a href = "#" className = "sf2-icon-cog1 op-icon group-top-op-icon" title = { gettext ( "Settings" ) } id = "group-settings-icon" aria - label = { gettext ( "Settings" ) } > < / a > : n u l l }
< a href = "#" className = "sf2-icon-user2 op-icon group-top-op-icon" title = { gettext ( "Members" ) } id = "group-members-icon" aria - label = { gettext ( "Members" ) } > < / a >
< / d i v >
) ;
return (
< React . Fragment >
{ path }
{ toolbar }
< / R e a c t . F r a g m e n t >
) ;
}
}
}
class Content extends Component {
render ( ) {
const { loading , errorMsg , items } = this . props . data . repos ;
if ( loading ) {
return < Loading / > ;
} else if ( errorMsg ) {
return < p className = "error text-center" > { errorMsg } < / p > ;
} else {
if ( ! items ) {
return null ;
}
const groupInfo = this . props . data . groupMetaInfo . data ;
let emptyTip ;
if ( groupInfo . parent _group _id == 0 ) {
emptyTip = (
< div className = "empty-tip" >
< h2 > { gettext ( 'No library is shared to this group' ) } < / h 2 >
< p > { gettext ( 'You can share libraries by clicking the "New Library" button above or the "Share" icon on your libraries list.' ) } < / p >
< p > { gettext ( 'Libraries shared as writable can be downloaded and synced by other group members. Read only libraries can only be downloaded, updates by others will not be uploaded.' ) } < / p >
< / d i v >
) ;
} else {
if ( groupInfo . admins . indexOf ( username ) == - 1 ) {
emptyTip = (
< div className = "empty-tip" >
< h2 > { gettext ( 'No libraries' ) } < / h 2 >
< / d i v >
) ;
} else {
emptyTip = (
< div className = "empty-tip" >
< h2 > { gettext ( 'No libraries' ) } < / h 2 >
< p > { gettext ( 'You can create libraries by clicking the "New Library" button above.' ) } < / p >
< / d i v >
) ;
}
}
const desktopThead = (
< thead >
< tr >
< th width = "8%" > < span className = "sr-only" > { gettext ( "Library Type" ) } < / s p a n > < / t h >
< th width = "34%" > { gettext ( "Name" ) } < a className = "table-sort-op by-name" href = "#" > { /*TODO: sort*/ } < span className = "sort-icon icon-caret-down hide" > < / s p a n > < / a > < / t h >
< th width = "10%" > < span className = "sr-only" > { gettext ( "Actions" ) } < / s p a n > < / t h >
< th width = "14%" > { gettext ( "Size" ) } < / t h >
< th width = "18%" > { gettext ( "Last Update" ) } < a className = "table-sort-op by-time" href = "#" > { /*TODO: sort*/ } < span className = "sort-icon icon-caret-up" > < / s p a n > < / a > < / t h >
< th width = "16%" > { gettext ( "Owner" ) } < / t h >
< / t r >
< / t h e a d >
) ;
const mobileThead = (
< thead >
< tr >
< th width = "18%" > < span className = "sr-only" > { gettext ( "Library Type" ) } < / s p a n > < / t h >
< th width = "68%" >
{ gettext ( "Sort:" ) } { /* TODO: sort */ }
{ gettext ( "name" ) } < a className = "table-sort-op mobile-table-sort-op by-name" href = "#" > < span className = "sort-icon icon-caret-down hide" > < / s p a n > < / a >
{ gettext ( "last update" ) } < a className = "table-sort-op mobile-table-sort-op by-time" href = "#" > < span className = "sort-icon icon-caret-up" > < / s p a n > < / a >
< / t h >
< th width = "14%" > < span className = "sr-only" > { gettext ( "Actions" ) } < / s p a n > < / t h >
< / t r >
< / t h e a d >
) ;
const extraData = {
groupId : groupInfo . id ,
isStaff : groupInfo . admins . indexOf ( username ) != - 1 ,
showRepoOwner : true
} ;
const table = (
2018-12-06 14:39:21 +08:00
< table >
2018-12-04 16:52:27 +08:00
{ window . innerWidth >= 768 ? desktopThead : mobileThead }
< TableBody items = { items } extra = { extraData } / >
< / t a b l e >
) ;
return items . length ? table : emptyTip ;
}
}
}
class TableBody extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
items : this . props . items
} ;
}
render ( ) {
let listItems = this . state . items . map ( function ( item , index ) {
return < GroupRepoItem key = { index } data = { item } extra = { this . props . extra } / > ;
} , this ) ;
return (
< tbody > { listItems } < / t b o d y >
) ;
}
}
class Group extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
groupMetaInfo : {
loading : true ,
errorMsg : ''
} ,
repos : {
loading : false ,
errorMsg : ''
}
} ;
}
componentDidMount ( ) {
2018-12-06 14:39:21 +08:00
this . updateGroupRepoList ( this . props . groupID ) ;
}
componentWillReceiveProps ( nextProps ) {
if ( nextProps . groupID !== this . props . groupID ) {
this . updateGroupRepoList ( nextProps . groupID ) ;
}
}
updateGroupRepoList = ( groupID ) => {
seafileAPI . getGroup ( groupID ) . then ( ( res ) => {
2018-12-04 16:52:27 +08:00
// res: {data: {...}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
this . setState ( {
groupMetaInfo : {
loading : false ,
data : res . data
}
} ) ;
this . listGroupRepos ( ) ;
} ) . catch ( ( error ) => {
if ( error . response ) {
if ( error . response . status == 403 ) {
this . setState ( {
groupMetaInfo : {
loading : false ,
errorMsg : gettext ( "Permission denied" )
}
} ) ;
location . href = ` ${ loginUrl } ?next= ${ encodeURIComponent ( location . href ) } ` ;
} else {
this . setState ( {
groupMetaInfo : {
loading : false ,
errorMsg : gettext ( "Error" )
}
} ) ;
}
} else {
this . setState ( {
groupMetaInfo : {
loading : false ,
errorMsg : gettext ( "Please check the network." )
}
} ) ;
}
} ) ;
}
listGroupRepos ( ) {
seafileAPI . listGroupRepos ( this . props . groupID ) . then ( ( res ) => {
// res: {data: [...], status: 200, statusText: "OK", headers: {…}, config: {…}, …}
this . setState ( {
repos : {
loading : false ,
items : res . data
}
} ) ;
} ) . catch ( ( error ) => {
if ( error . response ) {
if ( error . response . status == 403 ) {
this . setState ( {
repos : {
loading : false ,
errorMsg : gettext ( "Permission denied" )
}
} ) ;
location . href = ` ${ loginUrl } ?next= ${ encodeURIComponent ( location . href ) } ` ;
} else {
this . setState ( {
repos : {
loading : false ,
errorMsg : gettext ( "Error" )
}
} ) ;
}
} else {
this . setState ( {
repos : {
loading : false ,
errorMsg : gettext ( "Please check the network." )
}
} ) ;
}
} ) ;
}
2018-12-07 10:36:59 +08:00
onCreateRepo = ( repo ) => {
let groupId = this . props . groupID ;
seafileAPI . createGroupRepo ( groupId , repo ) . then ( ( ) => {
//todo update group list
} ) ;
}
2018-12-04 16:52:27 +08:00
render ( ) {
return (
2018-12-07 10:36:59 +08:00
< Fragment >
< div className = "main-panel-north" >
< RepoViewToolbar onShowSidePanel = { this . props . onShowSidePanel } onCreateRepo = { this . onCreateRepo } / >
< CommonToolbar onSearchedClick = { this . props . onSearchedClick } / >
< / d i v >
< div className = "main-panel-center" >
< div className = "cur-view-container" >
< div className = "cur-view-path" >
< Header data = { this . state . groupMetaInfo } / >
< / d i v >
< div className = "cur-view-content" >
< Content data = { this . state } / >
< / d i v >
2018-12-04 16:52:27 +08:00
< / d i v >
< / d i v >
2018-12-07 10:36:59 +08:00
< / F r a g m e n t >
2018-12-04 16:52:27 +08:00
) ;
}
}
export default Group ;