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 an error state to the image block to allow upload errors to display #10224

Merged
merged 8 commits into from
Dec 10, 2018
14 changes: 10 additions & 4 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,27 @@ class ImageEdit extends Component {

this.state = {
captionFocused: false,
hasError: false,
};
}

componentDidMount() {
const { attributes, setAttributes } = this.props;
const { attributes, setAttributes, noticeOperations } = this.props;
const { id, url = '' } = attributes;

if ( ! id && url.indexOf( 'blob:' ) === 0 ) {
const file = getBlobByURL( url );

if ( file ) {
mediaUpload( {
filesList: [ file ],
allowedType: 'image',
onFileChange: ( [ image ] ) => {
setAttributes( { ...image } );
},
allowedType: 'image',
onError: ( message ) => {
this.setState( { hasError: true } );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if make a call here similar to:

						noticeOperations.createErrorNotice( message );
						this.props.setAttributes( {
							url: undefined,
							alt: undefined,
							id: undefined,
							caption: undefined,
						} );

We may be able to avoid the need for a state flag.

Copy link
Member

@jorgefilipecosta jorgefilipecosta Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a complicated problem. If we apply a setAttributes here, the attributes are changed in the store, but the components are not rerendered even if caching is disabled on the getBlocks selector. That's why there is a delay on the notice appearing when we use the setAttributes approach.

@noisysocks given that the state approach was also used on the file blocks do you have any insights on why setAttributes does not work for this cases?
I'm fine with the approach being proposed here my only fear is that the fact the setAttributes approach does not work well may be related to some problem that may have an impact in other areas.

Thank you @brentswisher for this PR and for the research you made.

Copy link
Member

@noisysocks noisysocks Nov 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We opted to use setState() for the File block because using setAttributes() causes a useless undo level to be created.

noticeOperations.createErrorNotice( message );
},
} );
}
}
Expand Down Expand Up @@ -119,6 +123,7 @@ class ImageEdit extends Component {
} );
return;
}
this.setState( { hasError: false } );
this.props.setAttributes( {
...pick( media, [ 'alt', 'id', 'caption', 'url' ] ),
width: undefined,
Expand Down Expand Up @@ -210,6 +215,7 @@ class ImageEdit extends Component {
render() {
const { attributes, setAttributes, isLargeViewport, isSelected, className, maxWidth, noticeOperations, noticeUI, toggleSelection, isRTL } = this.props;
const { url, alt, caption, align, id, href, linkDestination, width, height } = attributes;
const { hasError } = this.state;

const controls = (
<BlockControls>
Expand All @@ -236,7 +242,7 @@ class ImageEdit extends Component {
</BlockControls>
);

if ( ! url ) {
if ( ! url || hasError ) {
return (
<Fragment>
{ controls }
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class BlockDropZone extends Component {
getBlockTransforms( 'from' ),
( transform ) => transform.type === 'files' && transform.isMatch( files )
);

if ( transformation ) {
const insertIndex = this.getInsertIndex( position );
const blocks = transformation.transform( files, this.props.updateBlockAttributes );
Expand Down