Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove autosaveable check from autosave monitor #10431

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/editor/src/components/autosave-monitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import { withSelect, withDispatch } from '@wordpress/data';

export class AutosaveMonitor extends Component {
componentDidUpdate( prevProps ) {
const { isDirty, isAutosaveable } = this.props;
const { isDirty } = this.props;

if (
prevProps.isDirty !== isDirty ||
prevProps.isAutosaveable !== isAutosaveable
) {
this.toggleTimer( isDirty && isAutosaveable );
if ( prevProps.isDirty !== isDirty ) {
this.toggleTimer( isDirty );
}
}

Expand Down Expand Up @@ -41,13 +38,11 @@ export default compose( [
withSelect( ( select ) => {
const {
isEditedPostDirty,
isEditedPostAutosaveable,
getEditorSettings,
} = select( 'core/editor' );
const { autosaveInterval } = getEditorSettings();
return {
isDirty: isEditedPostDirty(),
isAutosaveable: isEditedPostAutosaveable(),
autosaveInterval,
};
} ),
Expand Down
24 changes: 2 additions & 22 deletions packages/editor/src/components/autosave-monitor/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,19 @@ describe( 'AutosaveMonitor', () => {
} );

describe( '#componentDidUpdate()', () => {
it( 'should start autosave timer when having become dirty and saveable', () => {
wrapper.setProps( { isDirty: true, isAutosaveable: true } );
it( 'should start autosave timer when having become dirty', () => {
wrapper.setProps( { isDirty: true } );

expect( toggleTimer ).toHaveBeenCalledWith( true );
} );

it( 'should stop autosave timer when the autosave is up to date', () => {
wrapper.setProps( { isDirty: true, isAutosaveable: false } );

expect( toggleTimer ).toHaveBeenCalledWith( false );
} );

it( 'should stop autosave timer when having become dirty but not autosaveable', () => {
wrapper.setProps( { isDirty: true, isAutosaveable: false } );

expect( toggleTimer ).toHaveBeenCalledWith( false );
} );

it( 'should stop autosave timer when having become not dirty', () => {
wrapper.setProps( { isDirty: true } );
toggleTimer.mockClear();
wrapper.setProps( { isDirty: false } );

expect( toggleTimer ).toHaveBeenCalledWith( false );
} );

it( 'should stop autosave timer when having become not autosaveable', () => {
wrapper.setProps( { isDirty: true } );
toggleTimer.mockClear();
wrapper.setProps( { isAutosaveable: false } );

expect( toggleTimer ).toHaveBeenCalledWith( false );
} );
} );

describe( '#componentWillUnmount()', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/components/post-preview-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class PostPreviewButton extends Component {
* Triggers autosave if post is autosaveable.
*/
openPreviewWindow() {
const { isAutosaveable, previewLink, currentPostLink } = this.props;
const { isEditedPostAutosaveable, previewLink, currentPostLink } = this.props;
const isAutosaveable = isEditedPostAutosaveable();

// Open a popup, BUT: Set it to a blank page until save completes. This
// is necessary because popups can only be opened in response to user
Expand Down Expand Up @@ -157,7 +158,7 @@ export default compose( [
isDirty: isEditedPostDirty(),
isNew: isEditedPostNew(),
isSaveable: isEditedPostSaveable(),
isAutosaveable: isEditedPostAutosaveable(),
isEditedPostAutosaveable: isEditedPostAutosaveable,
Copy link
Member

@gziolo gziolo Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing, why does it pass down selector rather than calling it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing, why does it pass down selector rather than calling it here?

From original comment:

I'm also not sure if it would be a good idea to pass the selector function as a prop instead of the value, but like that we can call it only when needed.

It is a bit strange, though I understand why it's being done. I think though that if we were to go this direction, I'd rather we move toward dropping all consideration of whether the post is autosaveable, and just use the "is dirty" with error tolerance from the autosave REST response for unneeded saves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, we should document the solution to ensure that it doesn't get refactor by accident.

isViewable: get( postType, [ 'viewable' ], false ),
};
} ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,29 @@ describe( 'PostPreviewButton', () => {
it( 'should open the currentPostLink if not autosaveable nor preview link available', () => {
const currentPostLink = 'https://wordpress.org/?p=1';
assertForPreview( {
isAutosaveable: false,
isEditedPostAutosaveable: () => false,
previewLink: undefined,
currentPostLink,
}, currentPostLink, false );
} );

it( 'should save for autosaveable post with preview link', () => {
assertForPreview( {
isAutosaveable: true,
isEditedPostAutosaveable: () => true,
previewLink: 'https://wordpress.org/?p=1&preview=true',
}, null, true );
} );

it( 'should save for autosaveable post without preview link', () => {
assertForPreview( {
isAutosaveable: true,
isEditedPostAutosaveable: () => true,
previewLink: undefined,
}, null, true );
} );

it( 'should not save but open a popup window if not autosaveable but preview link available', () => {
assertForPreview( {
isAutosaveable: false,
isEditedPostAutosaveable: () => false,
previewLink: 'https://wordpress.org/?p=1&preview=true',
}, 'https://wordpress.org/?p=1&preview=true', false );
} );
Expand Down