How to use the @wordpress/data.createRegistrySelector function in @wordpress/data

To help you get started, we’ve selected a few @wordpress/data examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
return false;
		}
	}

	return ! getEditedPostContent( state );
}

/**
 * Returns true if the post can be autosaved, or false otherwise.
 *
 * @param {Object} state    Global application state.
 * @param {Object} autosave A raw autosave object from the REST API.
 *
 * @return {boolean} Whether the post can be autosaved.
 */
export const isEditedPostAutosaveable = createRegistrySelector( ( select ) => function( state ) {
	// A post must contain a title, an excerpt, or non-empty content to be valid for autosaving.
	if ( ! isEditedPostSaveable( state ) ) {
		return false;
	}

	// A post is not autosavable when there is a post autosave lock.
	if ( isPostAutosavingLocked( state ) ) {
		return false;
	}

	const postType = getCurrentPostType( state );
	const postId = getCurrentPostId( state );
	const hasFetchedAutosave = select( 'core' ).hasFetchedAutosaves( postType, postId );
	const currentUserId = get( select( 'core' ).getCurrentUser(), [ 'id' ] );

	// Disable reason - this line causes the side-effect of fetching the autosave
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
*
 * @return {?number} ID of the last revision.
 */
export function getCurrentPostLastRevisionId( state ) {
	return get( getCurrentPost( state ), [ '_links', 'predecessor-version', 0, 'id' ], null );
}

/**
 * Returns any post values which have been changed in the editor but not yet
 * been saved.
 *
 * @param {Object} state Global application state.
 *
 * @return {Object} Object of key value pairs comprising unsaved edits.
 */
export const getPostEdits = createRegistrySelector( ( select ) => ( state ) => {
	const postType = getCurrentPostType( state );
	const postId = getCurrentPostId( state );
	return select( 'core' ).getEntityRecordEdits( 'postType', postType, postId ) || EMPTY_OBJECT;
} );

/**
 * Returns a new reference when edited values have changed. This is useful in
 * inferring where an edit has been made between states by comparison of the
 * return values using strict equality.
 *
 * @deprecated since Gutenberg 6.5.0.
 *
 * @example
 *
 * ```
 * const hasEditOccurred = (
github woocommerce / woocommerce-gutenberg-products-block / assets / js / data / schema / selectors.js View on Github external
* @param {Array}  [ids]        This is for any ids that might be implemented in
 *                              the route request. It is not for any query
 *                              parameters.
 *
 * Ids example:
 * If you are looking for the route for a single product on the `wc/blocks`
 * namespace, then you'd have `[ 20 ]` as the ids.  This would produce something
 * like `/wc/blocks/products/20`
 *
 *
 * @throws {Error}  If there is no route for the given arguments, then this will
 *                  throw
 *
 * @return {string} The route if it is available.
 */
export const getRoute = createRegistrySelector(
	( select ) => ( state, namespace, resourceName, ids = [] ) => {
		const hasResolved = select( STORE_KEY ).hasFinishedResolution(
			'getRoutes',
			[ namespace ]
		);
		state = state.routes;
		let error = '';
		if ( ! state[ namespace ] ) {
			error = sprintf(
				'There is no route for the given namespace (%s) in the store',
				namespace
			);
		} else if ( ! state[ namespace ][ resourceName ] ) {
			error = sprintf(
				'There is no route for the given resource name (%s) in the store',
				resourceName
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
if ( isSingleUnmodifiedDefaultBlock ) {
		return [];
	}

	return blocks;
}

/**
 * Returns the content of the post being edited.
 *
 * @param {Object} state Global application state.
 *
 * @return {string} Post content.
 */
export const getEditedPostContent = createRegistrySelector( ( select ) => ( state ) => {
	const postId = getCurrentPostId( state );
	const postType = getCurrentPostType( state );
	const record = select( 'core' ).getEditedEntityRecord(
		'postType',
		postType,
		postId
	);
	if ( record ) {
		if ( typeof record.content === 'function' ) {
			return record.content( record );
		} else if ( record.blocks ) {
			return serializeBlocks( record.blocks );
		} else if ( record.content ) {
			return record.content;
		}
	}
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
}

/**
 * Returns an attribute value of the current autosave revision for a post, or
 * null if there is no autosave for the post.
 *
 * @deprecated since 5.6. Callers should use the `getAutosave( postType, postId, userId )` selector
 * 			   from the '@wordpress/core-data' package and access properties on the returned
 * 			   autosave object using getPostRawValue.
 *
 * @param {Object} state         Global application state.
 * @param {string} attributeName Autosave attribute name.
 *
 * @return {*} Autosave attribute value.
 */
export const getAutosaveAttribute = createRegistrySelector( ( select ) => ( state, attributeName ) => {
	if ( ! includes( AUTOSAVE_PROPERTIES, attributeName ) && attributeName !== 'preview_link' ) {
		return;
	}

	const postType = getCurrentPostType( state );
	const postId = getCurrentPostId( state );
	const currentUserId = get( select( 'core' ).getCurrentUser(), [ 'id' ] );
	const autosave = select( 'core' ).getAutosave( postType, postId, currentUserId );

	if ( autosave ) {
		return getPostRawValue( autosave[ attributeName ] );
	}
} );

/**
 * Returns the current visibility of the post being edited, preferring the
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
* Shared reference to an empty array for cases where it is important to avoid
 * returning a new array reference on every invocation, as in a connected or
 * other pure component which performs `shouldComponentUpdate` check on props.
 * This should be used as a last resort, since the normalized data should be
 * maintained by the reducer result in state.
 */
const EMPTY_ARRAY = [];

/**
 * Returns true if any past editor history snapshots exist, or false otherwise.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether undo history exists.
 */
export const hasEditorUndo = createRegistrySelector( ( select ) => () => {
	return select( 'core' ).hasUndo();
} );

/**
 * Returns true if any future editor history snapshots exist, or false
 * otherwise.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether redo history exists.
 */
export const hasEditorRedo = createRegistrySelector( ( select ) => () => {
	return select( 'core' ).hasRedo();
} );

/**
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
function getBlockEditorSelector( name ) {
	return createRegistrySelector( ( select ) => ( state, ...args ) => {
		deprecated( '`wp.data.select( \'core/editor\' ).' + name + '`', {
			alternative: '`wp.data.select( \'core/block-editor\' ).' + name + '`',
		} );

		return select( 'core/block-editor' )[ name ]( ...args );
	} );
}
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
) );
} );

/**
 * Returns the current autosave, or null if one is not set (i.e. if the post
 * has yet to be autosaved, or has been saved or published since the last
 * autosave).
 *
 * @deprecated since 5.6. Callers should use the `getAutosave( postType, postId, userId )`
 * 			   selector from the '@wordpress/core-data' package.
 *
 * @param {Object} state Editor state.
 *
 * @return {?Object} Current autosave, if exists.
 */
export const getAutosave = createRegistrySelector( ( select ) => ( state ) => {
	deprecated( '`wp.data.select( \'core/editor\' ).getAutosave()`', {
		alternative: '`wp.data.select( \'core\' ).getAutosave( postType, postId, userId )`',
		plugin: 'Gutenberg',
	} );

	const postType = getCurrentPostType( state );
	const postId = getCurrentPostId( state );
	const currentUserId = get( select( 'core' ).getCurrentUser(), [ 'id' ] );
	const autosave = select( 'core' ).getAutosave( postType, postId, currentUserId );
	return mapValues( pick( autosave, AUTOSAVE_PROPERTIES ), getPostRawValue );
} );

/**
 * Returns the true if there is an existing autosave, otherwise false.
 *
 * @deprecated since 5.6. Callers should use the `getAutosave( postType, postId, userId )` selector
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
*/
export const isSavingPost = createRegistrySelector( ( select ) => ( state ) => {
	const postType = getCurrentPostType( state );
	const postId = getCurrentPostId( state );
	return select( 'core' ).isSavingEntityRecord( 'postType', postType, postId );
} );

/**
 * Returns true if a previous post save was attempted successfully, or false
 * otherwise.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether the post was saved successfully.
 */
export const didPostSaveRequestSucceed = createRegistrySelector(
	( select ) => ( state ) => {
		const postType = getCurrentPostType( state );
		const postId = getCurrentPostId( state );
		return ! select( 'core' ).getLastEntitySaveError( 'postType', postType, postId );
	}
);

/**
 * Returns true if a previous post save was attempted but failed, or false
 * otherwise.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether the post save failed.
 */
export const didPostSaveRequestFail = createRegistrySelector(
github WordPress / gutenberg / packages / editor / src / store / selectors.js View on Github external
*
 * @return {boolean} Whether undo history exists.
 */
export const hasEditorUndo = createRegistrySelector( ( select ) => () => {
	return select( 'core' ).hasUndo();
} );

/**
 * Returns true if any future editor history snapshots exist, or false
 * otherwise.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether redo history exists.
 */
export const hasEditorRedo = createRegistrySelector( ( select ) => () => {
	return select( 'core' ).hasRedo();
} );

/**
 * Returns true if the currently edited post is yet to be saved, or false if
 * the post has been saved.
 *
 * @param {Object} state Global application state.
 *
 * @return {boolean} Whether the post is new.
 */
export function isEditedPostNew( state ) {
	return getCurrentPost( state ).status === 'auto-draft';
}

/**