2018-12-10 13:33:32 +08:00
import React , { Component , Fragment } from 'react' ;
2019-04-22 18:21:25 +08:00
import PropTypes from 'prop-types' ;
2019-04-17 10:34:43 +08:00
import { Button } from 'reactstrap' ;
2018-12-08 00:01:23 +08:00
import { seafileAPI } from '../../utils/seafile-api' ;
2019-05-06 15:50:44 +08:00
import { gettext , loginUrl , canPublishRepo } from '../../utils/constants' ;
2018-12-11 11:20:40 +08:00
import toaster from '../../components/toast' ;
2018-12-11 08:42:30 +08:00
import ModalPortal from '../../components/modal-portal' ;
2018-12-26 16:27:15 +08:00
import CommonToolbar from '../../components/toolbar/common-toolbar' ;
2018-12-10 13:33:32 +08:00
import NewWikiDialog from '../../components/dialog/new-wiki-dialog' ;
import WikiSelectDialog from '../../components/dialog/wiki-select-dialog' ;
2018-12-11 08:42:30 +08:00
import WikiListView from '../../components/wiki-list-view/wiki-list-view' ;
2018-12-08 00:01:23 +08:00
2019-04-22 18:21:25 +08:00
const propTypes = {
onShowSidePanel : PropTypes . func . isRequired ,
onSearchedClick : PropTypes . func . isRequired ,
} ;
2018-12-08 00:01:23 +08:00
class Wikis extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
loading : true ,
errorMsg : '' ,
wikis : [ ] ,
2018-12-11 13:44:09 +08:00
isShowAddWikiMenu : false ,
2018-12-08 00:01:23 +08:00
isShowSelectDialog : false ,
isShowCreateDialog : false ,
} ;
}
componentDidMount ( ) {
this . getWikis ( ) ;
}
getWikis = ( ) => {
seafileAPI . listWikis ( ) . then ( res => {
this . setState ( {
loading : false ,
wikis : res . data . data ,
} ) ;
} ) . catch ( ( error ) => {
if ( error . response ) {
if ( error . response . status == 403 ) {
this . setState ( {
loading : false ,
errorMsg : gettext ( 'Permission denied' )
} ) ;
location . href = ` ${ loginUrl } ?next= ${ encodeURIComponent ( location . href ) } ` ;
} else {
this . setState ( {
loading : false ,
errorMsg : gettext ( 'Error' )
} ) ;
}
} else {
this . setState ( {
loading : false ,
errorMsg : gettext ( 'Please check the network.' )
} ) ;
}
} ) ;
}
2018-12-11 13:22:52 +08:00
clickMenuToggle = ( e ) => {
e . preventDefault ( ) ;
this . onMenuToggle ( ) ;
2018-12-08 00:01:23 +08:00
}
2018-12-11 13:22:52 +08:00
onMenuToggle = ( ) => {
2018-12-11 13:44:09 +08:00
this . setState ( { isShowAddWikiMenu : ! this . state . isShowAddWikiMenu } ) ;
2018-12-08 00:01:23 +08:00
}
onSelectToggle = ( ) => {
2018-12-11 13:44:09 +08:00
this . setState ( { isShowSelectDialog : ! this . state . isShowSelectDialog } ) ;
2018-12-08 00:01:23 +08:00
}
onCreateToggle = ( ) => {
2018-12-11 13:44:09 +08:00
this . setState ( { isShowCreateDialog : ! this . state . isShowCreateDialog } ) ;
2018-12-08 00:01:23 +08:00
}
2019-04-17 10:34:43 +08:00
addWiki = ( repoID ) => {
seafileAPI . addWiki ( repoID ) . then ( ( res ) => {
2018-12-08 00:01:23 +08:00
this . state . wikis . push ( res . data ) ;
2018-12-11 13:44:09 +08:00
this . setState ( { wikis : this . state . wikis } ) ;
2018-12-08 00:01:23 +08:00
} ) . catch ( ( error ) => {
if ( error . response ) {
let errorMsg = error . response . data . error _msg ;
2018-12-11 11:20:40 +08:00
toaster . danger ( errorMsg ) ;
2018-12-08 00:01:23 +08:00
}
} ) ;
}
2018-12-08 11:40:28 +08:00
renameWiki = ( wiki , newName ) => {
seafileAPI . renameWiki ( wiki . slug , newName ) . then ( ( res ) => {
let wikis = this . state . wikis . map ( ( item ) => {
if ( item . name === wiki . name ) {
item = res . data ;
}
return item ;
} ) ;
2018-12-11 11:20:40 +08:00
this . setState ( { wikis : wikis } ) ;
2018-12-08 11:40:28 +08:00
} ) . catch ( ( error ) => {
if ( error . response ) {
let errorMsg = error . response . data . error _msg ;
2018-12-11 11:20:40 +08:00
toaster . danger ( errorMsg ) ;
2018-12-08 11:40:28 +08:00
}
} ) ;
}
2018-12-08 00:01:23 +08:00
deleteWiki = ( wiki ) => {
seafileAPI . deleteWiki ( wiki . slug ) . then ( ( ) => {
2018-12-11 11:20:40 +08:00
let wikis = this . state . wikis . filter ( item => {
return item . name !== wiki . name ;
2018-12-08 00:01:23 +08:00
} ) ;
2018-12-11 11:20:40 +08:00
this . setState ( { wikis : wikis } ) ;
2018-12-08 00:01:23 +08:00
} ) . catch ( ( error ) => {
if ( error . response ) {
let errorMsg = error . response . data . error _msg ;
2018-12-11 11:20:40 +08:00
toaster . danger ( errorMsg ) ;
2018-12-08 00:01:23 +08:00
}
} ) ;
}
render ( ) {
return (
2018-12-10 13:33:32 +08:00
< Fragment >
2019-02-20 11:54:25 +08:00
< div className = "main-panel-north border-left-show" >
< div className = "cur-view-toolbar" >
2018-12-26 16:27:15 +08:00
< span className = "sf2-icon-menu side-nav-toggle hidden-md-up d-md-none" title = "Side Nav Menu" onClick = { this . props . onShowSidePanel } > < / s p a n >
< div className = "operation" >
2019-05-06 15:50:44 +08:00
{ canPublishRepo &&
< Button className = "btn btn-secondary operation-item" onClick = { this . onSelectToggle } >
{ gettext ( 'Publish a Library' ) }
< / B u t t o n >
}
2018-12-26 16:27:15 +08:00
< / d i v >
< / d i v >
< CommonToolbar onSearchedClick = { this . props . onSearchedClick } / >
< / d i v >
< div className = "main-panel-center" >
< div className = "cur-view-container" id = "wikis" >
< div className = "cur-view-path" >
< div className = "path-container" >
2019-04-17 10:34:43 +08:00
< h3 className = "sf-heading" > { gettext ( 'Published Libraries' ) } < / h 3 >
2018-12-08 00:01:23 +08:00
< / d i v >
2018-12-10 13:33:32 +08:00
< / d i v >
< div className = "cur-view-content" >
{ ( this . state . loading || this . state . wikis . length !== 0 ) &&
2018-12-11 08:42:30 +08:00
< WikiListView
2018-12-10 13:33:32 +08:00
data = { this . state }
renameWiki = { this . renameWiki }
deleteWiki = { this . deleteWiki }
/ >
}
{ ( ! this . state . loading && this . state . wikis . length === 0 ) &&
< div className = "message empty-tip" >
2018-12-20 15:55:07 +08:00
< h2 > { gettext ( 'You do not have any wiki' ) } < / h 2 >
2018-12-10 13:33:32 +08:00
< p > { gettext ( 'Seafile Wiki enables you to organize your knowledge in a simple way. The contents of wiki is stored in a normal library with pre-defined file/folder structure. This enables you to edit your wiki in your desktop and then sync back to the server.' ) } < / p >
< / d i v >
}
< / d i v >
2018-12-08 00:01:23 +08:00
< / d i v >
< / d i v >
2018-12-11 13:44:09 +08:00
{ this . state . isShowCreateDialog && (
2018-12-10 13:33:32 +08:00
< ModalPortal >
< NewWikiDialog
toggleCancel = { this . onCreateToggle }
addWiki = { this . addWiki }
/ >
< / M o d a l P o r t a l >
2018-12-11 13:44:09 +08:00
) }
{ this . state . isShowSelectDialog && (
2018-12-10 13:33:32 +08:00
< ModalPortal >
< WikiSelectDialog
toggleCancel = { this . onSelectToggle }
addWiki = { this . addWiki }
/ >
< / M o d a l P o r t a l >
2018-12-11 13:44:09 +08:00
) }
2018-12-10 13:33:32 +08:00
< / F r a g m e n t >
2018-12-08 00:01:23 +08:00
) ;
}
}
2019-04-22 18:21:25 +08:00
Wikis . propTypes = propTypes ;
2018-12-08 00:01:23 +08:00
export default Wikis ;