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

File block: Handle drag-and-drop errors #8066

Merged
merged 1 commit into from
Jul 23, 2018
Merged
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
21 changes: 14 additions & 7 deletions core-blocks/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ class FileEdit extends Component {
this.changeShowDownloadButton = this.changeShowDownloadButton.bind( this );

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

componentDidMount() {
const { href } = this.props.attributes;
const { attributes, noticeOperations } = this.props;
const { href } = attributes;

// Upload a file drag-and-dropped into the editor
if ( this.isBlobURL( href ) ) {
Expand All @@ -59,6 +61,10 @@ class FileEdit extends Component {
allowedType: '*',
filesList: [ file ],
onFileChange: ( [ media ] ) => this.onSelectFile( media ),
onError: ( message ) => {
this.setState( { hasError: true } );
Copy link
Member Author

@noisysocks noisysocks Jul 20, 2018

Choose a reason for hiding this comment

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

I had to use local state here instead of setAttributes( { href: undefined } ) because we don't support calling setAttributes() in componentDidMount.

(Also, we probably shouldn't be using attributes for this anyway—see #3553.)

noticeOperations.createErrorNotice( message );
},
} );

revokeBlobURL( href );
Expand All @@ -74,6 +80,7 @@ class FileEdit extends Component {

onSelectFile( media ) {
if ( media && media.url ) {
this.setState( { hasError: false } );
this.props.setAttributes( {
href: media.url,
fileName: media.title,
Expand Down Expand Up @@ -129,14 +136,10 @@ class FileEdit extends Component {
downloadButtonText,
id,
} = attributes;
const { showCopyConfirmation } = this.state;
const { hasError, showCopyConfirmation } = this.state;
const attachmentPage = media && media.link;

const classes = classnames( className, {
'is-transient': this.isBlobURL( href ),
} );

if ( ! href ) {
if ( ! href || hasError ) {
return (
<MediaPlaceholder
icon="media-default"
Expand All @@ -153,6 +156,10 @@ class FileEdit extends Component {
);
}

const classes = classnames( className, {
'is-transient': this.isBlobURL( href ),
} );

return (
<Fragment>
<FileBlockInspector
Expand Down