-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48c092a
commit a553056
Showing
5 changed files
with
158 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
import { sprintf, _n } from 'i18n'; | ||
import IconButton from 'components/icon-button'; | ||
import PanelBody from 'components/panel/body'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { getCurrentPost, isSavingPost } from '../../selectors'; | ||
import { getWPAdminURL } from '../../utils/url'; | ||
|
||
class LastRevision extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
revisions: [], | ||
loading: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchRevisions(); | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( prevProps.postId !== this.props.postId ) { | ||
this.setState( { revisions: [] } ); | ||
} | ||
|
||
if ( | ||
( prevProps.postId !== this.props.postId ) || | ||
( prevProps.isSaving && ! this.props.isSaving ) | ||
) { | ||
this.fetchRevisions(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if ( this.fetchMediaRequest ) { | ||
this.fetchRevisionsRequest.abort(); | ||
} | ||
} | ||
|
||
fetchRevisions() { | ||
if ( ! this.props.postId ) { | ||
this.setState( { loading: false } ); | ||
return; | ||
} | ||
this.setState( { loading: true } ); | ||
const postIdToLoad = this.props.postId; | ||
this.fetchRevisionsRequest = new wp.api.collections.PostRevisions( {}, { parent: postIdToLoad } ).fetch() | ||
.done( ( revisions ) => { | ||
if ( this.props.postId !== postIdToLoad ) { | ||
return; | ||
} | ||
this.setState( { | ||
loading: false, | ||
revisions, | ||
} ); | ||
} ) | ||
.fail( () => { | ||
if ( this.props.postId !== postIdToLoad ) { | ||
return; | ||
} | ||
this.setState( { | ||
loading: false, | ||
} ); | ||
} ); | ||
} | ||
|
||
render() { | ||
const { revisions } = this.state; | ||
const lastRevision = revisions.length ? revisions[ 0 ] : null; | ||
|
||
return ( | ||
<PanelBody> | ||
<IconButton | ||
href={ lastRevision ? getWPAdminURL( 'revision.php', { revision: lastRevision.id } ) : undefined } | ||
className="editor-last-revision__title" | ||
icon="backup" | ||
> | ||
{ | ||
sprintf( | ||
_n( '%d Revision', '%d Revisions', revisions.length ), | ||
revisions.length | ||
) | ||
} | ||
</IconButton> | ||
</PanelBody> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
postId: getCurrentPost( state ).id, | ||
isSaving: isSavingPost( state ), | ||
}; | ||
} | ||
)( LastRevision ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.editor-last-revision__title { | ||
padding: 0; | ||
width: 100%; | ||
font-weight: 600; | ||
|
||
.dashicon { | ||
margin-right: 5px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters