-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 4 commits
d0d8cf9
1a6729b
56feb5e
8aa1b16
ff37316
3338a25
f055731
2bb901f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,23 +73,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 ], | ||
allowedTypes: ALLOWED_MEDIA_TYPES, | ||
onFileChange: ( [ image ] ) => { | ||
setAttributes( { ...image } ); | ||
}, | ||
allowedTypes: ALLOWED_MEDIA_TYPES, | ||
onError: ( message ) => { | ||
this.setState( { hasError: true } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? Thank you @brentswisher for this PR and for the research you made. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We opted to use |
||
noticeOperations.createErrorNotice( message ); | ||
}, | ||
} ); | ||
} | ||
} | ||
|
@@ -120,6 +124,7 @@ class ImageEdit extends Component { | |
} ); | ||
return; | ||
} | ||
this.setState( { hasError: false } ); | ||
this.props.setAttributes( { | ||
...pick( media, [ 'alt', 'id', 'caption', 'url' ] ), | ||
width: undefined, | ||
|
@@ -211,6 +216,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> | ||
|
@@ -237,7 +243,7 @@ class ImageEdit extends Component { | |
</BlockControls> | ||
); | ||
|
||
if ( ! url ) { | ||
if ( ! url || hasError ) { | ||
return ( | ||
<Fragment> | ||
{ controls } | ||
|
There was a problem hiding this comment.
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:
We may be able to avoid the need for a state flag.