-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1996 from WordPress/update/diff-attributes-dirty
Perform dirty detection as diff against saved post
- Loading branch information
Showing
12 changed files
with
690 additions
and
305 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
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,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { autosave } from '../actions'; | ||
import { | ||
isEditedPostDirty, | ||
isEditedPostSaveable, | ||
} from '../selectors'; | ||
|
||
export class AutosaveMonitor extends Component { | ||
componentDidUpdate( prevProps ) { | ||
const { isDirty, isSaveable } = this.props; | ||
if ( prevProps.isDirty !== isDirty || | ||
prevProps.isSaveable !== isSaveable ) { | ||
this.toggleTimer( isDirty && isSaveable ); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.toggleTimer( false ); | ||
} | ||
|
||
toggleTimer( isPendingSave ) { | ||
clearTimeout( this.pendingSave ); | ||
|
||
if ( isPendingSave ) { | ||
this.pendingSave = setTimeout( | ||
() => this.props.autosave(), | ||
10000 | ||
); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
isDirty: isEditedPostDirty( state ), | ||
isSaveable: isEditedPostSaveable( state ), | ||
}; | ||
}, | ||
{ autosave } | ||
)( AutosaveMonitor ); |
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,67 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { AutosaveMonitor } from '../'; | ||
|
||
describe( 'AutosaveMonitor', () => { | ||
const toggleTimer = jest.fn(); | ||
let wrapper; | ||
beforeEach( () => { | ||
toggleTimer.mockClear(); | ||
wrapper = shallow( | ||
<AutosaveMonitor />, | ||
{ lifecycleExperimental: true } | ||
); | ||
|
||
wrapper.instance().toggleTimer = toggleTimer; | ||
} ); | ||
|
||
describe( '#componentDidUpdate()', () => { | ||
it( 'should start autosave timer when having become dirty and saveable', () => { | ||
wrapper.setProps( { isDirty: true, isSaveable: true } ); | ||
|
||
expect( toggleTimer ).toHaveBeenCalledWith( true ); | ||
} ); | ||
|
||
it( 'should stop autosave timer when having become dirty but not saveable', () => { | ||
wrapper.setProps( { isDirty: true, isSaveable: 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 saveable', () => { | ||
wrapper.setProps( { isDirty: true } ); | ||
toggleTimer.mockClear(); | ||
wrapper.setProps( { isSaveable: false } ); | ||
|
||
expect( toggleTimer ).toHaveBeenCalledWith( false ); | ||
} ); | ||
} ); | ||
|
||
describe( '#componentWillUnmount()', () => { | ||
it( 'should stop autosave timer', () => { | ||
wrapper.unmount(); | ||
|
||
expect( toggleTimer ).toHaveBeenCalledWith( false ); | ||
} ); | ||
} ); | ||
|
||
describe( '#render()', () => { | ||
it( 'should render nothing', () => { | ||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
} ); | ||
} ); |
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
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
Oops, something went wrong.