Skip to content

Commit

Permalink
Add error notices mechanism to media placeholder; Make upload error n…
Browse files Browse the repository at this point in the history
…otices available in all blocks. (#6957)

Making error notices available directly in media placeholder makes all blocks even the ones created by the community take advantage of our upload error notices system.
Blocks can opt-out of this behavior and manage the notices themselves like the gallery is doing.
Gallery manages the upload error notices, because in the media placeholder for a gallery we may select many files, some may have been uploaded correctly and in some, we may have errors. When some file is uploaded correctly we stop displaying the placeholder, so if a file upload went wrong we would not display the notice. Given that the notices for the gallery are managed at the block level that problem does not happen.
  • Loading branch information
jorgefilipecosta authored Jun 18, 2018
1 parent 60b57db commit a5ceee2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
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 );

0 comments on commit a5ceee2

Please sign in to comment.