Skip to content

Commit

Permalink
Code Quality: Use store definition instead of string for notices pack…
Browse files Browse the repository at this point in the history
…ages (#27548)

* Code Quality: Use store definition instead of string for notices packages

* Add missing import statements

* Remove obsolete import statements for packages with stores
  • Loading branch information
gziolo authored Dec 10, 2020
1 parent c043d0f commit c7b6701
Show file tree
Hide file tree
Showing 36 changed files with 63 additions and 56 deletions.
5 changes: 0 additions & 5 deletions packages/block-directory/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import '@wordpress/notices';

/**
* Internal dependencies
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { store as blocksStore } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -144,7 +145,7 @@ export function* uninstallBlockType( block ) {
yield removeInstalledBlockType( block );
} catch ( error ) {
yield controls.dispatch(
'core/notices',
noticesStore,
'createErrorNotice',
error.message || __( 'An error occurred.' )
);
Expand Down
3 changes: 2 additions & 1 deletion packages/block-directory/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -281,7 +282,7 @@ describe( 'actions', () => {
expect( generator.throw( apiError ).value ).toMatchObject( {
type: '@@data/DISPATCH',
actionName: 'createErrorNotice',
storeKey: 'core/notices',
storeKey: noticesStore,
} );

expect( generator.next() ).toEqual( {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@wordpress/dom';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, _n, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -28,7 +29,7 @@ export function useNotifyCopy() {
( select ) => select( blocksStore ),
[]
);
const { createSuccessNotice } = useDispatch( 'core/notices' );
const { createSuccessNotice } = useDispatch( noticesStore );

return useCallback( ( eventType, selectedBlockClientIds ) => {
let notice = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useCallback } from '@wordpress/element';
import { cloneBlock } from '@wordpress/blocks';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

/**
* Retrieves the block patterns inserter state.
Expand All @@ -29,7 +30,7 @@ const usePatternsState = ( onInsert ) => {
patternCategories: __experimentalBlockPatternCategories,
};
}, [] );
const { createSuccessNotice } = useDispatch( 'core/notices' );
const { createSuccessNotice } = useDispatch( noticesStore );
const onClickPattern = useCallback( ( pattern, blocks ) => {
onInsert(
map( blocks, ( block ) => cloneBlock( block ) ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { withDispatch, useSelect } from '@wordpress/data';
import { DOWN, TAB, ESCAPE } from '@wordpress/keycodes';
import { compose } from '@wordpress/compose';
import { upload, media as mediaIcon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -205,7 +206,7 @@ const MediaReplaceFlow = ( {

export default compose( [
withDispatch( ( dispatch ) => {
const { createNotice, removeNotice } = dispatch( 'core/notices' );
const { createNotice, removeNotice } = dispatch( noticesStore );
return {
createNotice,
removeNotice,
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import '@wordpress/rich-text';
import '@wordpress/notices';

/**
* Internal dependencies
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { store as noticesStore } from '@wordpress/notices';
import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';

/**
Expand Down Expand Up @@ -76,7 +77,7 @@ export default function ReusableBlockEdit( {
} = useDispatch( reusableBlocksStore );

const { createSuccessNotice, createErrorNotice } = useDispatch(
'core/notices'
noticesStore
);
const save = useCallback( async function () {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import apiFetch from '@wordpress/api-fetch';
import { useDispatch } from '@wordpress/data';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

export default function useSaveImage( {
crop,
Expand All @@ -17,7 +18,7 @@ export default function useSaveImage( {
onSaveImage,
onFinishEditing,
} ) {
const { createErrorNotice } = useDispatch( 'core/notices' );
const { createErrorNotice } = useDispatch( noticesStore );
const [ isInProgress, setIsInProgress ] = useState( false );

const cancel = useCallback( () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { getPath } from '@wordpress/url';
import { createBlock } from '@wordpress/blocks';
import { crop, upload } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -119,7 +120,7 @@ export default function Image( {
} );
const { toggleSelection } = useDispatch( 'core/block-editor' );
const { createErrorNotice, createSuccessNotice } = useDispatch(
'core/notices'
noticesStore
);
const isLargeViewport = useViewportMatch( 'medium' );
const [ captionFocused, setCaptionFocused ] = useState( false );
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import '@wordpress/core-data';
import '@wordpress/notices';
import '@wordpress/block-editor';
import {
registerBlockType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { BlockPreview } from '@wordpress/block-editor';
import { Icon } from '@wordpress/components';
import { useAsyncList } from '@wordpress/compose';
import { store as noticesStore } from '@wordpress/notices';

/**
* External dependencies
Expand Down Expand Up @@ -36,7 +37,7 @@ function TemplatePartItem( {
// The fallback prevents an error in the parse function while saving.
const content = templatePart.content.raw || '';
const blocks = useMemo( () => parse( content ), [ content ] );
const { createSuccessNotice } = useDispatch( 'core/notices' );
const { createSuccessNotice } = useDispatch( noticesStore );

const onClick = useCallback( () => {
setAttributes( { postId: id, slug, theme } );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import { useState } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { TextControl, Button } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

const menuNameMatches = ( menuName ) => ( menu ) =>
menu.name.toLowerCase() === menuName.toLowerCase();

export default function AddMenuForm( { menus, onCreate } ) {
const [ menuName, setMenuName ] = useState( '' );

const { createErrorNotice, createInfoNotice } = useDispatch(
'core/notices'
);
const { createErrorNotice, createInfoNotice } = useDispatch( noticesStore );

const [ isCreatingMenu, setIsCreatingMenu ] = useState( false );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';

export default function useMenuNotifications( menuId ) {
const { lastSaveError, lastDeleteError } = useSelect(
Expand All @@ -20,7 +21,7 @@ export default function useMenuNotifications( menuId ) {
[ menuId ]
);

const { createErrorNotice } = useDispatch( 'core/notices' );
const { createErrorNotice } = useDispatch( noticesStore );

const processError = ( error ) => {
const document = new window.DOMParser().parseFromString(
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-navigation/src/components/notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { filter } from 'lodash';
*/
import { NoticeList, SnackbarList } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

export default function EditNavigationNotices() {
const { removeNotice } = useDispatch( 'core/notices' );
const { removeNotice } = useDispatch( noticesStore );
const notices = useSelect(
( select ) => select( 'core/notices' ).getNotices(),
( select ) => select( noticesStore ).getNotices(),
[]
);
const dismissibleNotices = filter( notices, {
Expand Down
1 change: 0 additions & 1 deletion packages/edit-navigation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { map, set, flatten, omit, partialRight } from 'lodash';
/**
* WordPress dependencies
*/
import '@wordpress/notices';
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-navigation/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { v4 as uuid } from 'uuid';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -96,7 +97,7 @@ export const saveNavigationPost = serializeProcessing( function* ( post ) {
throw new Error();
}
yield dispatch(
'core/notices',
noticesStore,
'createSuccessNotice',
__( 'Navigation saved.' ),
{
Expand All @@ -105,7 +106,7 @@ export const saveNavigationPost = serializeProcessing( function* ( post ) {
);
} catch ( e ) {
yield dispatch(
'core/notices',
noticesStore,
'createErrorNotice',
__( 'There was an error.' ),
{
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-navigation/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -346,7 +347,7 @@ describe( 'saveNavigationPost', () => {

expect( action.next( { success: true } ).value ).toEqual(
dispatch(
'core/notices',
noticesStore,
'createSuccessNotice',
__( 'Navigation saved.' ),
{
Expand Down Expand Up @@ -476,7 +477,7 @@ describe( 'saveNavigationPost', () => {

expect( action.next( { success: false } ).value ).toEqual(
dispatch(
'core/notices',
noticesStore,
'createErrorNotice',
__( 'There was an error.' ),
{
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/admin-notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { Component } from '@wordpress/element';
import { withDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

/**
* Mapping of server-supported notice class names to an equivalent notices
Expand Down Expand Up @@ -103,7 +104,7 @@ export class AdminNotices extends Component {
}

export default withDispatch( ( dispatch ) => {
const { createNotice } = dispatch( 'core/notices' );
const { createNotice } = dispatch( noticesStore );

return { createNotice };
} )( AdminNotices );
1 change: 0 additions & 1 deletion packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import '@wordpress/core-data';
import '@wordpress/block-editor';
import '@wordpress/editor';
import '@wordpress/notices';
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
Expand Down
1 change: 0 additions & 1 deletion packages/edit-post/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import '@wordpress/core-data';
import '@wordpress/notices';
import '@wordpress/format-library';
import { render } from '@wordpress/element';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { withDispatch, withSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useCopyOnClick, compose, ifCondition } from '@wordpress/compose';
import { useRef, useEffect } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';

function CopyContentMenuItem( { createNotice, editedPostContent } ) {
const ref = useRef();
Expand Down Expand Up @@ -36,7 +37,7 @@ export default compose(
),
} ) ),
withDispatch( ( dispatch ) => {
const { createNotice } = dispatch( 'core/notices' );
const { createNotice } = dispatch( noticesStore );

return {
createNotice,
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-site/src/components/notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { SnackbarList } from '@wordpress/components';
import { store as noticesStore } from '@wordpress/notices';

export default function Notices() {
const notices = useSelect(
( select ) =>
select( 'core/notices' )
select( noticesStore )
.getNotices()
.filter( ( notice ) => notice.type === 'snackbar' ),
[]
);
const { removeNotice } = useDispatch( 'core/notices' );
const { removeNotice } = useDispatch( noticesStore );
return (
<SnackbarList
className="edit-site-notices"
Expand Down
1 change: 0 additions & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import '@wordpress/notices';
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-widgets/src/components/notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import { filter } from 'lodash';
*/
import { SnackbarList } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

function Notices() {
const { notices } = useSelect( ( select ) => {
return {
notices: select( 'core/notices' ).getNotices(),
notices: select( noticesStore ).getNotices(),
};
}, [] );
const snackbarNotices = filter( notices, {
type: 'snackbar',
} );
const { removeNotice } = useDispatch( 'core/notices' );
const { removeNotice } = useDispatch( noticesStore );

return (
<SnackbarList
Expand Down
1 change: 0 additions & 1 deletion packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
registerBlockType,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
} from '@wordpress/blocks';
import '@wordpress/notices';
import { render } from '@wordpress/element';
import {
registerCoreBlocks,
Expand Down
Loading

0 comments on commit c7b6701

Please sign in to comment.