-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Directory: Add error messages inline. (#20001)
* Block Directory: Add error messages inline. * Make the linter happy * Place the retry button on its own line. * Update classNames to match standards * Get the error notices in `DownloadableBlockNotice` This prevents some prop-drilling by getting the notices when we need them * Get `isLoading` in `DownloadableBlockHeader` This prevents some prop-drilling by getting the loading status when we need it * Avoid `children` abstraction in item * Refactor install callback This consolidates the install logic into one callback function, simplifying the list component * Disable the "Add Block" button when installing * Fix broken tests. * Update padding under error messages. Co-authored-by: Kelly Dwan <[email protected]>
- Loading branch information
1 parent
6c80cc3
commit 573767e
Showing
16 changed files
with
599 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/block-directory/src/components/downloadable-block-notice/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Button, Notice } from '@wordpress/components'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { DOWNLOAD_ERROR_NOTICE_ID } from '../../store/constants'; | ||
|
||
export const DownloadableBlockNotice = ( { block, errorNotices, onClick } ) => { | ||
if ( ! errorNotices[ block.id ] ) { | ||
return null; | ||
} | ||
|
||
// A Failed install is the default error as its the first step | ||
let copy = __( 'Block could not be added.' ); | ||
|
||
if ( errorNotices[ block.id ] === DOWNLOAD_ERROR_NOTICE_ID ) { | ||
copy = __( | ||
'Block could not be added. There is a problem with the block.' | ||
); | ||
} | ||
|
||
return ( | ||
<Notice | ||
status="error" | ||
isDismissible={ false } | ||
className="block-directory-downloadable-blocks__notice" | ||
> | ||
<div className="block-directory-downloadable-blocks__notice-content"> | ||
{ copy } | ||
</div> | ||
<Button | ||
isSmall | ||
isPrimary | ||
onClick={ () => { | ||
onClick( block ); | ||
} } | ||
> | ||
{ __( 'Retry' ) } | ||
</Button> | ||
</Notice> | ||
); | ||
}; | ||
|
||
export default withSelect( ( select ) => { | ||
return { | ||
errorNotices: select( 'core/block-directory' ).getErrorNotices(), | ||
}; | ||
} )( DownloadableBlockNotice ); |
8 changes: 8 additions & 0 deletions
8
packages/block-directory/src/components/downloadable-block-notice/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.block-directory-downloadable-blocks__notice { | ||
margin: 0 0 16px; | ||
} | ||
|
||
.block-directory-downloadable-blocks__notice-content { | ||
padding-right: 12px; | ||
margin-bottom: 8px; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/block-directory/src/components/downloadable-block-notice/test/fixtures/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const plugin = { | ||
id: 'boxer-block', | ||
}; |
63 changes: 63 additions & 0 deletions
63
packages/block-directory/src/components/downloadable-block-notice/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { DownloadableBlockNotice } from '../index'; | ||
import { plugin } from './fixtures'; | ||
|
||
import { INSTALL_ERROR_NOTICE_ID } from '../../../store/constants'; | ||
|
||
const getContainer = ( { block, onClick = jest.fn(), errorNotices = {} } ) => { | ||
return shallow( | ||
<DownloadableBlockNotice | ||
block={ block } | ||
onClick={ onClick } | ||
errorNotices={ errorNotices } | ||
/> | ||
); | ||
}; | ||
|
||
describe( 'DownloadableBlockNotice', () => { | ||
describe( 'Rendering', () => { | ||
it( 'should return null when there are no error notices', () => { | ||
const wrapper = getContainer( { block: plugin } ); | ||
expect( wrapper.isEmptyRender() ).toBe( true ); | ||
} ); | ||
|
||
it( 'should return something when there are error notices', () => { | ||
const errorNotices = { | ||
[ plugin.id ]: INSTALL_ERROR_NOTICE_ID, | ||
}; | ||
const wrapper = getContainer( { block: plugin, errorNotices } ); | ||
expect( wrapper.length ).toBeGreaterThan( 0 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Behavior', () => { | ||
it( 'should trigger the callback on button click', () => { | ||
const errorNotices = { | ||
[ plugin.id ]: INSTALL_ERROR_NOTICE_ID, | ||
}; | ||
|
||
const onClick = jest.fn(); | ||
const wrapper = getContainer( { | ||
block: plugin, | ||
onClick, | ||
errorNotices, | ||
} ); | ||
|
||
wrapper.find( Button ).simulate( 'click', { event: {} } ); | ||
|
||
expect( onClick ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.