How to use @wordpress/data-controls - 10 common examples

To help you get started, we’ve selected a few @wordpress/data-controls 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 / actions.js View on Github external
export function* resetEditorBlocks( blocks, options = {} ) {
	const {
		__unstableShouldCreateUndoLevel,
		selectionStart,
		selectionEnd,
	} = options;
	const edits = { blocks, selectionStart, selectionEnd };

	if ( __unstableShouldCreateUndoLevel !== false ) {
		const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
		const noChange =
			( yield select( 'core', 'getEditedEntityRecord', 'postType', type, id ) )
				.blocks === edits.blocks;
		if ( noChange ) {
			return yield dispatch(
				'core',
				'__unstableCreateUndoLevel',
				'postType',
				type,
				id
			);
		}

		// We create a new function here on every persistent edit
		// to make sure the edit makes the post dirty and creates
		// a new undo level.
github WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
export function* refreshPost() {
	const post = yield select(
		STORE_KEY,
		'getCurrentPost'
	);
	const postTypeSlug = yield select(
		STORE_KEY,
		'getCurrentPostType'
	);
	const postType = yield select(
		'core',
		'getPostType',
		postTypeSlug
	);
	const newPost = yield apiFetch(
		{
			// Timestamp arg allows caller to bypass browser caching, which is
			// expected for this specific function.
github WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
);
	if ( error ) {
		const args = getNotificationArgumentsForSaveFail( {
			post: previousRecord,
			edits,
			error,
		} );
		if ( args.length ) {
			yield dispatch( 'core/notices', 'createErrorNotice', ...args );
		}
	} else {
		const updatedRecord = yield select( STORE_KEY, 'getCurrentPost' );
		const args = getNotificationArgumentsForSaveSuccess( {
			previousPost: previousRecord,
			post: updatedRecord,
			postType: yield select( 'core', 'getPostType', updatedRecord.type ),
			options,
		} );
		if ( args.length ) {
			yield dispatch( 'core/notices', 'createSuccessNotice', ...args );
		}
		// Make sure that any edits after saving create an undo level and are
		// considered for change detection.
		if ( ! options.isAutosave ) {
			yield dispatch( 'core/block-editor', '__unstableMarkLastChangeAsPersistent' );
		}
	}
}
github Automattic / wp-calypso / packages / data-stores / src / verticals-templates / resolvers.ts View on Github external
export function* getTemplates(
	// Resolver has the same signature as corresponding selector without the initial state argument
	verticalId: Parameters< typeof import('./selectors')[ 'getTemplates' ] >[ 1 ]
) {
	const resp = yield apiFetch( {
		url: `https://public-api.wordpress.com/wpcom/v2/verticals/${ verticalId }/templates`,
	} );

	return receiveTemplates( verticalId, resp.templates );
}
github Automattic / wp-calypso / client / landing / gutenboarding / stores / domain-suggestions / resolvers.ts View on Github external
export function* __internalGetDomainSuggestions(
	// Resolver has the same signature as corresponding selector without the initial state argument
	queryObject: Parameters< typeof import('./selectors').__internalGetDomainSuggestions >[ 1 ]
) {
	const url = 'https://public-api.wordpress.com/rest/v1.1/domains/suggestions';

	// If normalized search string (`query`) contains no alphanumerics, endpoint 404s
	if ( ! queryObject.query ) {
		return receiveDomainSuggestions( queryObject, [] );
	}

	// `credentials` and `mode` args are needed since we're accessing the WP.com REST API
	// (rather than the WP Core REST API)
	const suggestions = yield apiFetch( {
		credentials: 'same-origin',
		mode: 'cors',
		url: addQueryArgs( url, queryObject ),
	} );

	return receiveDomainSuggestions( queryObject, suggestions );
}
github Automattic / wp-calypso / client / landing / gutenboarding / stores / onboard / resolvers.ts View on Github external
for ( const langSlug of navigator.languages ) {
			const language = getLanguage( langSlug.toLowerCase() );
			if ( language ) {
				locale = language.langSlug;
				break;
			}
		}
	}

	let localeQueryParam;
	if ( locale && locale !== 'en' ) {
		// v2 api request
		localeQueryParam = { _locale: locale };
	}

	const verticals = yield apiFetch( { url: addQueryArgs( url, localeQueryParam ) } );

	// @TODO: validate and normalize verticals?

	return receiveVerticals( verticals );
}
github WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
export function* refreshPost() {
	const post = yield select(
		STORE_KEY,
		'getCurrentPost'
	);
	const postTypeSlug = yield select(
		STORE_KEY,
		'getCurrentPostType'
	);
	const postType = yield select(
		'core',
		'getPostType',
		postTypeSlug
	);
	const newPost = yield apiFetch(
		{
			// Timestamp arg allows caller to bypass browser caching, which is
			// expected for this specific function.
			path: `/wp/v2/${ postType.rest_base }/${ post.id }` +
				`?context=edit&_timestamp=${ Date.now() }`,
		}
	);
	yield dispatch(
		STORE_KEY,
		'resetPost',
		newPost
	);
}
github php4dev / heroku-wordpress / wp-content / plugins / woocommerce / packages / woocommerce-blocks / assets / js / data / collections / resolvers.js View on Github external
export function* getCollection( namespace, resourceName, query, ids ) {
	const route = yield select(
		SCHEMA_STORE_KEY,
		'getRoute',
		namespace,
		resourceName,
		ids
	);
	const queryString = addQueryArgs( '', query );
	if ( ! route ) {
		yield receiveCollection( namespace, resourceName, queryString, ids );
		return;
	}

	try {
		const {
			response = DEFAULT_EMPTY_ARRAY,
			headers,
github WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
export function* savePost( options = {} ) {
	if ( ! ( yield select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
		return;
	}
	let edits = {
		content: yield select( STORE_KEY, 'getEditedPostContent' ),
	};
	if ( ! options.isAutosave ) {
		yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
	}

	yield __experimentalRequestPostUpdateStart( options );
	const previousRecord = yield select( STORE_KEY, 'getCurrentPost' );
	edits = {
		id: previousRecord.id,
		...( yield select(
			'core',
			'getEntityRecordNonTransientEdits',
github WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
export function* trashPost() {
	const postTypeSlug = yield select(
		STORE_KEY,
		'getCurrentPostType'
	);
	const postType = yield select(
		'core',
		'getPostType',
		postTypeSlug
	);
	yield dispatch(
		'core/notices',
		'removeNotice',
		TRASH_POST_NOTICE_ID
	);
	try {
		const post = yield select(
			STORE_KEY,

@wordpress/data-controls

A set of common controls for the @wordpress/data api.

GPL-2.0-or-later
Latest version published 6 days ago

Package Health Score

95 / 100
Full package analysis