Skip to content

Commit

Permalink
Fix bug where server side upload errors may disapear automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 19, 2018
1 parent 5a65727 commit 5950495
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
15 changes: 12 additions & 3 deletions core-blocks/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton, Toolbar } from '@wordpress/components';
import { IconButton, Toolbar, withNotices } from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import {
MediaPlaceholder,
Expand All @@ -15,7 +15,7 @@ import {
*/
import './editor.scss';

export default class AudioEdit extends Component {
class AudioEdit extends Component {
constructor() {
super( ...arguments );
// edit component has its own src in the state so it can be edited
Expand All @@ -27,7 +27,7 @@ export default class AudioEdit extends Component {

render() {
const { caption, src } = this.props.attributes;
const { setAttributes, isSelected, className } = this.props;
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props;
const { editing } = this.state;
const switchToEditing = () => {
this.setState( { editing: true } );
Expand All @@ -38,7 +38,12 @@ export default class AudioEdit extends Component {
// selected media, then switches off the editing UI
setAttributes( { src: media.url, id: media.id } );
this.setState( { src: media.url, editing: false } );
return;
}
// in this case there was an error and we should continue in the editing state
// previous attributes should be removed because they may be temporary blob urls
setAttributes( { src: undefined, id: undefined } );
switchToEditing();
};
const onSelectUrl = ( newSrc ) => {
// set the block's src from the edit component's state, and switch off the editing UI
Expand All @@ -62,6 +67,8 @@ export default class AudioEdit extends Component {
accept="audio/*"
type="audio"
value={ this.props.attributes }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
);
}
Expand Down Expand Up @@ -96,3 +103,5 @@ export default class AudioEdit extends Component {
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}

export default withNotices( AudioEdit );
16 changes: 12 additions & 4 deletions core-blocks/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { IconButton, PanelBody, RangeControl, ToggleControl, Toolbar } from '@wordpress/components';
import { IconButton, PanelBody, RangeControl, ToggleControl, Toolbar, withNotices } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
Expand Down Expand Up @@ -99,10 +99,16 @@ export const settings = {
}
},

edit( { attributes, setAttributes, isSelected, className } ) {
edit: withNotices( ( { attributes, setAttributes, isSelected, className, noticeOperations, noticeUI } ) => {
const { url, title, align, contentAlign, id, hasParallax, dimRatio } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSelectImage = ( media ) => setAttributes( { url: media.url, id: media.id } );
const onSelectImage = ( media ) => {
if ( media ) {
setAttributes( { url: media.url, id: media.id } );
return;
}
setAttributes( { url: undefined, id: undefined } );
};
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } );

Expand Down Expand Up @@ -193,6 +199,8 @@ export const settings = {
onSelect={ onSelectImage }
accept="image/*"
type="image"
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
</Fragment>
);
Expand All @@ -219,7 +227,7 @@ export const settings = {
</div>
</Fragment>
);
},
} ),

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
Expand Down
3 changes: 1 addition & 2 deletions core-blocks/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ class GalleryEdit extends Component {
onSelect={ this.onSelectImages }
accept="image/*"
type="image"
disableMaxUploadErrorMessages
multiple
additionalNotices={ noticeUI }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
</Fragment>
Expand Down
6 changes: 5 additions & 1 deletion core-blocks/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TextControl,
TextareaControl,
Toolbar,
withNotices,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -177,7 +178,7 @@ class ImageEdit extends Component {
}

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

const controls = (
Expand Down Expand Up @@ -220,6 +221,8 @@ class ImageEdit extends Component {
} }
className={ className }
onSelect={ this.onSelectImage }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
accept="image/*"
type="image"
/>
Expand Down Expand Up @@ -415,4 +418,5 @@ export default compose( [
};
} ),
withViewportMatch( { isLargeViewport: 'medium' } ),
withNotices,
] )( ImageEdit );
15 changes: 12 additions & 3 deletions core-blocks/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton, Toolbar } from '@wordpress/components';
import { IconButton, Toolbar, withNotices } from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import {
MediaPlaceholder,
Expand All @@ -15,7 +15,7 @@ import {
*/
import './editor.scss';

export default class VideoEdit extends Component {
class VideoEdit extends Component {
constructor() {
super( ...arguments );
// edit component has its own src in the state so it can be edited
Expand All @@ -27,7 +27,7 @@ export default class VideoEdit extends Component {

render() {
const { caption, src } = this.props.attributes;
const { setAttributes, isSelected, className } = this.props;
const { setAttributes, isSelected, className, noticeOperations, noticeUI } = this.props;
const { editing } = this.state;
const switchToEditing = () => {
this.setState( { editing: true } );
Expand All @@ -38,7 +38,12 @@ export default class VideoEdit extends Component {
// selected media, then switches off the editing UI
setAttributes( { src: media.url, id: media.id } );
this.setState( { src: media.url, editing: false } );
return;
}
// in this case there was an error and we should continue in the editing state
// previous attributes should be removed because they may be temporary blob urls
setAttributes( { src: undefined, id: undefined } );
switchToEditing();
};
const onSelectUrl = ( newSrc ) => {
// set the block's src from the edit component's state, and switch off the editing UI
Expand All @@ -62,6 +67,8 @@ export default class VideoEdit extends Component {
accept="video/*"
type="video"
value={ this.props.attributes }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
);
}
Expand Down Expand Up @@ -96,3 +103,5 @@ export default class VideoEdit extends Component {
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}

export default withNotices( VideoEdit );
27 changes: 6 additions & 21 deletions editor/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import {
FormFileUpload,
Placeholder,
DropZone,
withNotices,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { Component } from '@wordpress/element';

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

onFilesUpload( files ) {
/**
* 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 { onSelect, type, multiple, onError } = this.props;
const setMedia = multiple ? onSelect : ( [ media ] ) => onSelect( media );
editorMediaUpload( {
allowedType: type,
filesList: files,
onFileChange: setMedia,
onError: ( errorMessage ) => {
onError( errorMessage );
if ( disableMaxUploadErrorMessages === false ) {
noticeOperations.createErrorNotice( errorMessage );
}
},
onError,
} );
}

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

return (
Expand All @@ -109,7 +94,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={ <Fragment>{ additionalNotices }{ noticeUI }</Fragment> }
notices={ notices }
>
<DropZone
onFilesDrop={ this.onFilesUpload }
Expand Down Expand Up @@ -156,4 +141,4 @@ class MediaPlaceholder extends Component {
}
}

export default withNotices( MediaPlaceholder );
export default MediaPlaceholder;

0 comments on commit 5950495

Please sign in to comment.