2019-02-22 11:10:33 +08:00
import React , { Fragment } from 'react' ;
2019-03-15 16:30:20 +08:00
import { gettext } from '../../utils/constants' ;
2018-10-15 15:51:29 +08:00
import editUtilties from '../../utils/editor-utilties' ;
2018-11-08 10:36:41 +08:00
import { Utils } from '../../utils/utils' ;
2018-11-22 17:00:23 +08:00
import PropTypes from 'prop-types' ;
2018-12-07 14:58:37 +08:00
import toaster from '../../components/toast' ;
2018-10-15 15:51:29 +08:00
import Loading from '../../components/loading' ;
import DraftListView from '../../components/draft-list-view/draft-list-view' ;
2018-11-22 17:00:23 +08:00
const propTypes = {
isLoadingDraft : PropTypes . bool . isRequired ,
2019-02-22 11:10:33 +08:00
updateDraftsList : PropTypes . func . isRequired ,
draftList : PropTypes . array . isRequired ,
2018-12-22 03:05:25 +00:00
getDrafts : PropTypes . func . isRequired ,
2018-11-22 17:00:23 +08:00
} ;
2018-10-15 15:51:29 +08:00
class DraftContent extends React . Component {
componentDidMount ( ) {
2018-12-22 03:05:25 +00:00
this . props . getDrafts ( ) ;
2018-10-15 15:51:29 +08:00
}
2019-02-22 11:10:33 +08:00
onDeleteHandler = ( draft ) => {
// let draft = this.state.currentDraft;
2018-11-08 10:36:41 +08:00
let draft _name = Utils . getFileName ( draft . draft _file _path ) ;
2018-10-15 15:51:29 +08:00
editUtilties . deleteDraft ( draft . id ) . then ( res => {
2018-11-08 10:36:41 +08:00
this . props . updateDraftsList ( draft . id ) ;
2018-10-25 17:50:47 +08:00
let msg _s = gettext ( 'Successfully deleted draft %(draft)s.' ) ;
2018-11-08 10:36:41 +08:00
msg _s = msg _s . replace ( '%(draft)s' , draft _name ) ;
2018-12-07 14:58:37 +08:00
toaster . success ( msg _s ) ;
2018-10-15 15:51:29 +08:00
} ) . catch ( ( ) => {
2018-10-25 17:50:47 +08:00
let msg _s = gettext ( 'Failed to delete draft %(draft)s.' ) ;
2018-11-08 10:36:41 +08:00
msg _s = msg _s . replace ( '%(draft)s' , draft _name ) ;
2018-12-07 14:58:37 +08:00
toaster . danger ( msg _s ) ;
2018-10-15 15:51:29 +08:00
} ) ;
}
2019-03-01 16:49:35 +08:00
onPublishHandler = ( draft ) => {
// let draft = this.state.currentDraft;
2018-11-08 10:36:41 +08:00
let draft _name = Utils . getFileName ( draft . draft _file _path ) ;
2018-10-15 15:51:29 +08:00
editUtilties . publishDraft ( draft . id ) . then ( res => {
2018-11-08 10:36:41 +08:00
this . props . updateDraftsList ( draft . id ) ;
2018-10-25 17:50:47 +08:00
let msg _s = gettext ( 'Successfully published draft %(draft)s.' ) ;
2018-11-08 10:36:41 +08:00
msg _s = msg _s . replace ( '%(draft)s' , draft _name ) ;
2018-12-07 14:58:37 +08:00
toaster . success ( msg _s ) ;
2018-10-15 15:51:29 +08:00
} ) . catch ( ( ) => {
2018-10-25 17:50:47 +08:00
let msg _s = gettext ( 'Failed to publish draft %(draft)s.' ) ;
2018-11-08 10:36:41 +08:00
msg _s = msg _s . replace ( '%(draft)s' , draft _name ) ;
2018-12-07 14:58:37 +08:00
toaster . danger ( msg _s ) ;
2018-10-15 15:51:29 +08:00
} ) ;
}
render ( ) {
return (
2018-10-25 14:42:53 +08:00
< div className = "cur-view-content" >
2019-02-22 11:10:33 +08:00
{ this . props . isLoadingDraft && < Loading / > }
{ ! this . props . isLoadingDraft && (
< Fragment >
{ this . props . draftList . length === 0 && (
< div className = "message empty-tip" >
< h2 > { gettext ( 'No draft yet' ) } < / h 2 >
< p > { gettext ( 'Draft is a way to let you collaborate with others on files. You can create a draft from a file, edit the draft and then ask for a review. The original file will be updated only after the draft be reviewed.' ) } < / p >
< / d i v >
) }
{ this . props . draftList . length !== 0 && (
< DraftListView
draftList = { this . props . draftList }
onDeleteHandler = { this . onDeleteHandler }
2019-03-01 16:49:35 +08:00
onPublishHandler = { this . onPublishHandler }
2019-02-22 11:10:33 +08:00
/ >
) }
< / F r a g m e n t >
) }
2018-10-15 15:51:29 +08:00
< / d i v >
2018-10-16 18:19:51 +08:00
) ;
2018-10-15 15:51:29 +08:00
}
}
2018-11-22 17:00:23 +08:00
DraftContent . propTypes = propTypes ;
2018-10-15 15:51:29 +08:00
export default DraftContent ;