2018-12-12 15:34:54 +08:00
import React , { Component , Fragment } from 'react' ;
2018-12-12 11:38:27 +08:00
import PropTypes from 'prop-types' ;
import { gettext , storages } from '../../utils/constants' ;
2018-12-12 17:09:15 +08:00
import Loading from '../../components/loading' ;
2018-12-12 17:51:12 +08:00
import TableBody from './table-body' ;
2018-12-12 17:09:15 +08:00
import ModalPortal from '../../components/modal-portal' ;
2018-12-12 17:51:12 +08:00
import LibHistorySetting from '../../components/dialog/lib-history-setting-dialog' ;
import TransferDialog from '../../components/dialog/transfer-dialog' ;
2018-12-12 17:09:15 +08:00
import DeleteRepoDialog from '../../components/dialog/delete-repo-dialog' ;
2018-12-12 11:38:27 +08:00
2018-12-12 15:34:54 +08:00
const propTypes = {
loading : PropTypes . bool . isRequired ,
errorMsg : PropTypes . string . isRequired ,
2018-12-28 15:19:10 +08:00
sortBy : PropTypes . string . isRequired ,
sortOrder : PropTypes . string . isRequired ,
sortItems : PropTypes . func . isRequired ,
2018-12-12 15:34:54 +08:00
items : PropTypes . array . isRequired ,
onRenameRepo : PropTypes . func . isRequired ,
onDeleteRepo : PropTypes . func . isRequired ,
onTransferRepo : PropTypes . func . isRequired ,
onRepoDetails : PropTypes . func . isRequired ,
2019-01-17 17:05:08 +08:00
onItemClick : PropTypes . func . isRequired
2018-12-12 15:34:54 +08:00
} ;
2018-12-12 11:38:27 +08:00
class Content extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
deleteItemPopupOpen : false ,
showTransfer : false ,
itemName : '' ,
showHistorySetting : false ,
showDetails : false ,
libID : '' ,
libSize : '' ,
libUpdateTime : ''
} ;
}
2018-12-12 15:34:54 +08:00
toggleDeleteItemPopup = ( ) => {
2018-12-12 11:38:27 +08:00
this . setState ( {
deleteItemPopupOpen : ! this . state . deleteItemPopupOpen
} ) ;
}
2018-12-12 15:34:54 +08:00
showDeleteItemPopup = ( data ) => {
2018-12-12 11:38:27 +08:00
this . toggleDeleteItemPopup ( ) ;
this . setState ( {
deleteItemPopupData : data
} ) ;
}
2018-12-12 15:34:54 +08:00
onTransfer = ( itemName , itemID ) => {
2018-12-12 11:38:27 +08:00
this . setState ( {
showTransfer : ! this . state . showTransfer ,
itemName : itemName ,
libID : itemID
} ) ;
}
2018-12-12 15:34:54 +08:00
onHistorySetting = ( itemName , itemID ) => {
2018-12-12 11:38:27 +08:00
this . setState ( {
showHistorySetting : ! this . state . showHistorySetting ,
itemName : itemName ,
libID : itemID
} ) ;
}
2018-12-28 15:19:10 +08:00
sortByName = ( e ) => {
e . preventDefault ( ) ;
const sortBy = 'name' ;
const sortOrder = this . props . sortOrder == 'asc' ? 'desc' : 'asc' ;
this . props . sortItems ( sortBy , sortOrder ) ;
}
sortByTime = ( e ) => {
e . preventDefault ( ) ;
const sortBy = 'time' ;
const sortOrder = this . props . sortOrder == 'asc' ? 'desc' : 'asc' ;
this . props . sortItems ( sortBy , sortOrder ) ;
}
2018-12-12 11:38:27 +08:00
render ( ) {
2018-12-28 15:19:10 +08:00
const { loading , errorMsg , items , sortBy , sortOrder } = this . props ;
2018-12-12 11:38:27 +08:00
if ( loading ) {
return < Loading / > ;
} else if ( errorMsg ) {
return < p className = "error text-center" > { errorMsg } < / p > ;
} else {
const emptyTip = (
< 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-12-28 15:19:10 +08:00
// sort
const sortByName = sortBy == 'name' ;
const sortByTime = sortBy == 'time' ;
const sortIcon = sortOrder == 'asc' ? < span className = "fas fa-caret-up" > < / s p a n > : < s p a n c l a s s N a m e = " f a s f a - c a r e t - d o w n " > < / s p a n > ;
2018-12-12 11:38:27 +08:00
// TODO: test 'storage backend'
const showStorageBackend = storages . length > 0 ; // only for desktop
const desktopThead = (
< thead >
< tr >
2018-12-12 17:51:12 +08:00
< th width = "4%" > < span className = "sr-only" > { gettext ( 'Library Type' ) } < / s p a n > < / t h >
2018-12-28 15:19:10 +08:00
< th width = "42%" > < a className = "d-block table-sort-op" href = "#" onClick = { this . sortByName } > { gettext ( 'Name' ) } { sortByName && sortIcon } < / a > < / t h >
2018-12-12 17:51:12 +08:00
< th width = "14%" > < span className = "sr-only" > { gettext ( 'Actions' ) } < / s p a n > < / t h >
2018-12-12 11:38:27 +08:00
2018-12-12 17:51:12 +08:00
< th width = { showStorageBackend ? '15%' : '20%' } > { gettext ( 'Size' ) } < / t h >
2018-12-12 11:38:27 +08:00
{ showStorageBackend ? < th width = "10%" > { gettext ( 'Storage backend' ) } < / t h > : n u l l }
2018-12-28 15:19:10 +08:00
< th width = { showStorageBackend ? '15%' : '20%' } > < a className = "d-block table-sort-op" href = "#" onClick = { this . sortByTime } > { gettext ( 'Last Update' ) } { sortByTime && sortIcon } < / a > < / t h >
2018-12-12 11:38:27 +08:00
< / t r >
< / t h e a d >
) ;
const mobileThead = (
< thead >
< tr >
2018-12-12 17:51:12 +08:00
< th width = "18%" > < span className = "sr-only" > { gettext ( 'Library Type' ) } < / s p a n > < / t h >
2018-12-12 11:38:27 +08:00
< th width = "76%" >
2018-12-28 15:19:10 +08:00
{ gettext ( "Sort:" ) }
< a className = "table-sort-op" href = "#" onClick = { this . sortByName } > { gettext ( "name" ) } { sortByName && sortIcon } < / a >
< a className = "table-sort-op" href = "#" onClick = { this . sortByTime } > { gettext ( "last update" ) } { sortByTime && sortIcon } < / a >
2018-12-12 11:38:27 +08:00
< / t h >
2018-12-12 17:51:12 +08:00
< th width = "6%" > < span className = "sr-only" > { gettext ( 'Actions' ) } < / s p a n > < / t h >
2018-12-12 11:38:27 +08:00
< / t r >
< / t h e a d >
) ;
const table = (
< table >
{ window . innerWidth >= 768 ? desktopThead : mobileThead }
2018-12-12 15:34:54 +08:00
< TableBody
items = { items }
onRenameRepo = { this . props . onRenameRepo }
onDeleteRepo = { this . props . onDeleteRepo }
onRepoDetails = { this . props . onRepoDetails }
2019-01-17 17:05:08 +08:00
onItemClick = { this . props . onItemClick }
2018-12-12 15:34:54 +08:00
onTransfer = { this . onTransfer }
showDeleteItemPopup = { this . showDeleteItemPopup }
onHistorySetting = { this . onHistorySetting }
/ >
2018-12-12 11:38:27 +08:00
< / t a b l e >
) ;
const nonEmpty = (
2018-12-12 15:34:54 +08:00
< Fragment >
2018-12-12 11:38:27 +08:00
{ table }
2018-12-12 17:51:12 +08:00
{ this . state . deleteItemPopupOpen && (
< ModalPortal >
< DeleteRepoDialog
toggle = { this . toggleDeleteItemPopup }
data = { this . state . deleteItemPopupData }
/ >
< / M o d a l P o r t a l >
) }
2018-12-12 11:38:27 +08:00
{ this . state . showTransfer &&
< ModalPortal >
2018-12-12 15:34:54 +08:00
< TransferDialog
toggleDialog = { this . onTransfer }
itemName = { this . state . itemName }
repoID = { this . state . libID }
submit = { this . props . onTransferRepo }
/ >
2018-12-12 11:38:27 +08:00
< / M o d a l P o r t a l >
}
{ this . state . showHistorySetting &&
< ModalPortal >
2018-12-12 15:34:54 +08:00
< LibHistorySetting
toggleDialog = { this . onHistorySetting }
itemName = { this . state . itemName }
repoID = { this . state . libID }
/ >
2018-12-12 11:38:27 +08:00
< / M o d a l P o r t a l >
}
2018-12-12 15:34:54 +08:00
< / F r a g m e n t >
2018-12-12 11:38:27 +08:00
) ;
return items . length ? nonEmpty : emptyTip ;
}
}
}
2018-12-12 15:34:54 +08:00
Content . propTypes = propTypes ;
2018-12-28 15:19:10 +08:00
export default Content ;