Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error notices mechanism directly to media placeholder #6957

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core-blocks/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ class GalleryEdit extends Component {
onSelect={ this.onSelectImages }
accept="image/*"
type="image"
disableMaxUploadErrorMessages
multiple
notices={ noticeUI }
additionalNotices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
</Fragment>
Expand Down
7 changes: 1 addition & 6 deletions core-blocks/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
TextControl,
TextareaControl,
Toolbar,
withNotices,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -178,7 +177,7 @@ class ImageEdit extends Component {
}

render() {
const { attributes, setAttributes, isLargeViewport, isSelected, className, maxWidth, noticeOperations, noticeUI, toggleSelection } = this.props;
const { attributes, setAttributes, isLargeViewport, isSelected, className, maxWidth, toggleSelection } = this.props;
const { url, alt, caption, align, id, href, width, height } = attributes;

const controls = (
Expand Down Expand Up @@ -221,8 +220,6 @@ class ImageEdit extends Component {
} }
className={ className }
onSelect={ this.onSelectImage }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
accept="image/*"
type="image"
/>
Expand Down Expand Up @@ -318,7 +315,6 @@ class ImageEdit extends Component {
return (
<Fragment>
{ controls }
{ noticeUI }
<figure className={ classes }>
<ImageSize src={ url } dirtynessTrigger={ align }>
{ ( sizes ) => {
Expand Down Expand Up @@ -419,5 +415,4 @@ export default compose( [
};
} ),
withViewportMatch( { isLargeViewport: 'medium' } ),
withNotices,
] )( ImageEdit );
27 changes: 21 additions & 6 deletions editor/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
FormFileUpload,
Placeholder,
DropZone,
withNotices,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { Component, Fragment } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -63,13 +64,26 @@ class MediaPlaceholder extends Component {
}

onFilesUpload( files ) {
const { onSelect, type, multiple, onError } = this.props;
/**
* We use a prop named `disable`, set to `false` by default, because it makes for a nicer
* component prop API. eg:
*
* <MediaPlaceholder disableMaxUploadErrorMessages />
* instead of:
* <MediaPlaceholder enableMaxUploadErrorMessages={ false } />
*/
const { onSelect, type, multiple, onError = noop, disableMaxUploadErrorMessages = false, noticeOperations } = this.props;
const setMedia = multiple ? onSelect : ( [ media ] ) => onSelect( media );
editorMediaUpload( {
allowedType: type,
filesList: files,
onFileChange: setMedia,
onError,
onError: ( errorMessage ) => {
onError( errorMessage );
if ( disableMaxUploadErrorMessages === false ) {
noticeOperations.createErrorNotice( errorMessage );
}
},
} );
}

Expand All @@ -85,7 +99,8 @@ class MediaPlaceholder extends Component {
onSelectUrl,
onHTMLDrop = noop,
multiple = false,
notices,
additionalNotices,
noticeUI,
} = this.props;

return (
Expand All @@ -94,7 +109,7 @@ class MediaPlaceholder extends Component {
label={ labels.title }
instructions={ sprintf( __( 'Drag %s, upload a new one or select a file from your library.' ), labels.name ) }
className={ classnames( 'editor-media-placeholder', className ) }
notices={ notices }
notices={ <Fragment>{ additionalNotices }{ noticeUI }</Fragment> }
>
<DropZone
onFilesDrop={ this.onFilesUpload }
Expand Down Expand Up @@ -141,4 +156,4 @@ class MediaPlaceholder extends Component {
}
}

export default MediaPlaceholder;
export default withNotices( MediaPlaceholder );