Skip to content

Commit

Permalink
Editor: Return previewLink from post link only if available (#12800)
Browse files Browse the repository at this point in the history
* Editor: Return previewLink from post link only if available

* Editor: Add CHANGELOG entry for publicly_queryable autosave fix

* Editor: Resolve previewLink lint errors

* Editor: Document autosave error handling
  • Loading branch information
aduth authored and youknowriad committed Jan 3, 2019
1 parent 97b3282 commit 047afb8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/editor/CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
### Bug Fixes

- `getEditedPostAttribute` now correctly returns the merged result of edits as a partial change when given `'meta'` as the `attributeName`.
- Fixes an error and unrecoverable state which occurs on autosave completion for a `'publicly_queryable' => false` post type.

## 9.0.4 (2018-11-30)

Expand Down
14 changes: 10 additions & 4 deletions packages/editor/src/store/reducer.js
Expand Up @@ -1300,17 +1300,23 @@ export function autosave( state = null, action ) {
}

/**
* Reducer returning the poost preview link
* Reducer returning the post preview link.
*
* @param {string?} state The preview link
* @param {Object} action Dispatched action.
* @param {string?} state The preview link
* @param {Object} action Dispatched action.
*
* @return {string?} Updated state.
*/
export function previewLink( state = null, action ) {
switch ( action.type ) {
case 'REQUEST_POST_UPDATE_SUCCESS':
return action.post.preview_link || addQueryArgs( action.post.link, { preview: true } );
if ( action.post.preview_link ) {
return action.post.preview_link;
} else if ( action.post.link ) {
return addQueryArgs( action.post.link, { preview: true } );
}

return state;

case 'REQUEST_POST_UPDATE_START':
// Invalidate known preview link when autosave starts.
Expand Down
66 changes: 66 additions & 0 deletions packages/editor/src/store/test/reducer.js
Expand Up @@ -37,6 +37,7 @@ import {
blockListSettings,
autosave,
postSavingLock,
previewLink,
} from '../reducer';
import { INITIAL_EDITS_DEFAULTS } from '../defaults';

Expand Down Expand Up @@ -2544,4 +2545,69 @@ describe( 'state', () => {
expect( state ).toEqual( {} );
} );
} );

describe( 'previewLink', () => {
it( 'returns null by default', () => {
const state = previewLink( undefined, {} );

expect( state ).toBe( null );
} );

it( 'returns preview link from save success', () => {
const state = previewLink( null, {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: {
preview_link: 'https://example.com/?p=2611&preview=true',
},
} );

expect( state ).toBe( 'https://example.com/?p=2611&preview=true' );
} );

it( 'returns post link with query arg from save success if no preview link', () => {
const state = previewLink( null, {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: {
link: 'https://example.com/sample-post/',
},
} );

expect( state ).toBe( 'https://example.com/sample-post/?preview=true' );
} );

it( 'returns same state if save success without preview link or post link', () => {
// Bug: This can occur for post types which are defined as
// `publicly_queryable => false` (non-viewable).
//
// See: https://github.com/WordPress/gutenberg/issues/12677
const state = previewLink( null, {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: {
preview_link: '',
},
} );

expect( state ).toBe( null );
} );

it( 'returns resets on preview start', () => {
const state = previewLink( 'https://example.com/sample-post/', {
type: 'REQUEST_POST_UPDATE_START',
options: {
isPreview: true,
},
} );

expect( state ).toBe( null );
} );

it( 'returns state on non-preview save start', () => {
const state = previewLink( 'https://example.com/sample-post/', {
type: 'REQUEST_POST_UPDATE_START',
options: {},
} );

expect( state ).toBe( 'https://example.com/sample-post/' );
} );
} );
} );

0 comments on commit 047afb8

Please sign in to comment.