How to use the @wordpress/data.useDispatch 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 / block-editor / src / components / keyboard-shortcuts / index.js View on Github external
const { clientIds, rootBlocksClientIds } = useSelect( ( select ) => {
		const { getSelectedBlockClientIds, getBlockOrder } = select( 'core/block-editor' );
		return {
			clientIds: getSelectedBlockClientIds(),
			rootBlocksClientIds: getBlockOrder(),
		};
	}, [] );

	const {
		duplicateBlocks,
		removeBlocks,
		insertAfterBlock,
		insertBeforeBlock,
		multiSelect,
		clearSelectedBlock,
	} = useDispatch( 'core/block-editor' );

	// Prevents bookmark all Tabs shortcut in Chrome when devtools are closed.
	// Prevents reposition Chrome devtools pane shortcut when devtools are open.
	useShortcut( 'core/block-editor/duplicate', useCallback( ( event ) => {
		event.preventDefault();
		duplicateBlocks( clientIds );
	}, [ clientIds, duplicateBlocks ] ), { bindGlobal: true } );

	// Does not clash with any known browser/native shortcuts, but preventDefault
	// is used to prevent any obscure unknown shortcuts from triggering.
	useShortcut( 'core/block-editor/remove', useCallback( ( event ) => {
		event.preventDefault();
		removeBlocks( clientIds );
	}, [ clientIds, removeBlocks ] ), { bindGlobal: true } );

	// Does not clash with any known browser/native shortcuts, but preventDefault
github ampproject / amp-wp / assets / src / stories-editor / components / context-menu / index.js View on Github external
getBlockRootClientId,
		getSettings,
	} = useSelect( ( select ) => select( 'core/block-editor' ), [] );

	const {
		getCopiedMarkup,
		getCurrentPage,
	} = useSelect( ( select ) => select( 'amp/story' ), [] );

	const {
		removeBlock,
		insertBlock,
		updateBlockAttributes,
	} = useDispatch( 'core/block-editor' );

	const { setCopiedMarkup } = useDispatch( 'amp/story' );

	const { __experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML, isRTL } = getSettings();

	const isBlockAllowedOnPage = useIsBlockAllowedOnPage();
	const displayPasteError = useDisplayPasteError();

	const copyBlock = ( clientId ) => {
		const block = getBlock( clientId );
		const serialized = serialize( block );

		// Set the copied block to component state for being able to Paste.
		setCopiedMarkup( serialized );
		copyTextToClipBoard( serialized );
	};

	const getPageClientId = ( clientId ) => {
github ampproject / amp-wp / assets / src / stories-editor / components / media-inserter / index.js View on Github external
const _currentPage = getCurrentPage();
		const block = getBlock( _currentPage );

		return {
			currentPage: _currentPage,
			blockOrder: getBlockOrder( _currentPage ),
			// As used in  component
			mediaType: block && block.attributes.mediaType ? block.attributes.mediaType : '',
			allowedVideoMimeTypes: getSettings().allowedVideoMimeTypes,
		};
	}, [] );

	const isBlockAllowedOnPage = useIsBlockAllowedOnPage();

	const { insertBlock, updateBlockAttributes, selectBlock } = useDispatch( 'core/block-editor' );

	const insertBlockOnPage = useCallback( ( name ) => {
		const index = blockOrder.length;

		const insertedBlock = createBlock( name, {} );

		insertBlock( insertedBlock, index, currentPage );
	}, [ blockOrder, currentPage, insertBlock ] );

	const updateBlock = useCallback( ( media ) => {
		if ( ! currentPage ) {
			return;
		}

		const processed = processMedia( media );
		updateBlockAttributes( currentPage, processed );
github ampproject / amp-wp / assets / src / stories-editor / components / editor-carousel / index.js View on Github external
useEffect( () => {
		if ( isReordering ) {
			wrapper.current.style.display = 'none';
		} else {
			wrapper.current.style.display = '';

			// Offset is number of pages times margin+width + plus a half.
			const offset = ( currentIndex + 0.5 ) * ( STORY_PAGE_MARGIN + STORY_PAGE_INNER_WIDTH );
			// Offset (and base value) changes with reading direction.
			const factor = isRTL ? -1 : 1;
			wrapper.current.style.transform = `translateX(calc(${ factor * 50 }% - ${ PAGE_BORDER }px - ${ factor * offset }px))`;
		}
	}, [ currentIndex, isReordering, wrapper, isRTL ] );

	const { setCurrentPage } = useDispatch( 'amp/story' );
	const { selectBlock } = useDispatch( 'core/block-editor' );

	const goToPage = ( page ) => {
		setCurrentPage( page );
		selectBlock( page );
	};

	if ( isReordering ) {
		return ;
	}

	return (
		
			<div>
				</div>
github DefinitelyTyped / DefinitelyTyped / types / wordpress__data / wordpress__data-tests.tsx View on Github external
const HookComponent = () =&gt; {
    const isTyping = data.useSelect(select =&gt; select('core/block-editor').isTyping());
    const { resetBlocks } = data.useDispatch('core/block-editor');
    const dispatch = data.useDispatch();
    return (
        <button disabled="{isTyping}"> dispatch('core/data').resetBlocks()}&gt;
            Reset
        </button>
    );
};
github WordPress / gutenberg / packages / block-library / src / columns / edit.js View on Github external
const { blockType, defaultPattern, hasInnerBlocks, patterns } = useSelect( ( select ) =&gt; {
		const {
			__experimentalGetBlockPatterns,
			getBlockType,
			__experimentalGetDefaultBlockPattern,
		} = select( 'core/blocks' );

		return {
			blockType: getBlockType( name ),
			defaultPattern: __experimentalGetDefaultBlockPattern( name ),
			hasInnerBlocks: select( 'core/block-editor' ).getBlocks( clientId ).length &gt; 0,
			patterns: __experimentalGetBlockPatterns( name ),
		};
	}, [ clientId, name ] );

	const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );

	if ( hasInnerBlocks ) {
		return (
			
		);
	}

	return (
		&lt;__experimentalBlockPatternPicker
			icon={ get( blockType, [ 'icon', 'src' ] ) }
			label={ get( blockType, [ 'title' ] ) }
			patterns={ patterns }
			onSelect={ ( nextPattern = defaultPattern ) =&gt; {
				if ( nextPattern.attributes ) {
					props.setAttributes( nextPattern.attributes );
				}
github dsifford / academic-bloggers-toolkit / src / js / gutenberg / sidebar / sidebar.tsx View on Github external
export default function Sidebar() {
    const { parseCitations, parseFootnotes, updateReference } = useDispatch(
        'abt/data',
    );
    const { toggleItemSelected } = useDispatch('abt/ui');

    const {
        citedItems,
        footnotes,
        isTyping,
        selectedItems,
        uncitedItems,
    } = useSelect(select => {
        const { getCitedItems, getFootnotes, getSortedItems } = select(
            'abt/data',
        );
        const {
            getSelectedItems,
github eventespresso / event-espresso-core / assets / src / hooks / use-copy-ticket.js View on Github external
const useCopyTicket = ( ticketEntity, dateEntities ) => {
	const { createEntity } = useDispatch( 'eventespresso/core' );
	const relatedPrices = useSelect( ( select ) => {
		const { getRelatedEntities } = select( 'eventespresso/core' );
		return getRelatedEntities( ticketEntity, 'prices' );
	}, [ ticketEntity ] );
	const newPrices = useCloneEntities( relatedPrices, 'price' );
	const updateTicketDateRelations = useCreateRelationsForTicketToEventDates();
	const updateTicketPriceRelations = useCreateRelationsForTicketToPrices();
	return useCallback( async () => {
		if ( ! isModelEntityOfModel( ticketEntity, 'ticket' ) ) {
			return falseFunc;
		}
		return async () => {
			const newTicket = await createEntity(
				'ticket',
				ticketEntity.forClone
			);
github WordPress / gutenberg / packages / edit-widgets / src / components / notices / index.js View on Github external
function Notices() {
	const { notices } = useSelect( ( select ) =&gt; {
		return {
			notices: select( 'core/notices' ).getNotices(),
		};
	}, [] );
	const snackbarNotices = filter( notices, {
		type: 'snackbar',
	} );
	const { removeNotice } = useDispatch( 'core/notices' );

	return (
		
	);
}
github dsifford / academic-bloggers-toolkit / src / js / gutenberg / sidebar / toolbar.tsx View on Github external
export default function Toolbar({ selectedItems }: Props) {
    const { addReferences, removeFootnotes, removeReferences } = useDispatch(
        'abt/data',
    );
    const { clearSelectedItems } = useDispatch('abt/ui');
    const { createErrorNotice } = useDispatch('core/notices');
    const removeSelectedItems = useCallback(() =&gt; {
        removeReferences([...selectedItems]);
        removeFootnotes([...selectedItems]);
        clearSelectedItems();
    }, [selectedItems]);
    return (
        &lt;&gt;