How to use the @wordpress/data.dispatch 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 insideout10 / wordlift-plugin / src / src / js / src / block-editor / stores / sagas.js View on Github external
function* toggleEntity({ entity }) {
  // Get the supported blocks.
  const blocks = Blocks.create(data.select(EDITOR_STORE).getBlocks(), data.dispatch(EDITOR_STORE));

  const mainType = entity.mainType || "thing";
  const onClassNames = ["disambiguated", `wl-${mainType.replace(/\s/, "-")}`];

  // Build a css selector to select all the annotations for the provided entity.
  const annotationSelector = makeEntityAnnotationsSelector(entity);

  // Collect the annotations that have been switch on/off.
  const occurrences = [];

  if (0 === entity.occurrences.length) {
    // Switch on.
    blocks.replace(
      new RegExp(``, "gi"),
      (match, annotationId, classNames) => {
        const newClassNames = mergeArray(classNames.split(/\s+/), onClassNames);
github Automattic / vip-go-mu-plugins-built / jetpack / extensions / blocks / simple-payments / edit.js View on Github external
saveProduct() {
		if ( this.state.isSavingProduct ) {
			return;
		}

		const { attributes, setAttributes } = this.props;
		const { email } = attributes;
		const { saveEntityRecord } = dispatch( 'core' );

		this.setState( { isSavingProduct: true }, () => {
			saveEntityRecord( 'postType', SIMPLE_PAYMENTS_PRODUCT_POST_TYPE, this.toApi() )
				.then( record => {
					if ( record ) {
						setAttributes( { productId: record.id } );
					}

					return record;
				} )
				.catch( error => {
					// Nothing we can do about errors without details at the moment
					if ( ! error || ! error.data ) {
						return;
					}
github zgordon / advanced-gutenberg-course / src / index.js View on Github external
.forEach(({ clientId }) => {
			dispatch("core/editor").selectBlock(clientId);
		});
	// Reselect whatever was selected in the beginning.
github moderntribe / events-gutenberg / plugins / events-pro / src / modules / data / status / __tests__ / sagas.spec.js View on Github external
expect( gen.next().value ).toEqual(
				put( disableEdits() )
			);

			expect( gen.next().value ).toEqual(
				call( sagas.fetchStatus )
			);

			const response = { done: false, items_created: 1, last_created_at: '2019-11-07 00:00:00' };
			expect( gen.next( response ).value ).toEqual(
				put( actions.setSeriesQueueStatus( response ) )
			);

			expect( gen.next().value ).toEqual(
				call(
					[ wpDispatch( 'core/notices' ), 'createSuccessNotice' ],
					sagas.NOTICES[ sagas.NOTICE_EDITING_SERIES ],
					{ id: sagas.NOTICE_EDITING_SERIES, isDismissible: false }
				)
			);

			expect( gen.next().value ).toEqual(
				 call(
					[ wpDispatch( 'core/notices' ), 'createSuccessNotice' ],
					`${ sprintf( sagas.NOTICES[ sagas.NOTICE_PROGRESS_ON_SERIES_CREATION_COUNT ], response.items_created ) } ${ sprintf( sagas.NOTICES[ sagas.NOTICE_PROGRESS_ON_SERIES_CREATION ], momentUtils.toDate( momentUtils.toMoment( response.last_created_at ) ) ) }`,
					{ id: sagas.NOTICE_PROGRESS_ON_SERIES_CREATION, isDismissible: true }
				)
			);

			expect( gen.next( false ).value ).toEqual(
				select( selectors.isCompleted )
			);
github Automattic / wp-calypso / apps / wpcom-block-editor / src / calypso / iframe-bridge-server.js View on Github external
port1.onmessage = ( { data: confirmed } ) => {
			port1.close();

			if ( confirmed !== true ) {
				return;
			}

			dispatch( 'core/editor' ).replaceBlock(
				blocks[ 0 ].clientId,
				rawHandler( {
					HTML: blocks[ 0 ].originalContent,
				} )
			);
		};
	} );
github ampproject / amp-wp / blocks / amp-story / amp-story-cta-layer.js View on Github external
const blockIndex = getBlockIndex( rootClientID );
				let noticeMessage = null;
				const noticeOptions = {
					id: 'amp-errors-notice-removed-cta',
				};

				if ( this.props.attributes.hasMultipleCtaBlocks ) {
					removeBlock( this.props.clientId );
					noticeMessage = __( 'Multiple CTA Layers are not allowed, the block was removed.', 'amp' );
				} else if ( 0 === blockIndex ) {
					removeBlock( this.props.clientId );
					noticeMessage = __( 'CTA layer is not allowed on the first page, the block was removed.', 'amp' );
				}

				if ( noticeMessage ) {
					dispatch( 'core/notices' ).createNotice( 'warning', noticeMessage, noticeOptions );
				}
			}
github LearnPress / learnpress / assets / src / js / frontend / quiz / store / actions.js View on Github external
function _dispatch() {
	const args = [].slice.call( arguments, 2 );
	const d = wpDispatch( arguments[ 0 ] );
	const f = arguments[ 1 ];
	d[ f ]( ...args );
}
github WordPress / gutenberg / packages / editor / src / store / effects / reusable-blocks.js View on Github external
const method = isTemporary ? 'POST' : 'PUT';

	try {
		const updatedReusableBlock = await apiFetch( { path, data, method } );
		dispatch( {
			type: 'SAVE_REUSABLE_BLOCK_SUCCESS',
			updatedId: updatedReusableBlock.id,
			id,
		} );
		const message = isTemporary ? __( 'Block created.' ) : __( 'Block updated.' );
		dataDispatch( 'core/notices' ).createSuccessNotice( message, {
			id: REUSABLE_BLOCK_NOTICE_ID,
			type: 'snackbar',
		} );

		dataDispatch( 'core/block-editor' ).__unstableSaveReusableBlock( id, updatedReusableBlock.id );
	} catch ( error ) {
		dispatch( { type: 'SAVE_REUSABLE_BLOCK_FAILURE', id } );
		dataDispatch( 'core/notices' ).createErrorNotice( error.message, {
			id: REUSABLE_BLOCK_NOTICE_ID,
		} );
	}
};
github dsifford / academic-bloggers-toolkit / src / js / stores / data / actions.ts View on Github external
function* setBibliography({ items, meta }: Processor.Bibliography) {
    const blocksList = select('core/block-editor').getBlocks();
    const bibliographyBlock = blocksList.find(
        block => block.name === 'abt/bibliography',
    );
    if (items.length > 0 && bibliographyBlock) {
        yield dispatch('core/block-editor').updateBlockAttributes(
            bibliographyBlock.clientId,
            { ...meta, items },
        );
    } else if (items.length > 0 && !bibliographyBlock) {
        yield dispatch('core/block-editor').insertBlock(
            createBlock('abt/bibliography', {
                ...meta,
                items,
            }),
            blocksList.length,
            undefined,
            false,
        );
    } else if (items.length === 0 && bibliographyBlock) {
        yield dispatch('core/block-editor').removeBlock(
            bibliographyBlock.clientId,
github eventespresso / event-espresso-core / assets / src / editor / events / tickets / editor-ticket / action-handlers / trash-ticket.js View on Github external
const { MODEL_NAME: TICKET } = ticketModel;
	if ( ! isModelEntityOfModel( ticketEntity, TICKET ) ) {
		return;
	}
	if ( ! window.confirm(
		__(
			'Are you sure you want to delete this Ticket?',
			'event_espresso'
		)
	) ) {
		return;
	}
	const {
		trashEntityById,
		persistTrashesForModel,
	} = dispatch( 'eventespresso/core' );
	await trashEntityById(
		TICKET,
		ticketEntity.id
	);
	persistTrashesForModel( TICKET );
};